Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example usage of the googlemaps package to lookup origin / destination information using Google Maps API.
# You need to do a "pip install googlemaps" before running this.
# The input CSV file has an origin in Column A, and desinations in Column B
import csv
from googlemaps import GoogleMaps
from time import sleep
gmaps = GoogleMaps("your-super-long-API-key-from-Googles-API-Console")
stationReader = csv.reader(open('StationList.csv', 'rb'), delimiter=',')
timeWriter = csv.writer(open('Output.csv', 'wb'), delimiter=',', quotechar='|')
sta_from_list = []
sta_to_list = []
for row in stationReader:
if len(row[0]) > 3:
if len(row[1]) > 3:
timeWriter.writerow(["Station From", "Station To", "Meters", "Time in Sec", "Human Time"])
for sta_from in sta_from_list:
for sta_to in sta_to_list:
directions = gmaps.directions(sta_from+", New South Wales, Australia", sta_to+", New South Wales, Australia")
meters = directions['Directions']['Distance']['meters']
secs = directions['Directions']['Duration']['seconds']
human_time = directions['Directions']['Duration']['html']
if directions['Status']['code'] == 200:
timeWriter.writerow([sta_from, sta_to, meters, secs, human_time])
print "Lookup from %s to %s succeeded, but no results found." % (sta_from, sta_to)
print "Error from %s to %s" % (sta_from, sta_to)
Sydenham Station Marrickville Station
Dulwich Hill Station
Hurlstone Park Station
Canterbury Station
Campsie Station
Bankstown Station
Berala Station
Sefton Station
Leightonfield Station
Ashfield Station
Homebush Station
Flemington Station
Lidcombe Station
Granville Station
Clyde Station
Auburn Station
Merrylands Station
Yennora Station

This comment has been minimized.

Copy link

@diouck diouck commented Jun 8, 2015

I'm interested in your application. Will you explain to me why when I exécutele script as I cca error "error from .. to .."
Thank you for your understanding
PS: I put my google key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment