Since I don’t think the documentation from Google is very clear on how to actually implement the Geocoding API, especially to a newbie like I am, here are the steps to geocode an address or reverse-geocode a set of coordinates and to get the key you want
Reverse-geocoding
Here is a short piece of code in Python
import googlemaps
gmaps = googlemaps.Client(key='<enter your Google API key here>’)result = gmaps.reverse_geocode((40.714224, -73.961452))
print(result)
If you run that code, what you will get is a very long json. It is important to use an online tool to have a good understanding of your code’s result. Here is how it looks
Go through each number to see what keys interest you. Let’s say if you are interested in [0] in the json. Here is the code to get it:
import googlemaps
gmaps = googlemaps.Client(key='<enter your Google API key here>’)result = gmaps.reverse_geocode((40.714224, -73.961452))
print(result[0])
Here is how it looks
Continue to parse through the JSON to get the key you want such as result[0][‘geometry’]…
Geocoding
import googlemaps
gmaps = googlemaps.Client(key='<enter your Google API key here>’)result = gmaps.reverse_geocode(‘1600 Amphitheatre Parkway, Mountain View, CA’)
print(result)
The rest is similar to Reverse-coding
Concatenate strings to form a full address
If you have address, city, state, zip code and countries in different columns, one quick way to form a full address is
df[‘fulladdress’] = df[[‘Address’, ‘City’, ‘State’, ‘Zip’]].apply(lambda x: ‘ ‘.join(x.astype(str)), axis=1)
Df stands for the current data frame. df[‘fulladdress’] refers to the creation of a new column called “full address”. The whole line of code means to add values on the same row in the address, city, state and zip columns to form a full address accordingly.
Hope it helps