Sangam Uprety
Sangam Uprety

Reputation: 1482

Google distance matrix gives different distance for altered source and destination

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.

Upvotes: 1

Views: 4208

Answers (3)

imtaher
imtaher

Reputation: 438

You can attempt to retrieve a JSON from the google server:

Example:

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

Then, use any information as needed, such as:

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

Upvotes: -2

Misunderstood
Misunderstood

Reputation: 5663

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
enter image description here enter image description here

Upvotes: 2

AniV
AniV

Reputation: 4037

The reason you are getting different results is because you are using the API call for Direction API and not the Distance Matrix API.

Direction API will give you the route with the shortest travel time in the current scenario. Depending on the traffic the routes might change and can have different routes for the same set of Origin and Destination.

To get the same distance use a distance Matrix API call which looks like this:

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

Hope this would Help!!

Upvotes: -1

Related Questions