Using Google Maps API, I'm attempting to get the driving distance between two points. I now have a function that calculates direct distance:
This function get lat and lng:
function get_coordinates($city, $street, $province)
{
$address = urlencode($city.','.$street.','.$province);
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=Poland";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
$return = array('lat' => $response_a->results[0]->geometry->location->lat, 'long' => $long = $response_a->results[0]->geometry->location->lng);
return $return;
}
This function calculates distance between two points based on lat & lng:
function getDistanceBetweenPoints($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$kilometers = $miles * 1.609344;
return $kilometers;
}
This function prints distance by kilometers:
function get_distance($lat1, $lat2, $long1, $long2)
{
/* These are two points in New York City */
$point1 = array('lat' => $lat1, 'long' => $long1);
$point2 = array('lat' => $lat2, 'long' => $long2);
$distance = getDistanceBetweenPoints($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
return $distance;
}
Usage:
$coordinates1 = get_coordinates('Katowice', 'Korfantego', 'Katowicki');
$coordinates2 = get_coordinates('Tychy', 'Jana Pawła II', 'Tyski');
echo 'Distance: <b>'.round(get_distance($coordinates1['lat'], $coordinates2['lat'], $coordinates1['long'], $coordinates2['long']), 1).'</b> km';
However, this code obtains a direct distance. I require a travel distance. How can I use the Google Maps API to get the driving distance?