-3

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 ;
}
xomena
  • 31,125
  • 6
  • 88
  • 117
  • Possible duplicate of [Calculate distance between two points in google maps V3](https://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3) – TarangP Feb 10 '18 at 07:29
  • 1
    What are you asking for? How to calculate DISTANCE? or how to calculate DRIVING DISTANCE? – MrUpsidown Feb 10 '18 at 11:15

2 Answers2

1

Try this code for calculating driving distance and time.

Code :

<?php

function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response, true);

    $status = "ERROR"; 
    $dist   = 0;
    $time   = 0;
    if($response_a['rows'][0]['elements'][0]['status'] === 'OK') {
        $dist   = $response_a['rows'][0]['elements'][0]['distance']['text'];
        $time   = $response_a['rows'][0]['elements'][0]['duration']['text'];
        $status = "OK";
    }

    return array('status' => $status, 'distance' => $dist, 'time' => $time);
}

Test : 1. Distance between Mumbai and Delhi 2. Random invalid lat lan.

$dist = GetDrivingDistance(19.228825, 28.644800,72.854118, 77.216721);
var_dump($dist);
// OUTPUT : array(3) { ["status"]=> string(2) "OK" ["distance"]=> string(9) "1 386 km" ["time"]=> string(15) "22 godz. 35 min" }
$dist = GetDrivingDistance(888.228825, 28.644800,72.854118, 77.216721);
var_dump($dist);
// OUTPUT : array(3) { ["status"]=> string(5) "ERROR" ["distance"]=> int(0) ["time"]=> int(0) }

Tip : You can get more details by var_dump($response_a) inside functions.

PS : Make sure you are giving proper LAT LAN.

Touheed Khan
  • 2,149
  • 16
  • 26
1

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

Ari Singh
  • 1,228
  • 7
  • 12