If you want to get all of the restaurant data from a specific city by text search in your application, you have come to the right place. In this tutorial, you’re going to learn how to do exactly that using Google Maps Places API. Also, I will be covering how to get more information about a specific restaurant using place_id.
STEP #1: Get the API Key
1. Log in to Google Cloud Platform
2. Then, go to Select a project ▾ drop down menu which will open up a dialog box with your existing projects if any. Choose the one that you want to obtain an API key from.
3. Otherwise, create a new project by clicking the NEW PROJECT button at the top right of the dialog box.
4. Once the project is selected, go to the Navigation Menu button at the top left of the page, choose APIs & Services → Credentials
5. Select Create Credentials → API Key which will open up a dialog box with your API Key.
That’s it… you have it!
STEP #2: Make A Request On Places API
1. Here is the base URL for Places API
http://maps.googleapis.com/maps/api/place/textsearch/json
All you need is two query parameters in order get this working, these are
-
- query: The actual text string that you want to get search results for. For example, restaurants in Toronto
- key: API key that you have obtained from the previous section.
So, the final URL with query parameters will look like this:
http://maps.googleapis.com/maps/api/place/textsearch/json?query=[yourquerystring]&key=[YOURAPIKEY]
Don’t forget to replace with your text string and API key
If you send a GET request from the browser at this stage, you may get an unauthorized API error.
STEP #3: Enable Maps JavaScript API & Places API
1. Go to APIs & Services → Dashboard → Enable APIs & Services at the top and Choose Maps JavaScrip API from the API Library. This will open up Map JavaScript API page and Enable it.
2. Scroll down to the bottom of the page and you can find Places API under the “More Solutions to Explore” section and Enable it as well.
STEP #4: Get Restaurants Data
https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+toronto+canada&key=YOURAPIKEY
The response JSON object will have 20 restaurants like the screenshot below.
Install JSON Formatter which is a chrome extension that will help to format JSON object.
STEP #5: Get Restaurant Details Data Using Place_id
Here is the Base URL:
https://maps.googleapis.com/maps/api/place/details/json
As you can see, I have passed three query parameters for this request.
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJKSp50NI0K4gR6C6L3yYqpYY&fields=name&key=YOURKEY
-
- placeid: This can be found the previous request JSON Object with the property name place_id.
- key: Your API Key.
- fields: Multiple values can be passed by commas and I have just a name field for now.
Some of the useful fields are as follows: name, rating, formatted_address, type, geometry, formatted_phone_number, opening_hours and more.
Here is the sample JSON response for the details request.
There you have it
Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.