Google 距离矩阵为改变的源和目的地提供不同的距离
Google distance matrix gives different distance for altered source and destination
我正在使用 google 地图距离矩阵 api 来查找一组源和目的地之间的距离和持续时间。我正在尝试找出澳大利亚悉尼任意两个郊区之间的距离。对于特定的源和目标,当我更改目标的源和源的目标时,我会得到不同的结果。我在PHP中的函数如下:
private function getDistance($from, $to, &$error,&$estimated_time) {
$start =urlencode($from.'+Sydney+Australia');
$destination = urlencode($to.'+Sydney+Australia');
$mode = "driving";
$json_resp = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$start&destinations=$destination&mode=$mode&language=en-EN&sensor=false");
dd($json_resp);
$data = json_decode($json_resp);
$distance = $data->rows[0]->elements[0]->distance->value;
$time = $data->rows[0]->elements[0]->duration->value;
if(empty($distance) || empty($time)){
$error='Empty data received. Could not find distance and duration between '.$from.' and '.$to;
$estimated_time='N/A';
return 0.00;
}else{
$estimated_time=$this->secondsToTime($time);
return floatval($distance/1000);
}
}
以下是当来源 $from='CBD' 和 $to='Sydney Int. Airport'.
时来自 api 的响应
string(569) "{ "destination_addresses" : [ "yes Optus Sydney
International Airport, Airport Drive, Mascot NSW 2020, Australia" ],
"origin_addresses" : [ "Sydney NSW, Australia" ], "rows" : [ {
"elements" : [ { "distance" : { "text" : "9.5 km", "value" : 9482 },
"duration" : { "text" : "16 mins", "value" : 930 }, "status" : "OK" }
] } ], "status" : "OK" } "
然而,当源 $from='Sydney Int. Airport' 和 $to='CBD' 时,输出是完全不同的,如下所示。
string(572) "{ "destination_addresses" : [ "Sydney NSW, Australia" ],
"origin_addresses" : [ "yes Optus Sydney International Airport,
Airport Drive, Mascot NSW 2020, Australia" ], "rows" : [ { "elements"
: [ { "distance" : { "text" : "17.5 km", "value" : 17513 }, "duration"
: { "text" : "18 mins", "value" : 1075 }, "status" : "OK" } ] } ],
"status" : "OK" } "
对于同一组郊区,我得到 9.5 公里和 17.5 公里,这是相当大的差异。有人可以建议吗?我正在使用 Laravel 框架在 PHP 中编程。
这是我检查步骤的代码。
$origin =urlencode('Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia');
$destination = urlencode('Sydney NSW, Australia');
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&sensor=false";
$data = @file_get_contents($url);
$data = json_decode($data,true);
foreach($data[routes][0][legs] as $val){
echo '<h4>' . $val[distance][text].'</h4>';
foreach ($val[steps] as $v){
echo $v[distance][text] . ' ' . $v[duration][text] . ' ' . $v[html_instructions] . ' ' . $v[end_location][lat] . ',' . $v[end_location][lng] . '<br>';
}
}
echo "<hr>";
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=$destination&destination=$origin&sensor=false";
$data = @file_get_contents($url);
$data = json_decode($data,true);
foreach($data[routes][0][legs] as $val){
echo '<h4>' . $val[distance][text].'</h4>';
foreach ($val[steps] as $v){
echo $v[distance][text] . ' ' . $v[duration][text] . ' ' . $v[html_instructions] . ' ' . $v[end_location][lat] . ',' . $v[end_location][lng] . '<br>';
}
}
结果
1.0 km 1 min Head west on Airport Dr toward Link Rd -33.9311912,151.1638899
0.4 km 1 min Take the ramp on the right to Metroad 5/Metroad 3/Rockdale/Sydney/Sydney Southwest -33.9338978,151.1613411
0.9 km 1 min Continue onto Marsh St -33.937509,151.1533556
0.3 km 1 min Slight left onto the Metroad 5 ramp to Port Botany/Sydney -33.939696,151.1553088
1.8 km 2 mins Merge onto M5 -33.9459699,151.1724068
11.4 km 10 mins Continue onto M1
Partial toll road
-33.871314,151.2179597
0.3 km 1 min Take the exit toward City North
Toll road
-33.8685533,151.2180181
29 m 1 min Slight left toward Sir John Young Cres (signs for Macquarie Street/City) -33.8683378,151.2178461
0.2 km 1 min Keep left, follow signs for Sir John Young Cres -33.867197,151.2163908
0.2 km 1 min Slight left onto Sir John Young Cres -33.8665953,151.2146068
0.2 km 1 min Continue onto Shakespeare Pl -33.8658241,151.2129679
0.1 km 1 min Turn left onto Macquarie St -33.8665564,151.2124343
0.5 km 2 mins Turn right onto Hunter St -33.865537,151.2072971
0.2 km 1 min Turn left onto George St -33.8674649,151.207086
.
1.7 km 4 mins Head south on George St toward Barrack St -33.8828335,151.2041774
0.3 km 1 min Slight left onto Lee St -33.8853847,151.2029028
1.2 km 2 mins Continue onto Regent St -33.8951109,151.1991316
0.7 km 1 min Continue onto Botany Rd -33.9014863,151.2011191
0.1 km 1 min Turn right onto McEvoy St -33.9017513,151.200088
0.5 km 1 min Turn left at the 1st cross street onto Wyndham St -33.905706,151.2019072
54 m 1 min Continue onto Green Square -33.9061048,151.2022107
3.1 km 4 mins Turn right onto O'Riordan St -33.9306923,151.186595
0.2 km 1 min Turn right onto Robey St -33.9312613,151.1842737
1.0 km 1 min Turn right onto Qantas Dr -33.9259671,151.1785015
0.5 km 1 min Continue onto Airport Dr
您可以尝试使用方向而不是距离,并指定 waypoints.
单个航点
$waypoints= '-33.9306923,151.186595'
多个Waypoints
$waypoints= '-33.9306923,151.186595|33.9312613,151.1842737'
用于API:
destination=$destination&waypoints=-`33.9306923,151.186595|33.9312613,151.1842737&sensor`
最简单的方法是Google路线
您得到不同结果的原因是因为您使用的是 API 调用方向 API 而不是距离矩阵 API。
方向API将为您提供当前场景下行程时间最短的路线。根据交通情况,路线可能会发生变化,并且同一组起点和目的地可以有不同的路线。
要获得相同的距离,请使用距离矩阵 API 调用,如下所示:
https://maps.googleapis.com/maps/api/distancematrix/output?parameters
希望这会有所帮助!!
您可以尝试从 google 服务器检索 JSON:
示例:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Hyderabad+Telangana+%20India&destinations=Mumbai+Maharashtra+India
Returns:
{
"destination_addresses" : [ "Mumbai, Maharashtra, Índia" ],
"origin_addresses" : [ "Hyderabad, Telangana, Índia" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "441 mi",
"value" : 709884
},
"duration" : {
"text" : "13 horas 3 minutos",
"value" : 46984
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
然后,根据需要使用任何信息,例如:
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Hyderabad+Telangana+%20India&destinations=Mumbai+Maharashtra+India';
$json = file_get_contents($url);
$result = json_decode($json, true);
echo $result['rows'][0]['elements'][0]['distance']['text']."<br/>";
echo $result['rows'][0]['elements'][0]['duration']['text'];
我正在使用 google 地图距离矩阵 api 来查找一组源和目的地之间的距离和持续时间。我正在尝试找出澳大利亚悉尼任意两个郊区之间的距离。对于特定的源和目标,当我更改目标的源和源的目标时,我会得到不同的结果。我在PHP中的函数如下:
private function getDistance($from, $to, &$error,&$estimated_time) {
$start =urlencode($from.'+Sydney+Australia');
$destination = urlencode($to.'+Sydney+Australia');
$mode = "driving";
$json_resp = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$start&destinations=$destination&mode=$mode&language=en-EN&sensor=false");
dd($json_resp);
$data = json_decode($json_resp);
$distance = $data->rows[0]->elements[0]->distance->value;
$time = $data->rows[0]->elements[0]->duration->value;
if(empty($distance) || empty($time)){
$error='Empty data received. Could not find distance and duration between '.$from.' and '.$to;
$estimated_time='N/A';
return 0.00;
}else{
$estimated_time=$this->secondsToTime($time);
return floatval($distance/1000);
}
}
以下是当来源 $from='CBD' 和 $to='Sydney Int. Airport'.
时来自 api 的响应string(569) "{ "destination_addresses" : [ "yes Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia" ], "origin_addresses" : [ "Sydney NSW, Australia" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "9.5 km", "value" : 9482 }, "duration" : { "text" : "16 mins", "value" : 930 }, "status" : "OK" } ] } ], "status" : "OK" } "
然而,当源 $from='Sydney Int. Airport' 和 $to='CBD' 时,输出是完全不同的,如下所示。
string(572) "{ "destination_addresses" : [ "Sydney NSW, Australia" ], "origin_addresses" : [ "yes Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "17.5 km", "value" : 17513 }, "duration" : { "text" : "18 mins", "value" : 1075 }, "status" : "OK" } ] } ], "status" : "OK" } "
对于同一组郊区,我得到 9.5 公里和 17.5 公里,这是相当大的差异。有人可以建议吗?我正在使用 Laravel 框架在 PHP 中编程。
这是我检查步骤的代码。
$origin =urlencode('Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia');
$destination = urlencode('Sydney NSW, Australia');
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&sensor=false";
$data = @file_get_contents($url);
$data = json_decode($data,true);
foreach($data[routes][0][legs] as $val){
echo '<h4>' . $val[distance][text].'</h4>';
foreach ($val[steps] as $v){
echo $v[distance][text] . ' ' . $v[duration][text] . ' ' . $v[html_instructions] . ' ' . $v[end_location][lat] . ',' . $v[end_location][lng] . '<br>';
}
}
echo "<hr>";
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=$destination&destination=$origin&sensor=false";
$data = @file_get_contents($url);
$data = json_decode($data,true);
foreach($data[routes][0][legs] as $val){
echo '<h4>' . $val[distance][text].'</h4>';
foreach ($val[steps] as $v){
echo $v[distance][text] . ' ' . $v[duration][text] . ' ' . $v[html_instructions] . ' ' . $v[end_location][lat] . ',' . $v[end_location][lng] . '<br>';
}
}
结果
1.0 km 1 min Head west on Airport Dr toward Link Rd -33.9311912,151.1638899
0.4 km 1 min Take the ramp on the right to Metroad 5/Metroad 3/Rockdale/Sydney/Sydney Southwest -33.9338978,151.1613411
0.9 km 1 min Continue onto Marsh St -33.937509,151.1533556
0.3 km 1 min Slight left onto the Metroad 5 ramp to Port Botany/Sydney -33.939696,151.1553088
1.8 km 2 mins Merge onto M5 -33.9459699,151.1724068
11.4 km 10 mins Continue onto M1
Partial toll road
-33.871314,151.2179597
0.3 km 1 min Take the exit toward City North
Toll road
-33.8685533,151.2180181
29 m 1 min Slight left toward Sir John Young Cres (signs for Macquarie Street/City) -33.8683378,151.2178461
0.2 km 1 min Keep left, follow signs for Sir John Young Cres -33.867197,151.2163908
0.2 km 1 min Slight left onto Sir John Young Cres -33.8665953,151.2146068
0.2 km 1 min Continue onto Shakespeare Pl -33.8658241,151.2129679
0.1 km 1 min Turn left onto Macquarie St -33.8665564,151.2124343
0.5 km 2 mins Turn right onto Hunter St -33.865537,151.2072971
0.2 km 1 min Turn left onto George St -33.8674649,151.207086
.
1.7 km 4 mins Head south on George St toward Barrack St -33.8828335,151.2041774
0.3 km 1 min Slight left onto Lee St -33.8853847,151.2029028
1.2 km 2 mins Continue onto Regent St -33.8951109,151.1991316
0.7 km 1 min Continue onto Botany Rd -33.9014863,151.2011191
0.1 km 1 min Turn right onto McEvoy St -33.9017513,151.200088
0.5 km 1 min Turn left at the 1st cross street onto Wyndham St -33.905706,151.2019072
54 m 1 min Continue onto Green Square -33.9061048,151.2022107
3.1 km 4 mins Turn right onto O'Riordan St -33.9306923,151.186595
0.2 km 1 min Turn right onto Robey St -33.9312613,151.1842737
1.0 km 1 min Turn right onto Qantas Dr -33.9259671,151.1785015
0.5 km 1 min Continue onto Airport Dr
您可以尝试使用方向而不是距离,并指定 waypoints.
单个航点
$waypoints= '-33.9306923,151.186595'
多个Waypoints
$waypoints= '-33.9306923,151.186595|33.9312613,151.1842737'
用于API:
destination=$destination&waypoints=-`33.9306923,151.186595|33.9312613,151.1842737&sensor`
最简单的方法是Google路线
您得到不同结果的原因是因为您使用的是 API 调用方向 API 而不是距离矩阵 API。
方向API将为您提供当前场景下行程时间最短的路线。根据交通情况,路线可能会发生变化,并且同一组起点和目的地可以有不同的路线。
要获得相同的距离,请使用距离矩阵 API 调用,如下所示:
https://maps.googleapis.com/maps/api/distancematrix/output?parameters
希望这会有所帮助!!
您可以尝试从 google 服务器检索 JSON:
示例:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Hyderabad+Telangana+%20India&destinations=Mumbai+Maharashtra+India
Returns:
{
"destination_addresses" : [ "Mumbai, Maharashtra, Índia" ],
"origin_addresses" : [ "Hyderabad, Telangana, Índia" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "441 mi",
"value" : 709884
},
"duration" : {
"text" : "13 horas 3 minutos",
"value" : 46984
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
然后,根据需要使用任何信息,例如:
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Hyderabad+Telangana+%20India&destinations=Mumbai+Maharashtra+India';
$json = file_get_contents($url);
$result = json_decode($json, true);
echo $result['rows'][0]['elements'][0]['distance']['text']."<br/>";
echo $result['rows'][0]['elements'][0]['duration']['text'];