I'm trying to get driving distance between two points using Google Maps API. Now, I have code which get 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 calculate 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 print 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';
But this code get direct distance. I need driving distance.
How I can get driving distance using google maps API?
Thanks for your help!
See Question&Answers more detail:
os