R/mp_get_matrix.R
mp_get_matrix.Rd
Extract distance or duration *matrix* from a Google Maps Distance Matrix API response
mp_get_matrix(
doc,
value = c("distance_m", "distance_text", "duration_s", "duration_text",
"duration_in_traffic_s", "duration_in_traffic_text")
)
XML document with Google Maps Distance Matrix API response
Value to extract, one of: "distance_m"
(the default), "distance_text"
, "duration_s"
, "duration_text"
, "duration_in_traffic_s"
, "duration_in_traffic_text"
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
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
library(xml2)
doc = as_xml_document(response_matrix)
mp_get_matrix(doc, value = "distance_m")
#> Tel Aviv-Yafo, Israel Jerusalem, Israel
#> Tel Aviv-Yafo, Israel 0 66866
#> Jerusalem, Israel 68524 0
#> Be'er Sheva, Israel 110858 106562
#> Be'er Sheva, Israel
#> Tel Aviv-Yafo, Israel 108728
#> Jerusalem, Israel 118083
#> Be'er Sheva, Israel 0
mp_get_matrix(doc, value = "distance_text")
#> Tel Aviv-Yafo, Israel Jerusalem, Israel
#> Tel Aviv-Yafo, Israel "1 m" "66.9 km"
#> Jerusalem, Israel "68.5 km" "1 m"
#> Be'er Sheva, Israel "111 km" "107 km"
#> Be'er Sheva, Israel
#> Tel Aviv-Yafo, Israel "109 km"
#> Jerusalem, Israel "118 km"
#> Be'er Sheva, Israel "1 m"
mp_get_matrix(doc, value = "duration_s")
#> Tel Aviv-Yafo, Israel Jerusalem, Israel
#> Tel Aviv-Yafo, Israel 0 3808
#> Jerusalem, Israel 3693 0
#> Be'er Sheva, Israel 4854 5194
#> Be'er Sheva, Israel
#> Tel Aviv-Yafo, Israel 4857
#> Jerusalem, Israel 5335
#> Be'er Sheva, Israel 0
mp_get_matrix(doc, value = "duration_text")
#> Tel Aviv-Yafo, Israel Jerusalem, Israel
#> Tel Aviv-Yafo, Israel "1 min" "1 hour 3 mins"
#> Jerusalem, Israel "1 hour 2 mins" "1 min"
#> Be'er Sheva, Israel "1 hour 21 mins" "1 hour 27 mins"
#> Be'er Sheva, Israel
#> Tel Aviv-Yafo, Israel "1 hour 21 mins"
#> Jerusalem, Israel "1 hour 29 mins"
#> Be'er Sheva, Israel "1 min"
if (FALSE) {
# 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")
}