Hello! In this post, you will learn how to calculate distance between two places using google map API in php.
Here you will calculate by air distance between two countries.
See below ..how to do it.
results[0]->geometry->location->lat;
$longitudeFrom = $outputFrom->results[0]->geometry->location->lng;
$latitudeTo = $outputTo->results[0]->geometry->location->lat;
$longitudeTo = $outputTo->results[0]->geometry->location->lng;
//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) + cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.151;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344).' km';
} else if ($unit == "N") {
return ($miles * 0.8684).' nm';
} else {
return $miles.' mi';
}
}
if(isset($_REQUEST['cal']))
{
$addressFrom = $_POST['addressfrom'];
$addressTo =$_POST['addressto'];
$distance = getDistance($addressFrom, $addressTo, "K");
}
?>
Distance Calculation
Distance:

Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co
