Get distance matrix from the Google Maps Distance Matrix API
mp_matrix(
origins,
destinations,
mode = c("driving", "transit", "walking", "bicycling"),
arrival_time = NULL,
departure_time = NULL,
avoid = c(NA, "tolls", "highways", "ferries", "indoor"),
region = NULL,
traffic_model = c("best_guess", "pessimistic", "optimistic"),
transit_mode = c("bus", "subway", "train", "tram"),
key,
quiet = FALSE
)
Origins, as
character
vector with addresses to be geocoded
numeric
vector of length two (lon, lat)
matrix
with two columns (lon, lat)
sf
or sfc
point layer
Destinations, in one of the same formats as for origins
Travel mode, one of: "driving"
, "transit"
, "walking"
, "bicycling"
The desired time of arrival for transit directions, as POSIXct
The desired time of departure, as POSIXct
NA
(default) or one of: "tolls"
, "highways"
, "ferries"
or "indoor"
The region code, specified as a ccTLD ("top-level domain") two-character value (e.g. "es"
for Spain) (optional)
The traffic model, one of: "best_guess"
(the default), "pessimistic"
, "optimistic"
. The traffic_model
parameter is only taken into account when departure_time
is specified!
Transit preferred mode, one or more of: "bus"
, "subway"
, "train"
or "tram"
Google APIs key
Logical; suppress printing URL for Google Maps API call (e.g. to hide API key)
XML document with Google Maps Distance Matrix API response
Use function mp_get_matrix
to extract distance and duration matrix
objects
# Built-in reponse example
library(xml2)
doc = as_xml_document(response_matrix)
if (FALSE) {
# Text file with API key
key = readLines("~/key")
# Using 'data.frame' input
doc = mp_matrix(
origins = rbind(c(34.811, 31.892), c(35.212, 31.769)),
destinations = c(34.781, 32.085),
key = key
)
# Using 'character' input
locations = c("Tel-Aviv", "Jerusalem", "Beer-Sheva", "Eilat")
doc = mp_matrix(
origins = locations,
destinations = locations,
key = key
)
# Setting transit modes
locations = c("Tel-Aviv", "Beer-Sheva", "Eilat")
doc = mp_matrix(
origins = locations,
destinations = locations,
key = key,
mode = "transit",
transit_mode = "train"
)
}