14

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.

tsspires
  • 591
  • 1
  • 5
  • 10
  • Isn't this against the Google Maps TOS? I don't think you can just get driving time unless you are also displaying Google Maps to the user. (https://developers.google.com/maps/terms, section 10.1 g, "No Use of Content without a Google Map") – element119 Jun 24 '13 at 04:44
  • I never said I wasn't going to display a map, even though that's not what I need. If I wasn't what is an available alternative? Since I have no problem doing so, how can I solve my issue of finding something to work with python? Although I am currently pursuing Google Distance Matrix as a solution. – tsspires Jun 24 '13 at 05:23
  • Because the API v3 is JS-only, any functionality must run through a web browser.. However, there are projects like pygmaps (https://code.google.com/p/pygmaps/) and py-googlemaps (http://py-googlemaps.sourceforge.net/) that act as Python wrappers for API operations, while running JS in the background. – element119 Jun 24 '13 at 13:59
  • Thanks. But pygmaps provides mapping functionality as far I can tell, without actually returning the data by itself as well. And I believe py-googlemaps I tried and is a wrapper for V2 which they arent providing api keys for anymore. My solution ended up being to use Google Distance Matrix by HTTP request and a JSON interpreter. – tsspires Jun 24 '13 at 20:13

4 Answers4

33

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']
tsspires
  • 591
  • 1
  • 5
  • 10
19
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.

Domnick
  • 509
  • 8
  • 25
3

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)

Frak
  • 832
  • 1
  • 11
  • 32
  • Hi, Any idea, how can this be iterated through the dataframe with 100's of records? – KApril Nov 12 '21 at 11:03
  • Does this take current traffic conditions into account because I've been running it for a day now where I get the driving time every 15 minutes and it has been the exact same time every time. If it doesn't consider current traffic conditions, how can I get it to do so? – Scott Mar 12 '23 at 00:22
2

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!