I have a function in PHP which calculates the distance between two places.
Here is the php code :
function distance($lat1, $lon1, $lat2, $lon2) {
$earth_radius = 6371;
$delta_lat = $lat2 - $lat1 ;
$delta_lon = $lon2 - $lon1 ;
$distance = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($delta_lon)) ;
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
$distance = round($distance, 4);
return $distance = $distance * 1.609344;
}
It gives good calculations around 25 km, but around 500 km the calculations are wrong.
My other question is it really giving miles or kilometers?
For example this map gives a distance of 443 km from Morbi to Surat, but the function gives me a result of 274 km
Ok, now that’s clear that you want the road distance, we can give you a proper answer, to get the result, you can use the Google API Distance Matrix, you have different options and parameters, you have to figure out which suits you the most, be aware that there’s some limitation (for the free edition):
However for the purpose of answering your question i wrote a simple PHP script that gets a XML file and parse the distance/duration with SimpleXMLElement …
Hope this was helpful 🙂