Mapsapi
'sf'-Compatible Interface to Google Maps APIs
Install / Use
/learn @michaeldorman/MapsapiREADME
mapsapi
The mapsapi package provides an interface to the Google Maps APIs,
currently four of them:
- <a href="https://developers.google.com/maps/documentation/directions/" target="_blank">Google Maps Direction API</a>
- <a href="https://developers.google.com/maps/documentation/distance-matrix/" target="_blank">Google Maps Distance Matrix API</a>
- <a href="https://developers.google.com/maps/documentation/geocoding/" target="_blank">Google Maps Geocode API</a>
- <a href="https://developers.google.com/maps/documentation/maps-static/" target="_blank">Maps Static API</a>
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.
-
Given a directions response, functions
mp_get_routesandmp_get_segmentscan be used to process the response document into a spatial layer. Functionmp_get_routesgives each alternative as a separate line, while functionmp_get_segmentsgives each segment (that is, a portion of the route associated with specific driving instructions) as a separate line. -
Given a distance matrix response, function
mp_get_matrixcan be used to obtain distance/duration matrices. -
Given a geocode response, functions
mp_get_pointsandmp_get_boundscan 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.
Installation
The CRAN version can be installed with:
install.packages("mapsapi")
The development version can be installed using remotes:
install.packages("remotes")
remotes::install_github("michaeldorman/mapsapi")
Once installed, the package can be loaded with library:
library(mapsapi)
Note: due to new Google Maps API policy, starting from June 2018 the functions require an API key.
key = "AIz....."
Documentation
The complete documentation can be found at https://michaeldorman.github.io/mapsapi/.
Example
The following code section obtains (and plots) the driving directions from New-York to Los Angeles.
# Get routes (XML document)
doc = mp_directions(
origin = "New-York",
destination = "Los Angeles",
alternatives = TRUE,
key = key,
quiet = TRUE
)
# Extract lines 'sf' layer
r = mp_get_routes(doc)
# Plot
library(maps)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
map("state", fill = FALSE, col = "grey")
plot(st_geometry(r), col = c("red", "green", "blue"), add = TRUE)
<!-- -->
Related Skills
node-connect
341.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.6kCommit, push, and open a PR
