Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Created April 16, 2013 16:42
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bradmontgomery/5397472 to your computer and use it in GitHub Desktop.
Save bradmontgomery/5397472 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']
@cesarrodrig
Copy link

There are some parethesis missing: response.json()

@mac389
Copy link

mac389 commented Aug 25, 2013

Nice example, very useful.

@eww125
Copy link

eww125 commented Oct 24, 2016

I could not get this working

@alicezyk
Copy link

alicezyk commented Dec 4, 2016

It works after changing the return line to return response.json()['results'][0]['formatted_address']

Copy link

ghost commented Oct 4, 2017

Thanks! Very useful.

Sometimes though google does not respond and it breaks the whole thing.
I modified the code to account for that, adding in the end of the function

while True:
    response = requests.get(url).json()
    if(response['status'] == 'OK'):
        result = response['results'][0]
        break

return result['address_components']

@sjmunoz
Copy link

sjmunoz commented Nov 22, 2017

How do I add my API key to this?

@11996
Copy link

11996 commented May 10, 2018

How do I add my API key to this?
also, it reaching its maximum limit

@vamsikeka
Copy link

How to print the results in my console?

@konradbachusz-zz
Copy link

konradbachusz-zz commented Nov 13, 2018

return response.json()['results'][0]['formatted_address']

I changed the return statement and got this error:

IndexError                                Traceback (most recent call last)
<ipython-input-3-48cbd428257a> in <module>()
     29     response = requests.get(url)
     30     return response.json()['results'][0]['formatted_address']
---> 31 example()

<ipython-input-3-48cbd428257a> in example()
     28     url = "{base}{params}".format(base=base, params=params)
     29     response = requests.get(url)
---> 30     return response.json()['results'][0]['formatted_address']
     31 example()

IndexError: list index out of range


@hlbglobal
Copy link

I am really looking for how to write python code that will read a .csv file that just contains latitudes and longitudes for locations in California. I would like for it to read in each row and convert the lat/long to a county in California. This seems like it would be much more simple than these examples however I don't know how to do this. Also, would I use Python 2.7 for GIS python apps?

Thanks very much for any help someone could provide!

Barbara DiLucchio

@hmdmph
Copy link

hmdmph commented Aug 15, 2020

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment