1
votes

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));
1
When saying "actual distance", do you mean "great circle distance", "driving distance" or something else entirely? There's no measure you could call the "one real distance" in this context (there are several valid possibilities), you need to be more specific. - Piskvor left the building
HI, i mean the driving distance. Bcoz there is sometimes a very large difference between the straight line distances and driving distance. - Aditya

1 Answers

1
votes

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.