Google places API is a highly valuable resource if you’re looking for certain types of places in a certain region (eg. an Indian restaurant in New York), identifying a business, generating leads for a list of professionals (eg electricians, plumbers, etc).
By Google definition, the Places API is a service that returns information about places using HTTP requests. Places are defined within this API as establishments, geographic locations, or prominent points of interest.
Google has moved everything related to geolocation, places, maps under a hood named “Google Maps Platform”, and is not free anymore. Over the years, it was free until Google realized the potential of these services and now requires you to set up a billing account before you can actually use it. Though they do provide a $200 credit, you still need to put in a card.
So to get started:
- create an account at Google’s Cloud Platform with billing.
- Once set up, please go to the API Console, and create a project.
- Once our project is created, we can actually decide which apis we want to use and enable only those. This is important as we don’t want to enable the apis we won’t use as if the API keys ever gets compromised, the key will only limit to certain API only and not to the whole API collection. This is how it will look once APIs are enabled.
- Now, we have the APIs we want to use, we can start using them. You can find all the documentation here: https://developers.google.com/maps/documentation. We will be using places API in this article, but you can use whichever API you want based on your requirement.
- Before we go further into API development, we need to generate API keys. To create keys, go to: https://console.cloud.google.com/google/maps-apis/credentials and click “Create Credentials” button. It will create an API key for you and will ask you to restrict the API key. Click “Restrict key” to go to the restrictions settings page. Choose the appropriate restrictions based on the app you will be integrating API with.
- Now to use API we can either select and SDK (JS, IOS, ) or just simply use HTTP API calls from the language of your choice.
- Here are the list of place requests types available on Google to query places and its metadata:
- Place Search returns a list of places based on a user’s location or search string.
- Place Details returns more detailed information about a specific place, including user reviews.
- Place Photos provides access to the millions of place-related photos stored in Google’s Place database.
- Place Autocomplete automatically fills in the name and/or address of a place as users type.
- Query Autocomplete provides a query prediction service for text-based geographic searches, returning suggested queries as users type.
- To do a place search, we need to use Place Search. There are three search endpoints available with different characteristics with Place Search. The following table highlights some of these differences (taken from Google):
- Using Find Place, we can do a free search like “Restaurants in New York” or “Hospitals near by” etc. Here is the API call to the same: https://maps.googleapis.com/maps/api/place/findplacefromtext/json?fields=formatted_address%2Cname%2Crating%2Copening_hours%2Cgeometry&input=Museum%20of%20Contemporary%20Art%20Australia&inputtype=textquery&key=YOUR_API_KEY
- Fields are divided into three billing categories: Basic, Contact, and Atmosphere. Basic fields are billed at base rate, and incur no additional charges. Contact and Atmosphere fields are billed at a higher rate. See the pricing sheet for more information. Attributions, html_attributions, are always returned with every call, regardless of whether the field has been requested. You can read more about them here in detail here: https://developers.google.com/maps/documentation/places/web-service/search-find-place
- Using PHP with Curl you can do this:
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?fields=formatted_address%2Cname%2Crating%2Copening_hours%2Cgeometry&input=Museum%20of%20Contemporary%20Art%20Australia&inputtype=textquery&key=YOUR_API_KEY', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', )); $response = curl_exec($curl); curl_close($curl); echo $response;
Hope this helps.