Google Maps: Places API
A step-by-step guide on how to use Google Places API — Find Place requests
I was working on a machine learning classification model to determine the level of diabetes in the population of the USA and decided to include a McDonald’s-to-population ratio variable in my data. I covered the steps I followed to collect McDonald’s addresses via web scraping on my Web Scraping for Data Collection blog post.
I used the Google Places API to get the full formatted address of the restaurants and the name registered for that address on Google Maps. Also, the Google Places API has a “permanently closed” feature, and I wanted to be sure that the restaurants on my list were still open to the public.
During this blog post I will give you a step-by-step guide on how I used Google Maps Places API and I will share the python routine I used for the API requests.
Using Google Maps — Places API
The Places API is one of the products offered within the Google Maps Platform in the Google Console. There are several different features and services you can get from the Places API, but they all come at different costs. For this exercise, I used the Places API — Find Place request and retrieved fields from the Basic category in order to avoid any costs on my requests.
Getting started:
- Create a Billing Account: You need a Google Account and a credit card for this step. Google gives you an initial $300 free trial balance to use within 365 days. This credit can be applied to any service from the Google console. Be aware of the cost of the services and features you use from the Google Console, as you can easily run out of free trial credits. For this exercise, I used free resources from the Places API, therefore I did not use any credits from my free trial balance. Following this link you can sign-up for your Google Cloud Platform account to access the Google Maps Places API.
- Create a project: this is usually done during the sign-up process. A project within the Google console is an organizing entity for settings, permissions and usage-tracking for what you are building. There is a nice getting started checklist that will help you with the initial setup once you have created your billing account.
- Restrict API key: go to APIs & Services >> Credentials. Here you can copy your project’s API Key and you can edit its settings. See Figure 1 for visuals on this step. It is recommended that you restrict your API key within the Google console. Restrictions can specify which Google APIs the key can call. This is to avoid unintended use of your key. Figure 2 shows how I enabled my API Key to the Places API only.
Once you have your API key, you are ready to make your API requests. As mentioned before, for this exercise I used the Google Maps Places API — Find Place request and requested basic fields. Make sure you review the documentation on the Places API as it explains the different requests you can make, the fields you can retrieve and details on usage quotas and billing.
Python Code
Below is the python routine I put together to send the requests to the Google Maps Places API. I put together the URL of the request following directions from the Find Place Requests documentation.
Important notes:
- After your free trial credit is spent, you will not be able to access any of the resources from the Google Console (even the free ones). You will need to activate the billing to your account in order to continue using the Google Console.
- Make sure you understand the usage quotas and costs associated to the requests you are making.
- Make sure you keep your API Key private and secure. Never hard code your API Key to your code.
- Be aware that the set-up of the billing of your project can take up to three days after signing up. Be careful of the resources you try during this period as it will not be displayed on your billing tracker and you could easily use all your free trial credit without knowing.
- This guide is not a substitute for the official documentation in the Google Cloud Console