0
votes

Some buddy help me?

I have tried to get distance between two address using google map but when i have change source and destination vice-versa then google map giving me diffrent response.

First Address: 1 Airport Drive, Oakland, CA 94621, USA Second Address: 44085 Laurel Canyon Way, Fremont, CA 94539, USA

http://maps.googleapis.com/maps/api/directions/json?origin=Oakland+International+Airport+%28OAK%29,+1+Airport+Drive,+Oakland,+CA+94621,+USA&destination=44085+Laurel+Canyon+Way,+Fremont,+CA,+United%20States&sensor=false

Response:

   "routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 37.7311528,
           "lng" : -121.932651
        },
        "southwest" : {
           "lat" : 37.5066302,
           "lng" : -122.2137008
        }
     },
     "copyrights" : "Map data ©2015 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "24.6 mi",
              "value" : 39536
           },
           "duration" : {
              "text" : "33 mins",
              "value" : 1982
           },

http://maps.googleapis.com/maps/api/directions/json?origin=44085+Laurel+Canyon+Way,+Fremont,+CA,+United%20States&destination=Oakland+International+Airport+%28OAK%29,+1+Airport+Drive,+Oakland,+CA+94621,+USA&sensor=false

Response:

"routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 37.7325325,
           "lng" : -121.932675
        },
        "southwest" : {
           "lat" : 37.5081351,
           "lng" : -122.2137008
        }
     },
     "copyrights" : "Map data ©2015 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "24.2 mi",
              "value" : 38993
           },
           "duration" : {
              "text" : "31 mins",
              "value" : 1839
           },

In given response object distance->text and distance->value both response diffrent.

I can't understand if same address so why this happen.

3

3 Answers

0
votes

Google map does not find distances using the formula distance between two points, instead they calculate distance based on routes.

There are various routes to reach the destination. Hence when you check the vice versa, it could have taken an alternative route.

0
votes

If you look at Google Maps (not API) you see that routes are not the same.

It is often the case around airports.

0
votes

Google's algorithm for Distance works around the routes. So for the same origin and destination the distance and duration would differ. It also takes the traffic into consideration and gives you the best optimized route so you have no control on the distance and duration the API is going to return for same origin and destination if they are flipped.

One work around would be to specify waypoints falling in between each route from origin to destination and destination to origin so that there is no change in the route and would possibly return the same distance in both directions.