mp_get_matrix: Extract distance or duration *matrix* from a Google Maps...

View source: R/mp_get_matrix.R

mp_get_matrixR Documentation

Extract distance or duration *matrix* from a Google Maps Distance Matrix API response

Description

Extract distance or duration *matrix* from a Google Maps Distance Matrix API response

Usage

mp_get_matrix(
  doc,
  value = c("distance_m", "distance_text", "duration_s", "duration_text",
    "duration_in_traffic_s", "duration_in_traffic_text")
)

Arguments

doc

XML document with Google Maps Distance Matrix API response

value

Value to extract, one of: "distance_m" (the default), "distance_text", "duration_s", "duration_text", "duration_in_traffic_s", "duration_in_traffic_text"

Value

A matrix, where rows represent origins and columns represent destinations. Matrix values are according to selected value, or NA if the API returned zero results

Note

The "duration_in_traffic_s" and "duration_in_traffic_text" options are only applicable when the API response contains these fields, i.e., when using mp_matrix with mode="driving", with departure_time specified, and API key key provided

Examples


library(xml2)
doc = as_xml_document(response_matrix)
mp_get_matrix(doc, value = "distance_m")
mp_get_matrix(doc, value = "distance_text")
mp_get_matrix(doc, value = "duration_s")
mp_get_matrix(doc, value = "duration_text")

## Not run: 
# Text file with API key
key = readLines("~/key")

locations = c("Tel-Aviv", "Jerusalem", "Neve Shalom")

# Driving times
doc = mp_matrix(
  origins = locations,
  destinations = locations,
  mode = "driving",
  departure_time = Sys.time() + as.difftime(10, units = "mins"),
  key = key
)
mp_get_matrix(doc, value = "distance_m")
mp_get_matrix(doc, value = "distance_text")
mp_get_matrix(doc, value = "duration_s")
mp_get_matrix(doc, value = "duration_text")
mp_get_matrix(doc, value = "duration_in_traffic_s")
mp_get_matrix(doc, value = "duration_in_traffic_text")

# Public transport times
doc = mp_matrix(
  origins = locations,
  destinations = locations,
  mode = "transit",
  key = key
)
mp_get_matrix(doc, value = "distance_m")
mp_get_matrix(doc, value = "distance_text")
mp_get_matrix(doc, value = "duration_s")
mp_get_matrix(doc, value = "duration_text")


## End(Not run)

mapsapi documentation built on July 26, 2023, 5:20 p.m.