Extracting information about geo-location using Google Places API Web Service in Python

Subarna Rana
4 min readApr 16, 2018
An introduction to using Google Places API Web Service in Python

Pre-requisites:

Who is this for?

Data Scientist or Data Analyst who want to get info about a geo location using either Address or Lat Long.

Main:

Nowadays for a variety of analysis, getting information from geo location can be very helpful. Be in co-ordinates of location or address, google geo APIs provides a good way to fetch information such as distance, types of place etc. Although, there are packages like geopy and geocoder in Python to get info from location which are easy to implement but they do not provide data that google APIs do provide. If you just want the address from lat long or vise versa, I suggest you https://geopy.readthedocs.io/en/stable/. In the below section, we will explore how to activate the APIs and what you need from GCP.

Part 1: First things first

Before jumping into Python code, we need to enable the API services offered by Google in GCP environment. Once logged into a Project in GCP, goto APIs & Services and click Dashboard as shown below.

In the dashboard, you would be able to see alot of info about the APIs such as traffic, latency etc. In the bottom, you will be able view the list of APIs already enabled for your project in GCP. We are in this are to enable the required APIs for our use. Click on ENABLE APIS AND SERVICES. To fetch info about geo-locations, you will need to enable Google Places API Web Service. Search the same in the search box and click enable. It may take upto 5 mins for google to enable the same for you. Once the API is enabled, you will need to generate API key. Go back API & Services dashboard and select Credentials. Click on Create Credentials -> API key to be able to generate API key.

Please note it may take upto 10 mins for Google to activate the generated key for you. It is suggested to provide restrictions to your API key for security purposes. To provide restrictions to your API key, click on the API, that will open up a google form to be used to put restrictions to your API key (https://support.google.com/cloud/answer/6158857?hl=en). Now that we have enabled the API and generated the key, we will move on the Part 2 of this blog which is using the API through Python.

Part 2: Python and Geo Locations

Google Places API Web Service can be used for variety of purposes such as Place Search, Place Details, Please autocomplete, Query autocomplete and Place Photos. In this blog, we will look into getting place details given lat long of different locations. Consider a case, where you have a file containing co-ordinates. You will want to gain information about the places given latitude and longitude. The information to be fetched can be Address, Type of place, Distance etc.

A note on lattitude and longitude — Sometimes you may not have the exact lat long in practical cases. For example, it may be the case that your lat long 51.88 0.78. In such cases, you can approximate the area by ranking with distance and radius (inputs to API).

Lets look into the function below. The parameter inputs to this function are lattitude, longitude, radius and auth_key. Look closely at the API call. It takes lat long at inputs through location and we have put a sorting by distance and a radius provided. Upon call of this API, it will return a JSON object. We will fetch the required information from the JSON object.

# Function to fetch data from google api
def GoogPlac(lat,lon,radius,auth_key):
location = str(lat) + ',' + str(lon)
MyUrl = ('https://maps.googleapis.com/maps/api/place/nearbysearch/json'
'?location=%s'
'&rankBy=distance&radius=%s'
'&key=%s') % (location, radius, auth_key)
#grabbing the JSON result
response = urllib.request.urlopen(MyUrl)
jsonRaw = response.read()
jsonData = json.loads(jsonRaw)
return jsonData

Below is an example of extracting type of place from the JSON object.

data['zero_type']=data.apply(lambda x: x['place_name']['results'][0]['types'] if len(x['place_name']['results'])>=1 else 'Na',axis=1)

Thats it. A simple way to get data from the location coordinates. Please refer to below github link for code and sample data.

https://github.com/subarnab219/Using-geo-location-using-Google-Places-API-Web-Service-in-Python

--

--