I have about 100 coordinates of places and would like to find the distance matrix of each of the element to other 99 in the list.
I can create all possible combination of origin, destination pairs and pass each of it to an API call and i am able to compute distance and duration as below.
But given than we can pass upto 25 destinations in one single API call, how can I do this with reduced number of api calls for the 100 locations I have ?
def get_gmaps_distance(row):
result = gmaps.distance_matrix(row[‘coordinates_origin’], row[‘coordinates_destin’], mode=’driving’,departure_time=’now’)
status = result[‘rows’][0][‘elements’][0][‘status’]
if status == “OK”:
KM = int(result[‘rows’][0][‘elements’][0][‘distance’][‘value’] / 1000)
#kmtxt = result[‘rows’][0][‘elements’][0][‘distance’][‘text’]
MIN = int(result[‘rows’][0][‘elements’][0][‘duration’][‘value’] / 60)
#txt = result[‘rows’][0][‘elements’][0][‘duration’][‘text’]
else:
#kmtxt = 0
KM = 0
MIN = 0
#txt = 0
return (KM,MIN)
df_gmaps[[‘distance’,’duration’]] = df_gmaps.apply(get_gmaps_distance, axis=1, result_type=’expand’)
This currently works but every pair of origin destination is sent as an api call.
I want to reduce the number of call happening.
Kirran Raj Mudhra R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.