Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Google Maps Driving Time

I need to get driving time between two sets of coordinates using Python. The only wrappers for the Google Maps API I have been able to find either use Google Maps API V2 (deprecated) or do not have the functionality to provide driving time. I'm using this in a local application and do not want to be bound to using JavaScript which is what the Google Maps API V3 is available in.

like image 513
tsspires Avatar asked Jun 24 '13 03:06

tsspires


People also ask

How do I get travel time on google maps?

From the configuration menu select: Devices & Services. In the bottom right, click on the Add Integration button. From the list, search and select “Google Maps Travel Time”. Follow the instruction on screen to complete the set up.

Can you use google maps API with Python?

The Java Client, Python Client, Go Client and Node. js Client for Google Maps Services enable you to work with Google Maps web services on your server. They wrap the functionality of the following APIs: Address Validation API.

How do I get google maps data in Python?

Installation: set up python for this exercise. Get a Google Map API key : this is necessary to be able to display google maps in your applications. How to prepare your data for geographical display : we will use pandas to read the dataset from file, and have a first look at the data before display.


2 Answers

Using URL requests to the Google Distance Matrix API and a json interpreter you can do this:

import simplejson, urllib
orig_coord = orig_lat, orig_lng
dest_coord = dest_lat, dest_lng
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(str(orig_coord),str(dest_coord))
result= simplejson.load(urllib.urlopen(url))
driving_time = result['rows'][0]['elements'][0]['duration']['value']
like image 114
tsspires Avatar answered Sep 28 '22 10:09

tsspires


import googlemaps
from datetime import datetime

gmaps = googlemaps.Client(key='YOUR KEY')


now = datetime.now()
directions_result = gmaps.directions("18.997739, 72.841280",
                                     "18.880253, 72.945137",
                                     mode="driving",
                                     avoid="ferries",
                                     departure_time=now
                                    )

print(directions_result[0]['legs'][0]['distance']['text'])
print(directions_result[0]['legs'][0]['duration']['text'])

This is been taken from here And alternatively you can change the parameters accordingly.

like image 39
Domnick Avatar answered Sep 28 '22 09:09

Domnick