Skip to content

Instantly share code, notes, and snippets.

@sanchezg
Created April 12, 2017 14:09
Show Gist options
  • Save sanchezg/be0c154444ef58f080722e5cfb21507f to your computer and use it in GitHub Desktop.
Save sanchezg/be0c154444ef58f080722e5cfb21507f to your computer and use it in GitHub Desktop.
import requests
from urllib.parse import quote_plus, urlencode
def geocode_from_address(address):
"""
Returns a tuple of coordinates (lat, long) obtained from the address passed
as argument. The Location is obtained using the Google APIs v3.
"""
address = quote_plus(address)
maps_api_url = "?".join([
"http://maps.googleapis.com/maps/api/geocode/json",
urlencode({"address": address, "sensor": False})
])
response = requests.get(maps_api_url)
if response.status_code in [200, '200']:
data = response.json()
lat = data['results'][0]['geometry']['location']['lat']
lng = data['results'][0]['geometry']['location']['lng']
return (lat, lng)
return (None, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment