Created
September 22, 2016 02:36
-
-
Save Kalaborative/2122909c2cba00d4d5e7da30a4121913 to your computer and use it in GitHub Desktop.
Sample Google Directions API Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First we import our googlemaps module | |
# so we can communicate with the API. | |
import googlemaps | |
# Set your API key to a variable | |
key = "AIzaSyCshp5LS_bmxQR6-vqGP6f5apFpw9vmVrQ" | |
# connect to the Google Maps API using that key | |
g = googlemaps.Client(key=key) | |
# Define our beginning and ending locations | |
startpl = "San Francisco" | |
stoppl = "Los Angeles" | |
# Using the directions method, we use our variables as the parameters. | |
result = g.directions(origin=startpl, destination=stoppl) | |
# Because the result is a LIST, we want to remove it | |
# so that we are working with a DICT instead. | |
maps = result[0] | |
# Use a for-loop to iterate through the dict | |
with open('trip.html','wb') as code: # We can save the code to an HTML file | |
for gmap in maps['legs']: | |
for step in gmap['steps']: | |
code.write(step['html_instructions']) | |
code.close() | |
# Make sure to close your file so you can save it! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment