Return an optimized route for a series of input coordinates

  profile = c("driving", "walking", "cycling", "driving-traffic"),
  output = "sf",
  source = c("any", "first"),
  destination = c("any", "last"),
  roundtrip = TRUE,
  annotations = NULL,
  approaches = NULL,
  bearings = NULL,
  distributions = NULL,
  language = NULL,
  overview = "simplified",
  radiuses = NULL,
  steps = NULL,
  access_token = NULL



An input dataset of class "sf", or a list of coordinate pairs of format c(longitude, latitude). Must be between 2 and 12 coordinate pairs.


One of "driving" (the default), "driving-traffic", "walking", or "cycling".


One of "sf" (the default), which returns an sf LINESTRING representing the route geometry, or "full", which returns the full request from the Directions API as a list.


One of "any" (the default) or "first". If "any" is specified, any of the input coordinates may be used as the starting point. If "first" is specified, the first coordinate will be used.


One of "any" (the default) or "last". If "any" is specified, any of the input coordinates may be used as the ending point. If "last" is specified, the last coordinate will be used.


If TRUE (the default), the route will start and end at the same point. roundtrip = FALSE only works when source is "first" and destination is "last". If FALSE is supplied here, the route will start at the first point in input_data and end at the last point.


A comma-separated string of additional route metadata, which may include duration, distance, speed, and congestion. Must be used with overview = "full".


A character string with semicolon-separated specifications for how to approach waypoints. Options include unrestricted and curb. Defaults to NULL which uses unrestricted for all waypoints.


A semicolon-delimited character string of bearings.


A semicolon-delimited character string of number pairs that specifies pick-up and drop-off locations. The first number indicates the index of the pick-up location, and the second number represents the index of the drop-off location.


The language of the returned instructions (defaults to English). Available language codes are found at Only available when steps = TRUE.


If left blank, defaults to 'simplified' for simplified geometry; the other option is 'full' which provides the most detailed geometry available.


A character string with semicolon-separated radii that specify the distance (in meters) to snap each input coordinate to the road network. Defaults to NULL.


If TRUE, returns the route object split up into route legs with step-by-step instructions included. If FALSE or NULL (the default), a single line geometry representing the full route will be returned.


Your Mapbox access token; which can be set with mb_access_token()


Either a list of two sf objects - one representing the waypoints, and one representing the route - or an R list representing the full optimization API response.


if (FALSE) {


to_visit <- data.frame(
  X = c(-0.209307, -0.185875, -0.216877, -0.233511, -0.234541),
  Y = c(5.556019, 5.58031, 5.582528, 5.566771, 5.550209)
) %>%
  st_as_sf(coords = c("X", "Y"), crs = 4326)

optimized_route <- mb_optimized_route(to_visit,
  profile = "driving-traffic"