Skip to content

Instantly share code, notes, and snippets.

@pnavarrc pnavarrc/geocode.py

Last active Jun 21, 2020
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)
@bishvesh

This comment has been minimized.

Copy link

bishvesh commented Nov 1, 2017

I have a csv which has 7500 values, I rewrote the code, but the script fails after 50-60 iterations, can u please help me out here

import requests
import csv
GOOGLE_MAPS_API_URL = 'http://maps.googleapis.com/maps/api/geocode/json'
with open('zipcodes.csv', 'rb') as f:
reader = csv.reader(f)
yourlist = list(reader)
for val in yourlist:
params = {
'address': val
#'sensor': false
#'region': us
}
req = requests.get(GOOGLE_MAPS_API_URL, params=params)
res =req.json()
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))

@pegajardo

This comment has been minimized.

Copy link

pegajardo commented Nov 25, 2017

Maybe it´s cause there is a limit of request in a period of time, have a look at your permisions :D

@BBijaya

This comment has been minimized.

Copy link

BBijaya commented Apr 1, 2018

yeah it's because of limitations, unless paid for the limit is 50 request per second and 2500 request per day. for further details visit this link https://developers.google.com/maps/documentation/geocoding/usage-limits

@mfran2002

This comment has been minimized.

Copy link

mfran2002 commented Mar 28, 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!

@taichoup

This comment has been minimized.

Copy link

taichoup commented Jun 5, 2019

you can add 'key': 'YOUR_KEY' to the params dictionary

@RyanKor

This comment has been minimized.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.