Note: If you prefer a video tutorial, skip to the next section.
Google Maps Platform is a set of APIs and SDKs that are managed from the Google Cloud console.
The step-by-step instructions to create an API for google maps can be found at this link.
To get started with Google Maps Platform you need to take the following four steps:
1. Create a billing account You will have to log into your Google account and add your billing information to start the process of getting and API Key. Google Maps charges $2 per 1000 API requests for static maps and $5 per 1000 API requests for geocoding or directions.
2. Create a project Next you go to the Google Cloud Platform and select “Got to console” and then select “create a new project.”
3. Enable one or more APIs or SDKs Next, go to the APIs & Services > Credentials page. On the Credentials page, click Create credentials > API key. The API Key created dialog displays your newly created API key.
4. Get, add, and restrict an API Key Close the page and your new API Key will be listed on the Credentials page under API. It recommended that you also follow these procedures to restrict your API Key. Restrictions add security to your application by ensuring only authorized requests are made with your API Key.
Now, you can copy your API Key from your APIs and Services Dashboard on the main Google Cloud Platforms page to use in R.
If you prefer video instructions, Google Maps made this informative tutorial:
leaflet is an easy to use mapping package available through CRAN. Even without using any APIs, it is easy to generate maps using the leaflet package. An easy way to use leaflet without an API is to look up the latitude and longitude of a place you want to map and then enter those coordinates into leaflet. For example, the coordinates of Xavier University are:
39.1498° N, 84.4741° W
When entering these coordinates, it is important to remember that positive latitude is above the equator (N), and negative latitude is below the equator (S). Positive longitude is east of the prime meridian, while negative longitude is west of the prime meridian (a north-south line that runs through a point in England). In the case of Xavier University, we need to enter the longitutde as negative since it is West. We keep the latitude positive since it is North.
leaflet() %>%
addTiles() %>%
addMarkers(lat = 39.1498, lng = -84.4741,
popup = "Xavier")
We have seen how easy making maps in leaflet can be. With our own Google API, we open up a whole new world of possibilities in terms of what kinds of maps we can make. There are many popular R packages that can make use of Google Maps but we will focus on mapsapi, which can be installed via CRAN.
The mapsapi package provides an interface to these four Google Maps APIs:
One very useful function in the mapsapi package is mp_directions. It uses our Google API to give us directions from one point to another.
Before getting started, we need to copy our API key we generated from Google into R. We will assign it as a text string to a variable named “key” that we will have to use for every mapsapi request we make.
In our first example, we will get directions from Xavier to the oldest Graeters location (in Hyde Park Square). We will start out with the coordinates for Xavier we used above and use them as our origin. To keep it simple to start out, we will also look up the longitudinal and latitudinal coordinates of the Graeters location we are interested in. We need to enter the longitude first, followed by the latitude.
We will use “driving” as our mode of transportation we want to get directions for. If we wanted to, we could instead get directions for walking, bicycling, or transit. Our key will refer back to the object we created earlier with our Google API key. We will set alternatives equal to FALSE so we get only the best route and assign these directions to iceCream.
iceCream <- mp_directions(origin = c(-84.4741, 39.1498), destination = c(-84.4430, 39.1399),
mode = "driving",
key = key,
alternatives = FALSE)
We have our directions. Now we will use the mp_get_routes function from the mapsapi package to obtain our route that we can then use as our data in the addPolyLines funcion inside of leaflet.
route = mp_get_routes(iceCream)
pal = colorFactor(palette = "Dark2", domain = route$alternative_id)
leaflet() %>%
addProviderTiles("CartoDB.DarkMatter") %>%
addPolylines(data = route, opacity = 1, weight = 7, color = ~pal(alternative_id))
The functions mp_directions, mp_matrix and mp_geocode are used to access the Directions, Matrix and Geocode APIs, respectively. They return an xml_document object (package xml2) with the response contents. We used the mp_directions function above.
Given a directions response, functions mp_get_routes and mp_get_segments can be used to process the response document into a spatial layer. Function mp_get_routes gives each alternative as a separate line, while function mp_get_segments gives each segment as a separate line. Above, we used mp_get_routes. Thus, our line was one color instead of a series of different colors.
Given a distance matrix response, function mp_get_matrix can be used to obtain distance/duration matrices.
Given a geocode response, functions mp_get_points and mp_get_bounds can be used to obtain geocoded locations as a point or polygon (bounds) layer.
The fourth function mp_map is used to access the Maps Static API. It returns a stars raster RGB image, which can be used as background in maps.