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.