Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Python requests and the Google Maps Geocoding API
# Using Python requests and the Google Maps Geocoding API.
#
# References:
#
# * http://docs.python-requests.org/en/latest/
# * https://developers.google.com/maps/
import requests
GOOGLE_MAPS_API_URL = 'http://maps.googleapis.com/maps/api/geocode/json'
params = {
'address': '221B Baker Street, London, United Kingdom',
'sensor': 'false',
'region': 'uk'
}
# Do the request and get the response data
req = requests.get(GOOGLE_MAPS_API_URL, params=params)
res = req.json()
# Use the first result
result = res['results'][0]
geodata = dict()
geodata['lat'] = result['geometry']['location']['lat']
geodata['lng'] = result['geometry']['location']['lng']
geodata['address'] = result['formatted_address']
print('{address}. (lat, lng) = ({lat}, {lng})'.format(**geodata))
# 221B Baker Street, London, Greater London NW1 6XE, UK. (lat, lng) = (51.5237038, -0.1585531)
@RyanKor
Copy link

RyanKor commented Dec 22, 2019

Hi all,
I would like to use the code above (actually I need to get the address details from the zipcode), I know my company owns a contract to use google API, but I get this error:

You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account

May someone tell me how can I authenticate with Python?
What are the statements to be used?

thanks!

I have the same problem as you. I solved this prob by issuing google map 'geocoding api'.

First, you should signup google cloud platform and make a new project in the dashboard.

Second, go to the library and search 'geocoding API', then enable the API.

Lastly, copy the key and paste it in your codes.

then you will solve the problem.

@gecko2019
Copy link

gecko2019 commented Aug 10, 2021

Just tried RyanKor's solution above (August 2021) ; it worked!
I've re-written his instructions (on adding the Geocoding API to your Google Cloud Platform project, then getting that's project's API key)

To get a google API key:

  1. go to Google Cloud Platform ( console.cloud.google.com )
  2. Sign up if you haven't already
  3. make a new project in the dashboard.
  4. in the top centre search bar ('Search products and resources'), search for 'geocoding API'
    Note it's not free, but the cost is very minimal: $6.29 per 1K requests.
  5. Hit the 'Enable' button. This will add that API to your project
  6. Last, find the API key (in your project, under the API menu on the left, select Credentials, then all your API keys will show up. Scroll right to the Key column, and use copy link to copy your API key.
  7. Paste the API key into 'params' for your request.

Here is what a key looks like (note this is not an actual working key; you'll have to get your own):
XIzaCymizrfTr77GlKxYb7BhqQmo5qImd3ZFZto

So params looks like:
params = {'address': 'Mountain View, CA',
'sensor': 'false', 'key': 'XIzaCymizrfTr77GlKxYb7BhqQmo5qImd3ZFZto'}

(There's also an optional 'region' key that I didn't include here)

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