views:

42

answers:

1

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  A: 

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.

killer_PL
Hi, I will try to implement your suggestion. Thanks for that.
Aditya