Lessons learned about Google Places
2014-09-19
The Google Places API allows you to query for place information on a variety of categories
The Google Places API allows you to query for place information on a variety of categories, such as: establishments, prominent points of interest, geographic locations, and more. You can search for places either by proximity or a text string. A Place Search returns a list of places along with summary information about each place; additional information is available via a Place Details query. In our case, we have done queries by establishments, so we will use the following place as an example in the rest of the article: Power Properties building, located at 5400 Live Oak Street, Dallas, TX, United States.
The first thing that you need to get details (e.g. reviews list) of a place from Google Places is the establishment place_id
. Because you only have a establishment name, you need to perform a query to Google Maps calling it's REST API:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Power%20Properties,%20Live%20Oak&key=AddYourOwnKeyHere
This query would return a json (could be a xml if you ask for) with the results. You will see many results, depending of the query parameter, so you will need to look what it the establishment that you want to get more detailed and then get the place_id of it.
See more documentation here: https://developers.google.com/places/documentation/search
As an alternative, you can configure a Google Map SearchBox so, as soon as you start writing and address or place name, it shows alternatives directly from Google Maps.
HTML CODE
<script src="http://maps.googleapis.com/maps/api/js?v=3&libraries=places&sensor=false"></script> <input class="form-control" id="place" name="place" placeholder="apartment name" type="text" value="" /> <output></output>
JS CODE
var places, userPlace, $input = $("#place"); var autocomplete = new google.maps.places.SearchBox($input[0]); google.maps.event.addListener(autocomplete, 'places_changed', function() { var places = autocomplete.getPlaces(); if (places.length == 0) return; firstPlace = places[0]; alert(firstPlace.place_id); });
In the places_changed event you will get the places (hopefully one) that match your search query and then you can get it's place_id (e.g. ChIJr8fiNUufToYROMv7EDCpIuY
). You can see more about SearchBox class here: https://developers.google.com/maps/documentation/javascript/reference#SearchBox
To get the details of a place (once you have the place_id
) you have to call other REST API like this:
This query will return a new json with the details of the placed represented by that placeid. In our case, we get the reviews of that place.
Some limitations of Place Details:
- The REST API let you grab only the latest 5 "most useful" 5 reviews of a place (although it has many more). Many people reported this issue.
- If some place has less than 5 reviews, the API don't get the overall rating, so if you want it, you will need to iterate over the individual reviews and calculate the average of them.
You can see more information about Place Details here:
Acknowledgments
This post was written for Vairix Software Development, so I want say thanks to them for let me share this with you in my homepage.
Significant Revisions
Sep 19, 2014: Original publication on vairix.com
Dic 9, 2014: Original publication on dariomac.com