Created
April 12, 2017 14:09
-
-
Save sanchezg/be0c154444ef58f080722e5cfb21507f to your computer and use it in GitHub Desktop.
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
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