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

Php 使用谷歌地图API获取两点之间的行驶距离,php,Php,我正在尝试使用谷歌地图API获取两点之间的驾驶距离。现在,我得到了直接距离的代码: 此功能用于获取lat和lng: function get_coordinates($city, $street, $province) { $address = urlencode($city.','.$street.','.$province); $url = "http://maps.google.com/maps/api/geocode/json?address=$address&s

我正在尝试使用谷歌地图API获取两点之间的驾驶距离。现在,我得到了直接距离的代码:

此功能用于获取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;
}
此函数基于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;
}
此功能以公里为单位打印距离:

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;
}
用法:

$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';
$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>';
}
$coordinates1=获取坐标('Katowice','Korfantego','Katowicki');
$coordinates 2=获取坐标('Tychy','Jana Pawła II','Tyski');
回音“距离:”.圆形(获取距离($coordinates 1['lat'],$coordinates 2['lat'],$coordinates 1['long'],$coordinates 2['long']),1)。'km';
但是这个代码得到了直接的距离。我需要驾驶距离。 如何使用谷歌地图API获取行车距离


谢谢你的帮助

好的,我用距离矩阵找到了解决方案:

此函数用于从城市、地址、省份获取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;
    }
}
此功能用于计算行驶距离和行驶时间:

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);
}
用法:

$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';
$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>';
}
$coordinates 1=获取坐标('Tychy','Jana Pawła II','Śląskie');
$coordinates 2=获取坐标('Lędziny','Lędzińska','Ląskie');
if(!$Coordinates 1 |!$Coordinates 2)
{
回音“坏地址”;
}
其他的
{
$dist=GetDrivingDistance($coordinates 1['lat'],$coordinates 2['lat'],$coordinates 1['long'],$coordinates 2['long']);
回音“距离:”.$dist['Distance'].
行程时间持续时间:“.$dist['time']”; }
返回:

距离:11.2公里
行程时间:15分钟

您可以使用谷歌距离矩阵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>";
/$details=”http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+弗朗西斯科&模式=驾驶&传感器=错误”;
//还可以在原点中传递纬度/经度值
$details=”http://maps.googleapis.com/maps/api/distancematrix/json?origins=41.43206,-81.38992&目的地=旧金山&模式=驾驶&传感器=假”;
$json=file\u get\u contents($details);
$details=json_decode($json,TRUE);
回声“;打印(详细信息);回声“;

这是谷歌距离矩阵API的链接:-

距离矩阵API将完成这项工作,然后不需要使用地址和距离来计算坐标

这是API

PHP代码:

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

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

<div>
     <?php echo $response_a['rows'][0]['elements'][0]['distance']['text']; ?>
</div>
现在最困难的部分是如何显示距离。通过研究,我得到了这个答案:

<!DOCTYPE html>
<html>
    <body>
        <form action="" method="post">
            <label>Origin:</label> <input type="text" name="o" placeholder="Enter Origin location" required> <br><br>
            <label>Destination:</label> <input type="text" name="d" placeholder="Enter Destination location" required> <br><br>
            <input type="submit" value="Calculate distance & time" name="submit"> <br><br>
        </form>
        <?php
            if(isset($_POST['submit'])){
            $origin = $_POST['o']; $destination = $_POST['d'];
            $api = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=".$origin."&destinations=".$destination."&key=YOUR_API_KEY");
            $data = json_decode($api);
        ?>
            <label><b>Distance: </b></label> <span><?php echo ((int)$data->rows[0]->elements[0]->distance->value / 1000).' Km'; ?></span> <br><br>
            <label><b>Travel Time: </b></label> <span><?php echo $data->rows[0]->elements[0]->duration->text; ?></span> 

        <?php } ?>
    </body>
</html>


您可以在任何地方打印此结果。它对我有用。

你可以尝试使用谷歌距离矩阵API。下面是一个示例HTML、PHP代码。在此,您必须使用应用程序密钥更改“您的API密钥”


来源:

目的地:



距离:

旅行时间:
你看过API了吗?@VivekPradhan ye。这很有用:谢谢!11,2km在这里定义了什么?@humbledust只需更改URLEdit问题中的语言,并添加一些解释