# Define the API key and other parameters
<- "" # Insert API Key inside quotes
api_key <- "39.1031,-84.5120" # Coordinates for Cincinnati
location <- 1000 # Search radius in meters
radius <- "hotel" # What I am looking for
type
# Construct the API URL
<- paste0(
url "https://maps.googleapis.com/maps/api/place/nearbysearch/json?",
"location=", location,
"&radius=", radius,
"&type=", type,
"&key=", api_key
)
# Make the GET request to the API
<- GET(url)
response
# Parse the response data
<- fromJSON(content(response, "text", encoding = "UTF-8"))
data
# Convert the 'results' section to a data frame
<- as.data.frame(data$results) data_frame
Assignment 6
Google Maps Places API
Introduction
The Google Maps Places API is a powerful tool that allows users to query for a variety of places (such as restaurants, hotels, gas stations) within a defined area. It is particularly useful for location-based apps or services, helping users find nearby places and get real-time data on locations, reviews, ratings, etc..
How to Access Google Maps Places API
To use the Google Maps Places API, you’ll need an API key. Follow these steps to set it up:
- Go to the “Google Cloud Console”
- Create a new project or select an existing one (top of the page)
- Enable the Google Places API (may have to create an account first)
- Generate an API key from the ‘Credentials’ section
- Use the key in your R script to authenticate your API calls
Here I accessed the Google Maps Places API and analyzed the JSON response. You will need to insert your own API key within the “” on the api_key line in order to see the data.
Call to the API
In this example (above), I used the Google Maps Places API to search for hotels near a location in Cincinnati. I specified the location using latitude and longitude coordinates, set a search radius of 1000 meters, and defined the type of place I was interested in (hotels). The API responded with a JSON object containing details of the matching places, which I then analyzed and stored in a data frame.
In the data frame, you will see a result of 20 hotels (each row represents one hotel) and 17 different variables (columns) describing the hotels. The Google Maps Places API provided me the name of the hotel, the address, the latitude/longitude, the status (operational or not), a rating, etc.