我使用谷歌地图距离矩阵有两种方法。 第一个是通过javascript:
service.getDistanceMatrix({
origins : [origin],
destinations : s[0],
travelMode : google.maps.DirectionsTravelMode.DRIVING,
unitSystem : google.maps.UnitSystem.METRIC,
avoidHighways : false,
avoidTolls : false,
drivingOptions : {
departureTime : new Date(),
trafficModel : google.maps.TrafficModel.BEST_GUESS
}
}, function(response, status) {
console.log(response);
});
第二个是通过php
function GetDrivingDistance($origins, $d_lat, $d_lon){
$url = "https://maps.googleapis.com/maps/api/distancematrix/json";
$url .= "?origins=".$origins;
$url .= "&destinations=".$d_lat.",".$d_lon;
$url .= "&mode=driving";
$url .= "&departure_time=" . strtotime(date('Y-m-d H:i:s'));
$url .= "&traffic_model=best_guess";
$url .= "&language=en";
$url .= "&key=" . GOOGLE_MAPS_KEY_DIS_MATRIX;
// echo $url;
$routes=json_decode(file_get_contents($url),true);
return $routes;
}
当我同时运行两个代码时,我得到的结果没有任何错误。令我困惑的是结果不一样!!!
我尝试将结果与谷歌地图(https://www.google.com/maps)进行比较,我得出结论,谷歌地图的结果与使用PHP运行的代码完全相同。
有谁知道为什么会这样?两种情况下的参数都是相同的,所以我等着获得相同的输出。
e.g。 PHP输出:
php find_nearest.php 37.9911865,23.7547297
Array
(
[destination_addresses] => Array
(
[0] => Geor. Kedrinou 46, Athina 115 22, Greece
)
[origin_addresses] => Array
(
[0] => Posidonos, Limni 340 05, Greece
[1] => A/D PAThE 53-65, Ag. Ioannis Rentis 182 33, Greece
)
[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 155 km
[value] => 154939
)
[duration] => Array
(
[text] => 2 hours 31 mins
[value] => 9089
)
[duration_in_traffic] => Array
(
[text] => 2 hours 34 mins
[value] => 9262
)
[status] => OK
)
)
)
[1] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 14.1 km
[value] => 14103
)
[duration] => Array
(
[text] => 28 mins
[value] => 1650
)
[duration_in_traffic] => Array
(
[text] => 30 mins
[value] => 1799
)
[status] => OK
)
)
)
)
[status] => OK
)
javascript结果:
/**/_xdc_._s0qz6t && _xdc_._s0qz6t( {
"destination_addresses" : [
"A/D PAThE 53-65, Ag. Ioannis Rentis 182 33, Greece",
"Posidonos, Limni 340 05, Greece"
],
"origin_addresses" : [ "Georgiou Kedrinou 46, Athens 115 22, Greece" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "11,2 km",
"value" : 11204
},
"duration" : {
"text" : "24 mins",
"value" : 1449
},
"duration_in_traffic" : {
"text" : "27 mins",
"value" : 1636
},
"status" : "OK"
},
{
"distance" : {
"text" : "156 km",
"value" : 155859
},
"duration" : {
"text" : "2 hours 32 mins",
"value" : 9097
},
"duration_in_traffic" : {
"text" : "2 hours 33 mins",
"value" : 9164
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
)