The Directions control
Parameters
options
Objectoptions.styles
Array? Override default layer properties of the directions source. Documentation for each property are specified in the Mapbox GL Style Reference.options.accessToken
String Required unlessmapboxgl.accessToken
is set globally (optional, defaultnull
)options.api
String Override default routing endpoint url (optional, default"https://api.mapbox.com/directions/v5/"
)options.interactive
Boolean Enable/Disable mouse or touch interactivity from the plugin (optional, defaulttrue
)options.profile
String Routing profile to use. Options:mapbox/driving-traffic
,mapbox/driving
,mapbox/walking
,mapbox/cycling
(optional, default"mapbox/driving-traffic"
)options.alternatives
Boolean Whether to enable alternatives. (optional, defaultfalse
)options.congestion
Boolean Whether to enable congestion along the route line. (optional, defaultfalse
)options.unit
String Measurement system to be used in navigation instructions. Options:imperial
,metric
(optional, default"imperial"
)options.compile
Function Provide a custom function for generating instruction, compatible with osrm-text-instructions. (optional, defaultnull
)options.geocoder
Object? Accepts an object containing the query parameters as documented here.options.controls
Object?options.controls.inputs
Boolean Hide or display the inputs control. (optional, defaulttrue
)options.controls.instructions
Boolean Hide or display the instructions control. (optional, defaulttrue
)options.controls.profileSwitcher
Boolean Hide or display the default profile switch with options for traffic, driving, walking and cycling. (optional, defaulttrue
)
options.zoom
Number If no bbox exists from the geocoder result, the zoom you set here will be used in the flyTo. (optional, default16
)options.language
String The language of returned turn-by-turn text instructions. See supported languages : https://docs.mapbox.com/api/navigation/#instructions-languages (optional, default"en"
)options.placeholderOrigin
String If set, this text will appear as the placeholder attribute for the origin input element. (optional, default"Choose a starting place"
)options.placeholderDestination
String If set, this text will appear as the placeholder attribute for the destination input element. (optional, default"Choose destination"
)options.flyTo
Boolean If false, animating the map to a selected result is disabled. (optional, defaulttrue
)options.exclude
String Exclude certain road types from routing. The default is to not exclude anything. Search forexclude
inoptional parameters
: https://docs.mapbox.com/api/navigation/#retrieve-directions (optional, defaultnull
)options.routePadding
(number | PaddingOptions) Specify padding surrounding route. A single number of pixels or a PaddingOptions object. (optional, default80
)
Examples
var MapboxDirections = require('../src/index');
var directions = new MapboxDirections({
accessToken: 'YOUR-MAPBOX-ACCESS-TOKEN',
unit: 'metric',
profile: 'mapbox/cycling'
});
// add to your mapboxgl map
map.addControl(directions);
Returns MapboxDirections this
Removes the control from the map it has been added to. This is called by map.removeControl
,
which is the recommended method to remove controls.
Parameters
map
Returns Control this
Turn on or off interactivity
Parameters
state
Boolean sets interactivity based on a state oftrue
orfalse
.
Returns MapboxDirections this
Returns the origin of the current route.
Returns Object origin
Sets origin. Note: calling this method requires the map load event to have run.
Parameters
Returns MapboxDirections this
Returns the destination of the current route.
Returns Object destination
Sets destination. Note: calling this method requires the map load event to have run.
Parameters
Returns MapboxDirections this
Swap the origin and destination.
Returns MapboxDirections this
Add a waypoint to the route. Note: calling this method requires the map load event to have run.
Parameters
index
Number position waypoint should be placed in the waypoint arraywaypoint
(Array<number> | Point) can be a GeoJSON Point Feature or [lng, lat] coordinates.
Returns MapboxDirections this;
Change the waypoint at a given index in the route. Note: calling this method requires the map load event to have run.
Parameters
index
Number indexed position of the waypoint to updatewaypoint
(Array<number> | Point) can be a GeoJSON Point Feature or [lng, lat] coordinates.
Returns MapboxDirections this;
Remove a waypoint from the route.
Parameters
index
Number position in the waypoints array.
Returns MapboxDirections this;
Fetch all current waypoints in a route.
Returns Array waypoints
Removes all routes and waypoints from the map.
Returns MapboxDirections this;
Subscribe to events that happen within the plugin.
Parameters
type
String name of event. Available events and the data passed into their respective event objects are:- clear{ type: } Type is one of 'origin' or 'destination'
- loading
{ type: } Type is one of 'origin' or 'destination'
- profile
{ profile } Profile is one of 'driving', 'walking', or 'cycling'
- origin
{ feature } Fired when origin is set
- destination
{ feature } Fired when destination is set
- route
{ route } Fired when a route is updated
- error
{ error } Error as string
- loading
fn
Function function that's called when the event is emitted.
Returns MapboxDirections this;