mp_directions function

Get directions from the Google Maps Directions API

Get directions from the Google Maps Directions API

mp_directions( origin, waypoints = NULL, destination, mode = c("driving", "transit", "walking", "bicycling"), arrival_time = NULL, departure_time = NULL, alternatives = FALSE, avoid = c(NA, "tolls", "highways", "ferries", "indoor"), region = NULL, traffic_model = c("best_guess", "pessimistic", "optimistic"), transit_mode = c("bus", "subway", "train", "tram"), transit_routing_preference = c(NA, "less_walking", "fewer_transfers"), language = NULL, key, quiet = FALSE )

Arguments

  • origin: Origin, as

    • character vector of length one with address to be geocoded
    • numeric vector of length two (lon, lat)
    • matrix with one row and two columns (lon, lat)
    • sf or sfc point layer with one feature
  • waypoints: Waypoints, in one of the same formats as for origins but possibly with more than one location, i.e.

    • 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
  • destination: Destination, in one of the same formats as for origins

  • mode: Travel mode, one of: "driving" (default), "transit", "walking", "bicycling"

  • arrival_time: The desired time of arrival for transit directions, as POSIXct

  • departure_time: The desired time of departure, as POSIXct

  • alternatives: Whether to return more than one alternative (logical, default is FALSE)

  • avoid: NA (default, means avoid nothing) 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!

  • transit_mode: Transit preferred mode, one or more of: "bus", "subway", "train" or "tram"

  • transit_routing_preference: Transit route preference. NA (default, means no preference) or one of: "less_walking" or "fewer_transfers"

  • language: The language in which to return directions. See https://developers.google.com/maps/faq#languagesupport for list of language codes.

  • key: Google APIs key

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

Returns

XML document with Google Maps Directions API response

Note

  • Use function mp_get_routes to extract sf line layer where each feature is a route
  • Use function mp_get_segments to extract sf line layer where each feature is a route segment

Examples

# Built-in reponse example library(xml2) doc = as_xml_document(response_directions_driving) r = mp_get_routes(doc) seg = mp_get_segments(doc) ## Not run: # Text file with API key key = readLines("~/key") # Using 'numeric' input doc = mp_directions( origin = c(34.81127, 31.89277), destination = c(34.781107, 32.085003), alternatives = TRUE, key = key ) # Using 'character' and 'sf' input library(sf) doc = mp_directions( origin = "Beer-Sheva", destination = c(34.781107, 32.085003) |> st_point() |> st_sfc(crs = 4326), alternatives = TRUE, key = key ) # Comparing traffic models doc = mp_directions( origin = "Beer-Sheva", destination = "Tel Aviv", departure_time = Sys.time() + as.difftime(1, units = "hours"), traffic_model = "best_guess", key = key ) mp_get_routes(doc)$duration_in_traffic_text doc = mp_directions( origin = "Beer-Sheva", destination = "Tel Aviv", departure_time = Sys.time() + as.difftime(1, units = "hours"), traffic_model = "optimistic", key = key ) mp_get_routes(doc)$duration_in_traffic_text doc = mp_directions( origin = "Beer-Sheva", destination = "Tel Aviv", departure_time = Sys.time() + as.difftime(1, units = "hours"), traffic_model = "pessimistic", key = key ) mp_get_routes(doc)$duration_in_traffic_text ## End(Not run)

References

https://developers.google.com/maps/documentation/directions/overview