When running this code it did not give any value.
But when replace lat lan value with (37.910689 ,-97.250977),(16.110560, -94.174805)
then it returns result in meters. It shows blank output when passed any other lat lan value then the USA address.
function location(){
$url="https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=28.666585,-77.229059&destinations=28.667053%2C-77.219898&key=Api key";
$json_string = $url;
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, TRUE); // Set second argument as TRUE
//print_r($obj['rows']); // Now this will works!
$prn=$obj['rows'];
foreach($prn as $row)
{
$ele=$row['elements'];
foreach($ele as $k)
{
$dis=$k['distance'];
foreach($dis as $l=> $lvalue){
$meter= $lvalue ;
}
}
}
return $meter ;
}
The two points you specified in your code are in the Atlantic ocean. You can verify by entering 28.667053,-77.219898 in search box at https://www.google.com. Google responds with ZERO_RESULTS, because it cannot calculate the distance between 2 points in ocean.
UPDATE: Note that what the google map api gives you is NOT the aerial or the shortest distance between 2 points or "as the crow flies" distance. It is the driving distance or walking distance. So it has to know about the 2 points before it can give you a distance. If it does not know the 2 points or cannot calculate the route to the 2 points it will not give results. https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=20.254916,76.715059&destinations=20.3127202,76.6839331 - these points are close but driving / walking distance is large. Goole maps gives the driving / walking distance.
{
"destination_addresses": [
"28.667053,-77.219898"
],
"origin_addresses": [
"28.666585,-77.229059"
],
"rows": [
{
"elements": [
{
"status": "ZERO_RESULTS"
}
]
}
],
"status": "OK"
}
Google responds with correct distance for 2 points within US e.g https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=42.025819,-88.0854497&destinations=41.910088,-87.6929887