使用谷歌地图API获取两点间的行驶距离

时间:2023-01-13 15:24:19

I'm trying to get driving distance between two points using Google Maps API. Now, I have code which get direct distance:

我想用谷歌地图API求两点间的行驶距离。现在,我有直接距离的代码:

This function get lat and lng:

此函数为lat和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&region=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:

该函数基于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?

但是这个代码有直接的距离。我需要开车的距离。如何使用谷歌地图API获取行驶距离?

Thanks for your help!

谢谢你的帮助!

4 个解决方案

#1


52  

OK, I found solution using distance matrix: https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixRequests

我找到了使用距离矩阵的解决方案:https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixRequests

This function get lat & lng from city, adress, province:

此功能从省(市)adress,市(市)获得lat & 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&region=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);
    $status = $response_a->status;

    if ( $status == 'ZERO_RESULTS' )
    {
        return FALSE;
    }
    else
    {
        $return = array('lat' => $response_a->results[0]->geometry->location->lat, 'long' => $long = $response_a->results[0]->geometry->location->lng);
        return $return;
    }
}

This function calculate driving distance and travel time duration:

该函数计算行驶距离和行驶时间:

function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
    $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, true);
    $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
    $time = $response_a['rows'][0]['elements'][0]['duration']['text'];

    return array('distance' => $dist, 'time' => $time);
}

Usage:

用法:

$coordinates1 = get_coordinates('Tychy', 'Jana Pawła II', 'Śląskie');
$coordinates2 = get_coordinates('Lędziny', 'Lędzińska', 'Śląskie');
if ( !$coordinates1 || !$coordinates2 )
{
    echo 'Bad address.';
}
else
{
    $dist = GetDrivingDistance($coordinates1['lat'], $coordinates2['lat'], $coordinates1['long'], $coordinates2['long']);
    echo 'Distance: <b>'.$dist['distance'].'</b><br>Travel time duration: <b>'.$dist['time'].'</b>';
}

Return:

返回:

Distance: 11,2 km Travel time duration: 15 min

距离:11、2公里行程时间:15分钟。

#2


13  

You can do this by using Google Distance Matrix API:

可以使用谷歌距离矩阵API实现:

// $details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=driving&sensor=false";

    //you can also pass latitude/longitude values in origins


   $details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=41.43206,-81.38992&destinations=San+Francisco&mode=driving&sensor=false";


    $json = file_get_contents($details);

    $details = json_decode($json, TRUE);

    echo "<pre>"; print_r($details); echo "</pre>";

Here is the link of Google Distance Matrix API :- https://developers.google.com/maps/documentation/distancematrix/?csw=1

下面是谷歌距离矩阵API的链接:- https://developers.google.com/maps/documentation/distancematrix/?

#3


3  

This is the method I got. In it, you can enter any manual distance from sessions or database.

这是我得到的方法。在其中,您可以从会话或数据库输入任何手动距离。

$origin = urlencode($_SESSION['source1']);
$destination = urlencode($_SESSION['dest1']);

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&key=Your api key;

$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, true);
?>

Now the tough part was how to show distance. Well, with research, I got this answer:

现在最难的部分是如何显示距离。通过研究,我得到了答案

<div class="">
  <?php echo $response_a['rows'][0]['elements'][0]['distance']['text']; ?>

</div>

You can print this result anywhere. It's working for me.

您可以在任何地方打印此结果。这对我的工作。

#4


1  

Distance matrix API will do job then no need to calculate coordinates by using address and then Distance!!

距离矩阵API将做的工作,然后不需要计算坐标通过使用地址和距离!!

Here is the API
https://maps.googleapis.com/maps/api/distancematrix/json?origins=orginaddress&destinations=destinationaddress&key=YOUR_API_KEY

这是API https://maps.googleapis.com/maps/api/distancematrix/json?origin =orginaddress&destinations= destinationaddress&key_key

PHP Code :

PHP代码:

$ch = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=orginaddress&destinations=destinationaddress&key=YOUR_API_KEY";
$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, true);
//print_( $response_a );

#1


52  

OK, I found solution using distance matrix: https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixRequests

我找到了使用距离矩阵的解决方案:https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixRequests

This function get lat & lng from city, adress, province:

此功能从省(市)adress,市(市)获得lat & 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&region=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);
    $status = $response_a->status;

    if ( $status == 'ZERO_RESULTS' )
    {
        return FALSE;
    }
    else
    {
        $return = array('lat' => $response_a->results[0]->geometry->location->lat, 'long' => $long = $response_a->results[0]->geometry->location->lng);
        return $return;
    }
}

This function calculate driving distance and travel time duration:

该函数计算行驶距离和行驶时间:

function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
    $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, true);
    $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
    $time = $response_a['rows'][0]['elements'][0]['duration']['text'];

    return array('distance' => $dist, 'time' => $time);
}

Usage:

用法:

$coordinates1 = get_coordinates('Tychy', 'Jana Pawła II', 'Śląskie');
$coordinates2 = get_coordinates('Lędziny', 'Lędzińska', 'Śląskie');
if ( !$coordinates1 || !$coordinates2 )
{
    echo 'Bad address.';
}
else
{
    $dist = GetDrivingDistance($coordinates1['lat'], $coordinates2['lat'], $coordinates1['long'], $coordinates2['long']);
    echo 'Distance: <b>'.$dist['distance'].'</b><br>Travel time duration: <b>'.$dist['time'].'</b>';
}

Return:

返回:

Distance: 11,2 km Travel time duration: 15 min

距离:11、2公里行程时间:15分钟。

#2


13  

You can do this by using Google Distance Matrix API:

可以使用谷歌距离矩阵API实现:

// $details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=driving&sensor=false";

    //you can also pass latitude/longitude values in origins


   $details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=41.43206,-81.38992&destinations=San+Francisco&mode=driving&sensor=false";


    $json = file_get_contents($details);

    $details = json_decode($json, TRUE);

    echo "<pre>"; print_r($details); echo "</pre>";

Here is the link of Google Distance Matrix API :- https://developers.google.com/maps/documentation/distancematrix/?csw=1

下面是谷歌距离矩阵API的链接:- https://developers.google.com/maps/documentation/distancematrix/?

#3


3  

This is the method I got. In it, you can enter any manual distance from sessions or database.

这是我得到的方法。在其中,您可以从会话或数据库输入任何手动距离。

$origin = urlencode($_SESSION['source1']);
$destination = urlencode($_SESSION['dest1']);

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&key=Your api key;

$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, true);
?>

Now the tough part was how to show distance. Well, with research, I got this answer:

现在最难的部分是如何显示距离。通过研究,我得到了答案

<div class="">
  <?php echo $response_a['rows'][0]['elements'][0]['distance']['text']; ?>

</div>

You can print this result anywhere. It's working for me.

您可以在任何地方打印此结果。这对我的工作。

#4


1  

Distance matrix API will do job then no need to calculate coordinates by using address and then Distance!!

距离矩阵API将做的工作,然后不需要计算坐标通过使用地址和距离!!

Here is the API
https://maps.googleapis.com/maps/api/distancematrix/json?origins=orginaddress&destinations=destinationaddress&key=YOUR_API_KEY

这是API https://maps.googleapis.com/maps/api/distancematrix/json?origin =orginaddress&destinations= destinationaddress&key_key

PHP Code :

PHP代码:

$ch = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=orginaddress&destinations=destinationaddress&key=YOUR_API_KEY";
$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, true);
//print_( $response_a );