1

Google Map API..

I want shortest path between two addresses.

Below is my code for it.

Its working fine but it returns unnecessary information thats why url request takes too much time to get response.

$source = "ClinRad Diagnostics & Research Centre, 725, CMH Road, Near CMH Hospital, Indiranagar 1st stage, Bengaluru, Karnataka 560038"; 

$destination = "HENNUR POLICE STATION, Balachandra Layout, Babusabpalya, Hennur Gardens, Bengaluru, Karnataka 560043";

$url ='http://maps.googleapis.com/maps/api/directions/json?origin='.urlencode($source).'&destination='.urlencode($destination).'&alternatives=true&sensor=false&mode=driving';

$data = file_get_contents($url);
$response = json_decode($data);
echo "<pre>";
print_r($response);

Reponse array details: https://jsfiddle.net/mbb5Lu32/

I want only km value for the shortest path.. Rest of the things are waste for me..

How can I get that specific information using google map API?

Sarfaraz bheda
  • 457
  • 1
  • 6
  • 23

1 Answers1

0

You could use the Google distancematrix API for that. It returns only the distance and the duration for the shortest route.

request:

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Washington,DC&destinations=New+York+City,NY&key=

answer:

{
   "destination_addresses" : [ "New York City, New York, USA" ],
   "origin_addresses" : [ "Washington, D.C., District of Columbia, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "362 km",
                  "value" : 361721
               },
               "duration" : {
                  "text" : "3 Stunden, 47 Minuten",
                  "value" : 13640
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
J. Bergmann
  • 440
  • 3
  • 13
  • please compare this results with https://www.google.co.in/maps/ it will show wrong value – Sarfaraz bheda May 08 '17 at 11:46
  • Ok, this is the correct value for the distance on the road network. If you want the shortest distance by walking or via transit routes, you can set the travel mode in the api request. – J. Bergmann May 08 '17 at 11:57
  • Try to compare this two address: $source = "M G Rd, KG Halli, Shanthala Nagar, Ashok Nagar, Bengaluru, Karnataka 560001"; $destination = "36th Cross Rd, BDA Layout, Tilak Nagar, Jayanagar, Bengaluru, Karnataka 560041"; – Sarfaraz bheda May 08 '17 at 11:59
  • the values differ by 300m – J. Bergmann May 08 '17 at 12:07