Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mp_matrix

Get distance matrix from the Google Maps Distance Matrix API


Description

Get distance matrix from the Google Maps Distance Matrix API

Usage

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"),
  key,
  quiet = FALSE
)

Arguments

origins

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

Destinations, in one of the same formats as for origins

mode

Travel mode, one of: "driving", "transit", "walking", "bicycling"

arrival_time

The desired time of arrival for transit directions, as POSIXct

departure_time

The desired time of departure, as POSIXct

avoid

NA (default) or one of: "tolls", "highways", "ferries" or "indoor"

region

The region code, specified as a ccTLD ("top-level domain") two-character value (e.g. "es" for Spain) (optional)

traffic_model

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!

key

Google APIs key

quiet

Logical; suppress printing URL for Google Maps API call (e.g. to hide API key)

Value

XML document with Google Maps Distance Matrix API response

Note

Use function mp_get_matrix to extract distance and duration matrix objects

References

Examples

# Built-in reponse example
library(xml2)
doc = as_xml_document(response_matrix)

## Not run: 
# 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("Haifa", "Tel-Aviv", "Jerusalem", "Beer-Sheva")
doc = mp_matrix(
  origins = locations,
  destinations = locations
)

## End(Not run)

mapsapi

'sf'-Compatible Interface to 'Google Maps' APIs

v0.4.8
MIT + file LICENSE
Authors
Michael Dorman [aut, cre], Tom Buckley [ctb], Alex Dannenberg [ctb], Mihir Bhaskar [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.