Assignment 6

Google Maps Places API

Author

CT

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:

  1. Go to the “Google Cloud Console”
  2. Create a new project or select an existing one (top of the page)
  3. Enable the Google Places API (may have to create an account first)
  4. Generate an API key from the ‘Credentials’ section
  5. 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.

# Define the API key and other parameters
api_key <- "" # Insert API Key inside quotes
location <- "39.1031,-84.5120"  # Coordinates for Cincinnati
radius <- 1000  # Search radius in meters
type <- "hotel"  # What I am looking for

# Construct the API URL
url <- paste0(
  "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
response <- GET(url)

# Parse the response data
data <- fromJSON(content(response, "text", encoding = "UTF-8"))

# Convert the 'results' section to a data frame
data_frame <- as.data.frame(data$results)

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.