开发者

Distance calculation between two zip codes(post codes) in php?

开发者 https://www.devze.com 2023-01-13 04:30 出处:网络
开发者_C百科I am working on a website which requires distance calculation between two zip-codes. I have a database table which consists of zip-codes and their related latitudes and longitudes. I make
开发者_C百科

I am working on a website which requires distance calculation between two zip-codes. I have a database table which consists of zip-codes and their related latitudes and longitudes. I make use of this to calculate distance between the two places.

But I have a problem that this gives me straight line distance and not driving distance - how can I find that?

I'm using the following code

  $lat1 = $this->deg_to_rad($lat1);
   $lon1 = $this->deg_to_rad($lon1);
   $lat2 = $this->deg_to_rad($lat2);
   $lon2 = $this->deg_to_rad($lon2);

   $delta_lat = $lat2 - $lat1;
   $delta_lon = $lon2 - $lon1;

   $temp = pow(sin($delta_lat/2.0),2) + cos($lat1) * cos($lat2) * pow(sin($delta_lon/2.0),2);

   $distance = 3956 * 2 * atan2(sqrt($temp),sqrt(1-$temp));


You can somehere silently call a Google Maps GDirection(). It gives you not only a route but real distance also, but this is javascript soulution, and only way to provide this data to php is catch received distance and send to php by AJAX.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号