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 (deprec
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']
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.
Check out this link: https://developers.google.com/maps/documentation/distancematrix/#unit_systems
Read the part about "Optional parameters." essentially, you add the parameter into your request in your url. So if you wanted biking, it would be "mode=bicycling." Check out the example towards the bottom of the link and play around with some parameters. Good Luck!
Updated the Accepted Answer to include the API Key and use a string for the addresses.
import simplejson, urllib
KEY = "xxxxxxxxxxxxxx"
orig = "Street Address 1"
dest = "Street Address 2"
url = "https://maps.googleapis.com/maps/api/distancematrix/json?key={0}&origins={1}&destinations={2}&mode=driving&language=en-EN&sensor=false".format(KEY,str(orig),str(dest))
result= simplejson.load(urllib.urlopen(url))
#print(result)
driving_time = result['rows'][0]['elements'][0]['duration']['text']
print(driving_time)