Skip to content

Instantly share code, notes, and snippets.

@KanzakiRanko1
Last active March 23, 2020 15:13
Show Gist options
  • Select an option

  • Save KanzakiRanko1/69730f8ea50878697ca987cab352a824 to your computer and use it in GitHub Desktop.

Select an option

Save KanzakiRanko1/69730f8ea50878697ca987cab352a824 to your computer and use it in GitHub Desktop.
Google Maps Reverse Geocoding Example
import googlemaps
import pandas as pd
import unidecode
# Google Maps API Key/Client
api_key = 'YOUR_API_KEY'
gmaps = googlemaps.Client(key=api_key)
def to_latlng(address, maps_client=gmaps):
address = unidecode.unidecode(address)
print('Running for address {}'.format(address))
gc = maps_client.geocode(address)[0]
coords = gc['geometry']['location']
print(' Returned {} lat {} lng {}'.format(unidecode.unidecode(gc['formatted_address']),coords['lat'],coords['lng']))
return gc['formatted_address'],coords['lat'],coords['lng']
[YOUR_DATAFRAME_LOADING_CODE]
df['latlng'] = df['address'].apply(to_latlng)
df[['formatted_address','lat','lng']] = pd.DataFrame(df['latlng'].tolist(), index=df.index)
df = df.drop(columns='latlng')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment