I have a series of functions used to calculate the closest driving distance from the target address to one of my several locations and for the most part it works well. It first gets my closest location as the crow flies, then uses that address to calculate the actual driving distance to the target address and this is where the problem is. Sometimes the as the crow flies distance is far closer to one location but closer to drive from another so how can I calculate the driving distance from the start and skip the as the crow flies calculation?
// Provides "as the crow flies" distance from my closest location to target address
function closestLocation($Address) {
$values = locationFromAddress($Address);
$center_lat = $values[0];
$center_lng = $values[1];
// My locations
$query = sprintf("SELECT ID, Address, (3959 * acos(cos(radians('%s')) * cos(radians(lat)) * cos(radians(lng) - radians('%s')) + sin(radians('%s')) * sin( radians(lat)))) AS Distance
FROM locations
WHERE Address > 0 AND lat <> 0 AND lng <> 0
ORDER BY Distance ASC
LIMIT 1",
$center_lat,
$center_lng,
$center_lat);
$rowCat = DBConnect($query, "Select", "pchome_geoip");
// Returns array of the location
return $rowCat;
}
You can see that the query simply uses the latitude and longitude stored in the database to calculate it using the latitude and longitude of the target address provided by locationFromAddress() function:
function locationFromAddress($Address) {
$Address = urlencode($Address);
if ($Address) :
$geocode = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$Address&sensor=false");
$output = json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
return array($lat,$long);
else:
return "";
endif;
}
And then the details are used to calculate the driving distance using the Google API:
function compareDistance($unit,$inLatitude,$inLongitude,$outLatitude,$outLongitude) {
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=$inLatitude,$inLongitude&destination=$outLatitude,$outLongitude&sensor=false";
// Retrieve the URL contents
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $url);
$jsonResponse = curl_exec($c);
curl_close($c);
$dataset = json_decode($jsonResponse);
if(!$dataset)
return 0;
if(!isset($dataset->routes[0]->legs[0]->distance->value))
return 0;
$miles = ($dataset->routes[0]->legs[0]->distance->value * .00062);
// Google API returns meters, multiplied by .00062 to get miles
// Round up to nearest unit
if ($unit == "K") :
return ceil($miles * 1.609344);
elseif ($unit == "N") :
return ceil($miles * 0.8684);
else :
return ceil($miles);
endif;
}
function getDrivingDistance($myAddress, $custAddress, $unit) {
// 'M' is statute miles
// 'K' is kilometers
// 'N' is nautical miles
// Get accurate latitude and longitude based on actual location
$myValues = locationFromAddress($myAddress);
$myLat = $myValues[0];
$myLng = $myValues[1];
$custValues = locationFromAddress($custAddress);
$custLat = $custValues[0];
$custLng = $custValues[1];
// Returns actual driving distance
return compareDistance($unit,$myLat,$myLng,$custLat,$custLng);
}
Also, is there an API parameter for closest rather than fastest route?