Chapter 10 Google Places API
You will need to install the following packages for this chapter (run the code):
# install.packages('pacman') library(pacman) p_load('httr', 'googleway', 'ggplot2', 'tidyverse', 'mapsapi', 'stars')
10.1 Provided services/data
- What data/service is provided by the API?
The following five requests are available: Place Search, Place Details, Place Photos, Place Autocomplete and Query Autocomplete. Place Search returns a list of places along with summary information about each place based on a user’s location (by proximity) or search string. Once you find a place_id from a Place Search, you can request more details about a particular place by doing a Place Details request. A Place Details request returns more detailed information about the indicated place such as its complete address, phone number, user rating and reviews. Place Photos provides access to the millions of place-related photos stored in Google’s Place database. When you get place information using a Place Details request, photo references will be returned for relevant photographic content. Find Place, Nearby Search, and Text Search requests also return a single photo reference per place, when relevant. Place Autocomplete automatically fills in the name and/or address of a place as users type. Query Autocomplete service provides a query prediction for text-based geographic searches, by returning suggested queries as you type.
Note: You can display Places API results on a Google Map, or without a map but it is prohibited to use Places API data on a map that is not a Google map.
- What are the prerequisites to access the API (authentication)?
The prerequisites to access Google Places API are a Google Cloud project (to create it you need a Google account to log into the Google Cloud Platform) and an API Key. Before creating your API Key, don’t forget to enable Places API! To create your API key, go the APIs & Services > Credentials > API key page.
10.3 Simple API call
- What does a simple API call look like?
The API provides different searches and services that can be accessed via HTTP Urls. These Urls requests all take the same general form and pattern:
Here, service can take the inputs findplacefromtext for find place requests, nearbysearch to look for nearby places, details for a request of place details and more. output may take the value json or xml, dependent on the requested output format.
Furthermore, certain parameters are required for all requests. Most importantly, every request must entail a key parameter, indicating the API key. Second, all search places requests take an input parameter that identifies the search target and an inputtype parameter that identifies the type of input given in the input parameter. For place requests, the inputtype parameter can take the values textquery and phonenumber.
Nearby requests take a location parameter setting longitude and latitude of the requested place as well as a radius parameter. Detail request, however, take a mandatory parameter place_id, which indicates the place for which the details are requested.
Additionally, different optional parameters can be used. These entail a language parameter, a fields parameter indicating the types of place data to return and more.
An examples for an API request for pizza places in Mannheim can look like this:
10.4 API access in R
- How can we access the API from R (httr + other packages)?
Instead of typing the API request into our browser, we can use the httr package’s
GET() function to access the API from R.
# Option 1: Accessing the API with base "httr" commands library(httr) <- Sys.getenv("GooglePlaces_token") key <-GET("https://maps.googleapis.com/maps/api/place/textsearch/json?", query = list( resquery = "pizza", location = "49.487459,8.466039", radius = 5000, key = key ))
Alternatively, we can use a wrapper function for R provided by the R-Package googleway.
library(googleway) <- Sys.getenv("GooglePlaces_token") key set_key(key)
Step 1: Load packages
# Option 2: Accessing the API with googleway # Request 'Mannheim' to get latitude and longitude information <- googleway::google_places("Mannheim")location
# Save latitude and longitude information in vector <- location$results$geometry location <- c(location$location$lat, location$location$lng)location
# Plot places to google map library(mapsapi) # for this you will also need to activate the "maps static API" = mapsapi::mp_map(center = ("49.48746,8.466039"), zoom = 14, key = key, quiet = TRUE) r library(stars) plot(r)
# Google places request with googleway <- google_places("Pizza", location = location, radius = 5000, place_type = "food")pizza
# Plot rankings as barplot $results %>% pizzaggplot() + geom_col(aes(x = reorder(name, rating), y = rating)) + geom_text(aes(x = reorder(name, rating), y = rating), label = paste0(pizza$results$user_ratings_total, " \n ratings"), size = 2) + ylab("Average Rating")+ xlab("") + ggtitle("Pizza Places in Mannheim by Rating") + theme_minimal() + theme( axis.text.x = element_text(angle = 90, size = 8, hjust=0.95,vjust=0.2))
10.5 Social science examples
In his study “Using Google places data to analyze changes in mobility during the COVID-19 pandemic”, Konrad (2020) looked at the “popular times” data provided by Google Places to measure the effect of social distancing effort on mobility.