0

I am trying to calculate the distances between points by using gmaps.distance_matrix() with a google api. The code outputs unreasonable results, but I can't find where the error is.

Here is my code:

gmaps = googlemaps.Client(key=API_key)
origins = ['41.844775, -87.626719', '41.614779, -87.55133599999999', '41.567222, -87.550503', '41.800635, -87.604568', '41.69154, -87.62171', '41.894244, -87.62284100000001', '42.010228000000005, -72.840536', '42.010228000000005, -72.840536', '42.010228000000005, -72.840536', '42.010228000000005, -72.840536', '47.617177000000005, -122.208579']

destinations = ['41.614779, -87.55133599999999', '41.567222, -87.550503', '41.800635, -87.604568', '41.69154, -87.62171', '41.894244, -87.62284100000001', '42.010228000000005, -72.840536', '42.010228000000005, -72.840536', '42.010228000000005, -72.840536', '42.010228000000005, -72.840536', '47.617177000000005, -122.208579', '41.894244, -87.62284100000001']

actual_distance_testing = []

for origin, destination in zip(origins, destinations):
        result = gmaps.distance_matrix(origin, destinations, mode='driving')["rows"][0]["elements"][0]["distance"]["value"]  
        result = result/1000
        actual_distance_testing.append(result)

print(actual_distance_testing)

This is the output I got from the code above:

[31.716, 0.0, 7.257, 28.524, 13.981, 39.688, 1416.856, 1416.856, 1416.856, 1416.856, 3347.006]

The first value is correct, but the rest of them are not accurate. This is my expected and correct result:

[31.716, 7.247, 33.71, 14.976, 25.557, .....]

For more information: I am calculating the distance in km. For example, the distance between '41.844775, -87.626719' and '41.614779, -87.55133599999999' is 31.716 km.

I tried to calculate the second value only, and the output is correct if I do the calculation separately as followed:

o = (41.614779, -87.55133599999999); d = (41.567222, -87.550503)
result = gmaps.distance_matrix(o, d, mode='driving')["rows"][0]["elements"][0]["distance"]["value"]/1000

print(result)

7.247
Wu Kris
  • 77
  • 8
  • It is very difficult to tell from the above code provided, like what is the response of the api, what parameters does the api take. Have you checked Haversine distance, u can use it if not much accuracy is needed. – Epsi95 Jun 29 '21 at 17:00
  • I want to minimize the error, so I have to use the distance matrix from google. Something weird is that the output is accurate if I select two specific coordinates instead of a list of coordinates and run the same code again. Can you please refresh the page? I added more information at the bottom. Thank you – Wu Kris Jun 29 '21 at 17:11
  • 1
    in ur code origin and destinations are stings not list or tuple – Epsi95 Jun 29 '21 at 17:20
  • 1
    can u try `result = gmaps.distance_matrix(ast.literal_eval(origin), ast.literal_eval(destinations), mode='driving')["rows"][0]["elements"][0]["distance"]["value"]` do `import ast` – Epsi95 Jun 29 '21 at 17:23
  • The code works now. Appreciate the help! – Wu Kris Jun 29 '21 at 18:20

1 Answers1

0

here I think you should look at the full response to understand how Google API provides the requested query. I recommend for you trace the response first. here in this presented example below the result['rows'][0]['elements'] is a JSON object that has two keys one for the distance and the other for the duration. The first element is in each object text format and the second element, value is in number format.

In the distance object, the text's value contains the distance in kilometer unit, and the value of the value key has the distance in meter unit in number format.

In the duration object, the text's value contains the duration in minutes unit, and the value of the value key has the duration in seconds unit in number format.

result['rows'][0]['elements']= [{'distance': {'text': '31.7 km', 'value': 31715}, 'duration': {'text': '26 mins', 'value': 1568}, 'status': 'OK'}]