I want to use Googles distance matrix API to measure the road distance between multiple addresses and a single address. I have a list of origin Latitudes and Longitudes (list_o), and destination Latitudes and longitudes (list_d). My Api key is set up.
list_o
[(51.268369, -1.361543),
(51.368369, -1.361043),
(51.062992, -1.250635),
(51.910508, -1.314208),
(51.233251, -1.551402)]
list_d
[(53.423451, -1.462141),
(53.483451, -1.172141),
(51.423451, -1.262141),
(52.623451, -1.246211),
(52.323451, -1.462141)]
if I pass in the lists of addresses
result = gmaps.distance_matrix(list_o, list_d, mode='walking')["rows"][0]["elements"][0]["distance"]["value"]
I only get a single result, eg 34000 metres = 34km , which looks correct. I have tried appending the result to a list and it still only comes up with one result.
With
gmaps.distance_matrix(list_o, list_d, mode='walking')["rows"][0]["elements"]
I am getting 10 values (the full size of the df, above I only showed the first 5).
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'},
{'distance': {'text': '34.0 km', 'value': 34014},
'duration': {'text': '7 hours 6 mins', 'value': 25554},
'status': 'OK'}]
However they are all the same value.
CodePudding user response:
I believe what you want to do is using:
list_o
[(51.668369, -1.361043),
(51.668369, -1.361043),
(51.662992, -1.270635),
(51.610508, -1.314208),
(51.733251, -1.351402)]
and:
list_d
[(52.423451, -1.462141)]
And then call:
results = gmaps.distance_matrix(list_o, list_d, mode='walking')
The results you are looking for are:
# Distance between first origin and destination.
# I.e.: distance (51.668369, -1.361043) -> (52.423451, -1.462141)
results['rows'][0]['elements'][0]
#... and so on
In other words, results['rows'][i]['elements'][0]
is the distance of location i
to the destination.
gmaps.distance_matrix
calculates the distance between the origins and each of the destinations.