带或不带钥匙的驾驶距离Google API

时间:2018-05-31 18:52:07

标签: google-maps

谷歌似乎表明我需要使用SSL并提供一个用于距离计算的密钥,所以我试图重新设计我的功能以容纳但是结果只能得到0。使用distancematrix显示的URL在浏览器中运行时会给出正确的值,但我不确定如何在PHP中获取结果。似乎$ dataset是空的,所以在curl调用中必须有问题。任何想要获得距离的想法是否有办法获得最近距离而不是最快距离?

function compareDistance($inLatitude,$inLongitude,$outLatitude,$outLongitude,$units) {
    // If one of my own addresses is entered, return 0
    if ($inLatitude != $outLatitude && $inLongitude != $outLongitude) :
        $googlekey = "ABCDEFG123456";
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$inLatitude,$outLatitude&destinations=$outLatitude,$outLongitude&key=$googlekey";
        // Retrieve the URL contents
        $c = curl_init();
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        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;

        // Google API returns meters: multiply by .00062 to get miles
        $miles = ($dataset->routes[0]->legs[0]->distance->value * .00062);

        // Convert units
        if ($units == "K") :
            return $miles * 1.609344;
        elseif ($units == "N") :
            return $miles * 0.8684;
        else :
            return $miles;
        endif;
    else:
        return 0;
    endif;
}

$ jsonResponse输出:

{ "destination_addresses" : [ "44.405159,-121.2556414" ], "origin_addresses" : [ "38.257061,44.405159" ], "rows" : [ { "elements" : [ { "status" : "ZERO_RESULTS" } ] } ], "status" : "OK" } 

1 个答案:

答案 0 :(得分:0)

现在正在运行,可以输出英里,海里,公里或米。使用Google API时,不需要units参数,因为原始输出始终以米为单位。我还通过以逗号分隔的对提交纬度/经度来简化它,而不是单独提交每个

function compareDistance($inLocation,$outLocation,$units,$googlekey) {
    // If one of my own addresses is entered, return 0
    if ($inLocation != $outLocation) :
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$inLocation&destinations=$outLocation&mode=driving&key=$googlekey";

        // Retrieve the URL contents
        $c = curl_init();
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($c, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($c, CURLOPT_URL, $url);
        $jsonResponse = curl_exec($c);
        curl_close($c);

        $dataset = json_decode($jsonResponse);

        if (!$dataset)
            return 0;
        if (!isset($dataset->rows[0]->elements[0]->distance->value))
            return 0;

        $meters = $dataset->rows[0]->elements[0]->distance->value;

        // Convert default meters to other units
        if ($units == "M") :
            return ($meters / 1609.344); // Miles
        elseif ($units == "N") :
            return ($meters / 1852); // Nautical miles
        elseif ($units == "K") :
            return ($meters * 0.001); // Kilometers
        else :
            return $meters;
        endif;
    else:
        return 0;
    endif;
}