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