Skip to content

Instantly share code, notes, and snippets.

@mccbala
Forked from bradmontgomery/geo.py
Created July 19, 2017 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccbala/00afcc5aa69caae6f484de4f33625417 to your computer and use it in GitHub Desktop.
Save mccbala/00afcc5aa69caae6f484de4f33625417 to your computer and use it in GitHub Desktop.
Example of Reverse Geocoding in python with Google Maps api
import requests
def example():
# grab some lat/long coords from wherever. For this example,
# I just opened a javascript console in the browser and ran:
#
# navigator.geolocation.getCurrentPosition(function(p) {
# console.log(p);
# })
#
latitude = 35.1330343
longitude = -90.0625056
# Did the geocoding request comes from a device with a
# location sensor? Must be either true or false.
sensor = 'true'
# Hit Google's reverse geocoder directly
# NOTE: I *think* their terms state that you're supposed to
# use google maps if you use their api for anything.
base = "http://maps.googleapis.com/maps/api/geocode/json?"
params = "latlng={lat},{lon}&sensor={sen}".format(
lat=latitude,
lon=longitude,
sen=sensor
)
url = "{base}{params}".format(base=base, params=params)
response = requests.get(url)
return response.json['results'][0]['formatted_address']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment