Aakash Singh
Aakash Singh

Reputation: 1062

Google map show different distance between two dastination

I am creating a window form application, which calculate the distance between two locations by their Lat-Long using google API. This works fine, But i notice that some time api give different result (distance) for same locations.

Example :-

Location-A => Mumbai (19.075983 , 72.877655)

Location-B => Delhi (28.704060 , 77.102493)

When i calculate the distance from location-A To location-B

decimal Distance = EmployeeTaskWorkLogManager.getDistance(string.Concat(19.075983, ", ", 72.877655), string.Concat(28.704060, ", ", 77.102493));

This shows Distance = 1423.297

And when i calculate the distance from location-B To location-A

decimal Distance = EmployeeTaskWorkLogManager.getDistance(string.Concat(28.704060, ", ", 77.102493), string.Concat(19.075983, ", ", 72.877655));

This shows Distance = 1415.239

This the code that i am using to calculate distance between two locations,

 public static decimal getDistance(string origin, string destination)
        {
            decimal num;
            Thread.Sleep(1000);
            decimal num1 = new decimal(0);
            string[] strArrays = new string[] { "https://maps.googleapis.com/maps/api/distancematrix/json?origins=", origin, "&destinations=", destination, "&key=****" };
            string str = string.Concat(strArrays);
            JObject jObjects = JObject.Parse(EmployeeTaskWorkLogManager.fileGetContents(str));
            try
            {
                num1 = (decimal)jObjects.SelectToken("rows[0].elements[0].distance.value");
                num = num1 / new decimal(1000);
            }
            catch
            {
                num = num1;
            }
            return num;
        }

I Checked each line of my code, but unable to find why this difference has occurs.

Upvotes: 1

Views: 1373

Answers (1)

Tian van Heerden
Tian van Heerden

Reputation: 639

The Google Maps Distance Matrix API does not calculate a straight-line or big-circle distance between two points. Instead it returns the distance according to the mode of travel (which you haven't specified in your API call, so it defaults to driving). Think about it: there is no way that you will travel the exact distance in both directions, and the difference will become more marked the longer the journey and the more route changes it requires. It will depend on freeway off ramps (not available at particular intersections in both directions), one way streets, etc etc.

Therefore, the result is as expected.

Source: Google Maps Distance Matrix API

Upvotes: 3

Related Questions