Skip to content

Instantly share code, notes, and snippets.

@jakeemerson
Created July 6, 2017 15:29
Show Gist options
  • Save jakeemerson/78b85748d5ee190da95150b8888454a8 to your computer and use it in GitHub Desktop.
Save jakeemerson/78b85748d5ee190da95150b8888454a8 to your computer and use it in GitHub Desktop.
Using the google geocode api
import pandas as pd
import re
import urllib2
import pprint
import json
def geocode(place):
add = urllib2.quote(place)
geocode_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false&region=us" % add
req = urllib2.urlopen(geocode_url)
jsonResponse = json.loads(req.read())
# pprint.pprint(jsonResponse)
try:
lat = jsonResponse['results'][0]['geometry']['location']['lat']
lon = jsonResponse['results'][0]['geometry']['location']['lng']
return lat, lon
except IndexError:
return '', ''
print geocode('123 Main St. Presque Isle ME 04769')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment