Imagine that you have a friend who recommend a restaurant to you, how will you check its location and know if it’s really good? Maybe Google Maps is a good choice, since you can get its note and reviews given by clients. In this blog, I’ll talk about how to get the note and reviews for a batch of address by Google Place API:
- What is Google Place API?
- How to get information by Google Place API?
- How to realise the request by Python?
What is Google Place API?
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.
The following place requests are available:
- 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.
How to get information by Google Place API?
In order to get note and reviews by API, we need location’s place_id
. If you
don’t have the information, we can search it by “Place Search” request or
“Geocoding API”, then get reviews by “Place Details” request.
In the following, I will show how to get note and reviews by Google API. If you
have the place_id
, you can jump to “Step 2”.
Step 1: If you have coordinates, we can get “place_id” by “Place Search”
The Places API lets you search for place information using a variety of categories, including establishments, prominent points of interest, and geographic locations. 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.
I prefer to search for places by proximity, since I assume that the coordinates that I have might not be accurate. A Nearby Search lets you search for places within a specified area. You can refine your search request by supplying keywords or specifying the type of place you are searching for.
A Nearby Search request is an HTTP URL of the following form:
where output
may be either of the following values:
json
(recommended) indicates output in JavaScript Object Notation (JSON)xml
indicates output as XML
Required parameters:
key
— Your application’s API key. This key identifies your application.location
— The latitude/longitude around which to retrieve place information. This must be specified as latitude,longitude.radius
— Defines the distance (in meters) within which to return place results. The maximum allowed radius is 50 000 meters. Note thatradius
must not be included ifrankby=distance
is specified.- If
rankby=distance
is specified, then one or more of keyword, name, or type is required.
Certain parameters are required to initiate a Nearby Search request. As is
standard in URLs, all parameters are separated using the ampersand (&)
character. About information of parameters
, you can find here.
Example
Imagine that I have the coordinates of one place, its latitude and longitude
are 48.830216 and 2.806147, respectively; I want to find the nearest location
whose name is “franprix”. We can find its place_id
with the following request:
then we can get the result in “json” format:
…
According to the result, we can get the correct coordinates, correct address,
global rating of location, total reviews’ amount, the place_id
and else. Once
we get the place_id
, we can continue to step 2.
Step 1 bis: If you have an address, we can get “place_id” by “Geocoding API”
Instead of coordinates, perhaps you have only address. In this case, we can
apply “Geocoding API” to get the place_id
.
Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map.
A Geocoding API request takes the following form:
where outputFormat
may be either of the following values:
json
(recommended) indicates output in JavaScript Object Notation (JSON)xml
indicates output in XML
About information of parameters
, you can find here.
Example
Imagine that I have the address”1 Rue du Pré des Merlans, 77700
Bailly-Romainvilliers”, we can find its place_id
with the following request:
then we can get the similar result as above, where we can find place_id
.
Step 2: If you have “place_id”, we can get reviews by “Place Details”
Once you have a place_id
from a Place Search, you can request more details
about a particular establishment or point of interest by initiating a Place
Details request. A Place Details request returns more comprehensive information
about the indicated place such as its complete address, phone number, user
rating and reviews.
A Place Details request is an HTTP URL of the following form:
where output
may be either of the following values:
json
(recommended) indicates output in JavaScript Object Notation (JSON)xml
indicates output as XML
Certain parameters are required to initiate a search request:
key
(required) — Your application’s API key. This key identifies your application for purposes of quota management.place_id
(required) — A textual identifier that uniquely identifies a place, returned from a Place Search.
About information of parameters
, you can find here.
Example
Now let’s use the place_id
that we found at the first step with the following
request:
then we can get the result in “json” format:
According to the result, we can get the time, note and text for each review. We can get 5 reviews at most by “Place Details”.
How to realise the request by Python?
If you want to get requests’ results by Python, we need to import requests
module and urlencode
from urllib.parse
, then you find 3 python functions
for the steps above in the following.
Get “place_id” with coordinates
Get “place_id” with address
Get reviews with “place_id”
Reference
- “Places API”, developers.google.com. [Online]. Available: https://developers.google.com/places/web-service/intro
- “Place Search”, developers.google.com. [Online]. Available: https://developers.google.com/places/web-service/search
- “Place Details”, developers.google.com. [Online]. Available: https://developers.google.com/places/web-service/details
- “Geocoding API”, developers.google.com. [Online]. Available: https://developers.google.com/maps/documentation/geocoding/start
- henry perks, “BJXAxQ1L7dI”, unsplash.com. [Online]. Available: https://unsplash.com/photos/BJXAxQ1L7dI