Google距离矩阵为更改的源和目标提供不同的距离

时间:2015-02-05 15:54:17

标签: php google-maps

我正在使用谷歌地图距离矩阵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);
        }
    }

以下是源码$来自''CBD'和$ to ='悉尼国际机场时来自api的回复。机场”。

  

string(569)“{”destination_addresses“:[”是的Optus Sydney   国际机场,机场大道,Mascot NSW 2020,澳大利亚“],   “origin_addresses”:[“Sydney NSW,Australia”],“rows”:[{   “elements”:[{“distance”:{“text”:“9.5 km”,“value”:9482},   “持续时间”:{“text”:“16分钟”,“值”:930},“状态”:“确定”}   ]},“status”:“OK”}“

然而,当源$ from ='Sydney Int时,输出完全不同。 Airport'和$ to ='CBD',如下所示。

  

string(572)“{”destination_addresses“:[”Sydney NSW,Australia“],   “origin_addresses”:[“是的悉尼国际机场,   Airport Drive,Mascot NSW 2020,Australia“],”rows“:[{”elements“   :[{“distance”:{“text”:“17.5 km”,“value”:17513},“duration”   :{“text”:“18分钟”,“值”:1075},“状态”:“OK”}]}],   “状态”:“确定”}“

对于同一组郊区,我得到了9.5公里和17.5公里,这是相当不同的。有人可以建议吗?我使用Laravel框架在PHP中编程。

3 个答案:

答案 0 :(得分:2)

以下是检查步骤的代码。

$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= '-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`

简单的方法就是谷歌的路线
enter image description here enter image description here

答案 1 :(得分:0)

您获得不同结果的原因是因为您正在使用Direction API的API调用而不是Distance Matrix API。

Direction API将为您提供当前场景中行程时间最短的路线。根据流量的不同,路由可能会发生变化,并且可以为同一组Origin和Destination设置不同的路由。

要获得相同的距离,请使用距离Matrix API调用,如下所示:

https://maps.googleapis.com/maps/api/distancematrix/output?parameters

希望这会有所帮助!!

答案 2 :(得分:-1)

您可以尝试从Google服务器检索JSON:

示例:

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Hyderabad+Telangana+%20India&destinations=Mumbai+Maharashtra+India

返回:

{
   "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'];