Google距离矩阵为改变源和目的地提供了不同的距离 [英] Google distance matrix gives different distance for altered source and destination

查看:60
本文介绍了Google距离矩阵为改变源和目的地提供了不同的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用google地图距离矩阵api查找一组源和目标之间的距离和持续时间。我试图找到澳大利亚悉尼任何两个郊区之间的距离。对于特定的来源和目的地,当我更改来源的目的地和目的地来源时,我会得到不同的结果。我在PHP中的功能如下:

pre $ private $ getDistance($ from,$ to,& $ error,& $ estimated_time){
$ start = urlencode($ from。'+ Sydney + Australia');
$ destination = urlencode($ to。'+ Sydney + Australia');
$ mode =驾驶;
$ json_resp = file_get_contents(http://maps.googleapis.com/maps/api/distancematrix/json?origins=$start&destinations=$destination&mode=$mode&language=en-EN&sensor =假);
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 ='收到空数据。无法找到'。$ from。'和'。$ to之间的距离和持续时间。
$ estimated_time ='N / A';
返回0.00;
} else {
$ estimated_time = $ this-> secondsToTime($ time);
返回floatval($ distance / 1000);






以下是当源$ from = 'CBD'和$ to ='Sydney Int。机场'。


字符串(569){destination_addresses:[yes Optus悉尼
国际机场, Mascot NSW 2020,Australia],
origin_addresses:[Sydney NSW,Australia],rows:[{b $ belements:[{distance:{text 9.5 km,value:9482},
duration:{text:16分钟,value:930},status:OK}
]但是,当source $ from ='Sydney Int。时,输出会有很大的不同。#:/ / / / / / / / / / / / / / / /机场'和$至='CBD',如下所示。


$ b


字符串(572){destination_addresses:[Sydney NSW,澳大利亚],
origin_addresses:[是Optus悉尼国际机场,b $ b Airport Drive,Mascot NSW 2020,澳大利亚],行:[{elements
: {text:17.5 km,value:17513},duration
:{text:18分钟,value:1075},status :OK}]}],
status:OK}


郊区,我得到了9.5公里和17.5公里,这是相当不同的。任何人都可以建议吗?我使用Laravel框架在PHP中进行编程。

解决方案

这是我的代码,用于检查步骤。 b

  $ 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 [距离] [文本]< / H4>。
foreach($ val [steps] as $ v){
echo $ v [distance] [text]。 ''。 $ v [持续时间] [文本]。 ''。 $ 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 [距离] [文本]< / H4>。
foreach($ val [steps] as $ v){
echo $ v [distance] [text]。 ''。 $ v [持续时间] [文本]。 ''。 $ v [html_instructions]。 ''。 $ v [end_location] [lat]。 ','。 $ v [end_location] [lng]。 <峰; br>;
}
}

结果

  1.0公里1分钟在机场博士西部朝林克路往南-33.9311912,151.1638899 
0.4公里1分钟乘坐右转到Metroad 5 / Metroad 3 / Rockdale / Sydney / Sydney Southwest -33.9338978,151.1613411
0.9 km 1分钟继续前往Marsh街-33.937509,151.1533556
0.3 km 1分钟左转进入Metroad 5坡道Port Botany /悉尼-33.939696,151.1553088
1.8公里2分钟合并到M5 -33.9459699,151.1724068
11.4公里10分钟继续M1
部分收费公路
-33.871314,151.2179597
0.3 km 1分钟出口驶向城市北部
收费公路
-33.8685533,151.2180181
29 m 1分钟向约翰·杨·克雷斯爵士左转(麦格理街/城市路牌) -33.8683378,151.2178461
0.2公里1分钟左转,按照John John Young Cres的指示牌-33.867197,151.2163908
0.2公里1分钟轻轻左转到John Young Cres爵士队-33.8665953,151.2146068
0.2 km 1分钟继续进入莎士比亚Pl -33.8658241,151.2129679
0.1公里1分钟左转进入麦格理街-33.8665564,151.2124343
0.5公里2分钟右转进入亨特街-33.865537,151.2072971
0.2公里1分钟左转进入George St -33.8674649,151.207086



  1.7公里4分钟沿乔治街南侧向巴拉克街St -33.8828335,151.2041774 
0.3公里1分钟稍向左转上李街-33.8853847,151.2029028
1.2公里2分钟继续进入Regent St -33.8951109,151.1991316
0.7公里1分钟继续进入Botany Rd -33.9014863,151.2011191
0.1公里1分钟右转上McEvoy St -33.9017513,151.200088
0.5公里1分钟在第一个十字路口左转进入Wyndham St -33.905706,151.2019072
54 m 1分钟继续前往绿色广场-33.9061048,151.2022107
3.1公里4分钟右转进入O'Riordan St - 33.9306923,151.186595
0.2 km 1分钟右转Robey St -33.9312613,151.1842737
1.0公里1分钟右转到Qantas Dr -33.9259671,151.1785015
0.5公里1分钟继续到机场博士

您可以尝试使用方向而不是距离并指定航点。



单点航点

  $ waypoints ='-33.9306923,151.186595'

多个航点

  $ waypoints ='-33.9306923,151.186595 | 33.9312613,151.1842737'

用于API:

  destination = $ destination& waypoints = -`33.9306923,151.186595 | 33.9312613,151.1842737& sensor` 

简单的方法是将谷歌的路线


I am using google map distance matrix api to find distance and duration between a set of source and destination. I am trying to find distance between any two suburbs of Sydney, Australia. For a specific source and destination, I get different results when I alter source for destination and destination for source. My function in PHP is as below:

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);
        }
    }

Following is the response from api when source $from='CBD' and $to='Sydney Int. Airport'.

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" } "

However, output is quite different when source $from='Sydney Int. Airport' and $to='CBD', as below.

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" } "

For the same set of suburbs, I am getting 9.5km and 17.5km which is quite a difference. Could anyone suggest please? I am programming in PHP with Laravel framework.

解决方案

Here is my code to check the steps.

$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>';
  }
}

Results

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

You could try using directions rather than distance and specify waypoints.

Single Waypoint

$waypoints= '-33.9306923,151.186595'

Multiple Waypoints

$waypoints= '-33.9306923,151.186595|33.9312613,151.1842737'

Used in API:

destination=$destination&waypoints=-`33.9306923,151.186595|33.9312613,151.1842737&sensor`

The easy way would have been to Google the route

这篇关于Google距离矩阵为改变源和目的地提供了不同的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆