I'm trying to get the distance between 2 locations through Google Maps API using the below PHP script, which apparently works (their hosted test url works, added below), but when I use the code they provide on my own server it doesn't seem to do anything... PHP is working just fine by the way, and it doesn't seem to be doing anything funny that would require me turning something on in my ini file.
I'm not sure what the issue is... been at this all day.
Any ideas? Maybe it is my .ini?
I'm not sure what I'm missing here. I'm using a shared host, but that shouldn't make a difference. It should technically work as is, any way that is why I'm here - I need some insight.
Website with source code: http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/#comment-24117
Their live demo: http://demos.codexworld.com/distance-between-two-addresses-google-maps-api-php/
My full test code:
<?php
/**
*
* Author: CodexWorld
* Function Name: getDistance()
* $addressFrom => From address.
* $addressTo => To address.
* $unit => Unit type.
*
**/
function getDistance($addressFrom, $addressTo, $unit){
//Change address format
$formattedAddrFrom = str_replace(' ','+',$addressFrom);
$formattedAddrTo = str_replace(' ','+',$addressTo);
//Send request and receive json data
$geocodeFrom = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false');
$outputFrom = json_decode($geocodeFrom);
$geocodeTo = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false');
$outputTo = json_decode($geocodeTo);
//Get latitude and longitude from geo data
$latitudeFrom = $outputFrom->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.1515;
$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 (!empty($_POST['addrFrom']) && !empty($_POST['addrTo'])) {
$addressFrom = htmlspecialchars($_POST['addrFrom']);
$addressTo = htmlspecialchars($_POST['addrTo']);
$distance = getDistance($addressFrom, $addressTo, "K");
$success = true;
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="post">
<p class="distance"><span>Distance:<?php if ($success == true) { echo ' ' . $distance; } ?></span> </p>
<p><label>Address From</label><input type="text" name="addrFrom" value=""></p>
<p><label>Address To</label><input type="text" name="addrTo" value=""></p>
<p><input type="submit" name="submit" value="Calculate Distance"></p>
</form>
</body>
</html>