How to Calculate Distance Matrix using Google Maps in Python | Python | Google Maps
HTML-код
- Опубликовано: 13 сен 2024
- * Calculating point-to-point distance and travel time using google maps API in Python.
* A demo for solving a Travelling Salesman Problem (TSP) with the distance matrix.
Links:
Google Maps Platform: mapsplatform.g...
Direction API Documentaton: developers.goo...
Thanks. This tutorial has been super helpful. I keep getting the following index error when trying to iterate through the rows to get the distance and travel time:
in ()
3 direction_result = gmaps_client.directions(row['from_lat_long'], row['to_lat_long'],
4 mode = "driving", avoid = "ferries", departure_time = start_time)
----> 5 df.loc[index, 'distance'] = direction_result[0]['legs'][0]['distance']['value']
6 df.loc[index, 'travel_time'] = direction_result[0]['legs'][0]['duration']['value']
7 df.loc[index, 'distance_text'] = direction_result[0]['legs'][0]['distance']['text']
IndexError: list index out of range
Do you have any recommendations?
Hi Paul, currently I get stuck with this idea: I have 1 depot and 9 customer (and I also have a dynamic customer list that will occur in each iteration). So can I ask you about the way to arrange my data files for the problem? In your video, I see that you set the "from_node" and "to_node" columns, so I think it will be hard for me to follow this since when there is a dynamic customer, their lat and long will be insert into the data file and create a new distance matrix of all nodes. So I hope to receive your response soon. Thank you very much
Thanks for the video. When you are calculating distance matrix, does google charge you based on number of API calls or on the basis of number of origins X number of destinations?
Basically, each origin-destination pair is considered as one call. So its number of origins X number of destinations unless you prune efficiently.
hi. thank you for your explanation about the distance matrix calculation. that's so helpful for me to understand. if i may ask, how can u get thos latlong data? can we use the geocode in gmaps api? please kindly enlighten me. thank you!
Yes, you can use geocode in gmaps API. You can refer to ruclips.net/video/mXGyH8_FcMQ/видео.html
In "source" and "destination", you can just enter the location names instead of the lat longs.
Also, you can use gmaps "geocode" API to get the lat, longs. E.g.
"geocode_result = gmaps.geocode("Kolkata")
print(geocode_result[0]['geometry']['location'] ['lat'], geocode_result[0]['geometry']['location']['lng'])"
Hope this helps.
Hi, thank you for your videl. If I may ask, there are some option parameters in distance matrix api, such as we can set up trafifc_model as 'best_guess', 'pessimistic', 'optimistic'. can we set up these parameters?
yes, you can setup these parameters as follows:
direction_result = gmaps_client.directions(source, destination
, mode="driving", avoid="ferries", departure_time=now, transit_mode = 'bus', traffic_model='optimistic')
Hi, thanks your video, it is usefull to me could you share your source code?