使用 Google 地图 API 获取两个纬度局域网地址之间的距离(以公里为单位)
Get distance in Km between two lat lan address using Google Maps API
当运行这段代码它没有给出任何值。
但是当用 (37.910689 ,-97.250977),(16.110560, -94.174805)
替换 lat lan 值时,它会 returns 导致米。当传递任何其他 lat lan 值然后是美国地址时,它显示空白输出。
function location(){
$url="https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=28.666585,-77.229059&destinations=28.667053%2C-77.219898&key=Api key";
$json_string = $url;
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, TRUE); // Set second argument as TRUE
//print_r($obj['rows']); // Now this will works!
$prn=$obj['rows'];
foreach($prn as $row)
{
$ele=$row['elements'];
foreach($ele as $k)
{
$dis=$k['distance'];
foreach($dis as $l=> $lvalue){
$meter= $lvalue ;
}
}
}
return $meter ;
}
试试这个计算行驶距离和时间的代码。
代码:
<?php
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);
$status = "ERROR";
$dist = 0;
$time = 0;
if($response_a['rows'][0]['elements'][0]['status'] === 'OK') {
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
$status = "OK";
}
return array('status' => $status, 'distance' => $dist, 'time' => $time);
}
测试: 1.孟买到德里的距离2.随机无效lat lan.
$dist = GetDrivingDistance(19.228825, 28.644800,72.854118, 77.216721);
var_dump($dist);
// OUTPUT : array(3) { ["status"]=> string(2) "OK" ["distance"]=> string(9) "1 386 km" ["time"]=> string(15) "22 godz. 35 min" }
$dist = GetDrivingDistance(888.228825, 28.644800,72.854118, 77.216721);
var_dump($dist);
// OUTPUT : array(3) { ["status"]=> string(5) "ERROR" ["distance"]=> int(0) ["time"]=> int(0) }
提示:您可以通过var_dump($response_a)
内部函数获得更多详细信息。
PS : 确保您提供正确的 LAT LAN。
您在代码中指定的两个点位于大西洋中。您可以在 https://www.google.com 的搜索框中输入 28.667053,-77.219898 进行验证。 Google 响应 ZERO_RESULTS,因为它无法计算海洋中两点之间的距离。
更新:请注意,google 地图 api 给你的不是航拍图或两点之间的最短距离或 "as the crow flies" 距离。它是驾驶距离或步行距离。所以它必须知道这 2 个点才能给你一个距离。如果它不知道 2 个点或无法计算到 2 个点的路线,它将不会给出结果。 https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=20.254916,76.715059&destinations=20.3127202,76.6839331 - 这些点很近,但驾车/步行距离很远。 Goole 地图给出了驾驶/步行距离。
{
"destination_addresses": [
"28.667053,-77.219898"
],
"origin_addresses": [
"28.666585,-77.229059"
],
"rows": [
{
"elements": [
{
"status": "ZERO_RESULTS"
}
]
}
],
"status": "OK"
}
Google 响应美国境内 2 个点的正确距离,例如 https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=42.025819,-88.0854497&destinations=41.910088,-87.6929887
当运行这段代码它没有给出任何值。
但是当用 (37.910689 ,-97.250977),(16.110560, -94.174805)
替换 lat lan 值时,它会 returns 导致米。当传递任何其他 lat lan 值然后是美国地址时,它显示空白输出。
function location(){
$url="https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=28.666585,-77.229059&destinations=28.667053%2C-77.219898&key=Api key";
$json_string = $url;
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, TRUE); // Set second argument as TRUE
//print_r($obj['rows']); // Now this will works!
$prn=$obj['rows'];
foreach($prn as $row)
{
$ele=$row['elements'];
foreach($ele as $k)
{
$dis=$k['distance'];
foreach($dis as $l=> $lvalue){
$meter= $lvalue ;
}
}
}
return $meter ;
}
试试这个计算行驶距离和时间的代码。
代码:
<?php
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);
$status = "ERROR";
$dist = 0;
$time = 0;
if($response_a['rows'][0]['elements'][0]['status'] === 'OK') {
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
$status = "OK";
}
return array('status' => $status, 'distance' => $dist, 'time' => $time);
}
测试: 1.孟买到德里的距离2.随机无效lat lan.
$dist = GetDrivingDistance(19.228825, 28.644800,72.854118, 77.216721);
var_dump($dist);
// OUTPUT : array(3) { ["status"]=> string(2) "OK" ["distance"]=> string(9) "1 386 km" ["time"]=> string(15) "22 godz. 35 min" }
$dist = GetDrivingDistance(888.228825, 28.644800,72.854118, 77.216721);
var_dump($dist);
// OUTPUT : array(3) { ["status"]=> string(5) "ERROR" ["distance"]=> int(0) ["time"]=> int(0) }
提示:您可以通过var_dump($response_a)
内部函数获得更多详细信息。
PS : 确保您提供正确的 LAT LAN。
您在代码中指定的两个点位于大西洋中。您可以在 https://www.google.com 的搜索框中输入 28.667053,-77.219898 进行验证。 Google 响应 ZERO_RESULTS,因为它无法计算海洋中两点之间的距离。
更新:请注意,google 地图 api 给你的不是航拍图或两点之间的最短距离或 "as the crow flies" 距离。它是驾驶距离或步行距离。所以它必须知道这 2 个点才能给你一个距离。如果它不知道 2 个点或无法计算到 2 个点的路线,它将不会给出结果。 https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=20.254916,76.715059&destinations=20.3127202,76.6839331 - 这些点很近,但驾车/步行距离很远。 Goole 地图给出了驾驶/步行距离。
{
"destination_addresses": [
"28.667053,-77.219898"
],
"origin_addresses": [
"28.666585,-77.229059"
],
"rows": [
{
"elements": [
{
"status": "ZERO_RESULTS"
}
]
}
],
"status": "OK"
}
Google 响应美国境内 2 个点的正确距离,例如 https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=42.025819,-88.0854497&destinations=41.910088,-87.6929887