Using Google Maps in R

Routes using Leaflet

Using Google Maps in R is easy! This series of blog posts will guide you through the possibilities to work with Google Maps in R.

In part one we will discuss the package mapsapi. In part two we will discuss a ggplot2 friendly option: ggmaps.

Getting your API key(s)

Whether you want to work with mapsapi or ggmaps: the first step is getting your API key(s). Sounds like an easy step, right? Be aware that this may take some time.

You will need a billing account on Google Cloud Platform (GCP). This requires you to attach your credit card to your account. After you have a billed account, you can set up a project. Within this project you can enable API(s). Detailed instructions on how to generate API key(s) are in this article.

There is not one single API for Google Maps. Actually, there are multiple services underneath Google Maps. Every service has its own API. For example, there is an API for directions (Directions API) and an API for geocoding (Geocoding API). The kind of API you need depends on the package and functions you’re going to use.

In essence you only need one API key. This single API key can give access to multiple API(s). But that’s a choice. You can also opt to have a key for each API. You can manage everything to your liking within GCP. I just went along with a single key to grant me access to multiple APIs.

To follow the examples in this blog you will need the Directions API and Geocoding API.

Your API key is private and unique to you, so be careful not to share it online. Be aware when opening a GitHub issue or saving it in a shared R script. If you share it inadvertantly, just get on Google’s website and regenerate your key – this will retire the old one.

Once you get your keys you are can easily access and manage them via GCP. Using API(s) comes at a cost. While the amount you get for free is enough for playing around, you might want to keep an eye out on the costs.

Visualizing routes

Time to code! One thing you might know about me: I love hiking. Let’s say I want to hike from my beautiful hometown, Oegstgeest, to Alphen aan den Rijn. How do we visualize this in R?

To visualize routes we need to have the Directions API enabled on GCP. To interact with the API we use the package mapsapi. For visualization we use leaflet. Don’t worry, you don’t necessarily need to use leaflet, but it’s a pretty nice package for plotting maps.

 

# Libraries
library(mapsapi)
library(leaflet)

# Your private key
key = "XXX"

# Getting the directions from the Google Maps Directions API
doc = mp_directions(
  origin = "Oegstgeest",
  destination = "Alphen aan den Rijn",
  mode = "walking",
  alternatives = TRUE,
  key = key,
  quiet = TRUE
)

# Extract routes from the response
routes = mp_get_routes(doc)

# Visualize using leaflet
palette = colorFactor(palette = "Set2", domain = routes$alternative_id)

leaflet() %>% 
  addProviderTiles(provider = providers$CartoDB.Positron) %>%
  addPolylines(data = routes, 
               opacity = 1, 
               weight = 7, 
               color = ~palette(alternative_id),
               label = ~distance_text,
               labelOptions = labelOptions(noHide = TRUE))

Resulting in:

Routes using Leaflet

That hike is going to take some time. Especially when you want to hike back ;).

With numerous options and themes you can completely customize your plot. Once you obtain the routes object you can extract all the information from it.

Visualizing locations

To visualize locations you need to enable the Geocoding API. 

Visualizing locations can be useful when you want to map points of interest. Locations can be addresses, place names, but also coordinates.

Let’s work out an example with some infamous places near my hometown:

# Libraries
library(mapsapi)
library(leaflet)

# Your private key
key = "XXX"

# Getting the geolocations from the Google Maps Directions API
addresses = c("Oegstgeest", "Katwijk aan Zee", "Leiden", "Warmond",
               "Voorschoten", "Alphen aan den Rijn")

doc = mp_geocode(
  addresses = addresses,
  key = key
)

# Extract pointss from the response
point = mp_get_points(doc)

# Visualize using leaflet
palette = colorFactor(palette = "Set2", domain = point$location_type)

leaflet() %>% 
  addProviderTiles(provider = providers$CartoDB.Positron) %>%
  addCircleMarkers(data = point)

Generating the following plot:

Google maps locations R

So here are all our six specified locations on a map! Pretty easy, right?

Wrap-up

Hopefully you learned something about using Google Maps in R. Obviously this is not a complete guide on all the things you can do. The sky (or map?) is the limit. 

We explored just one package, so head over to part two for more!

PS: view this GitHub repository for the scripts of this blog series