Skip to main content

Off-route detection

A newer version of the Navigation SDK is available
This page uses v1.6.2 of the Mapbox Navigation SDK. A newer version of the SDK is available. Learn about the latest version, v2.20.2, in the Navigation SDK documentation.

The Navigation SDK detects when a device strayed from a route it was navigating. When a device is determined to be off route, the SDK provides notifications and takes actions to keep the user navigating.

Observer

The OffRouteObserver interface provides a boolean whenever the Navigation SDK is in turn-by-turn navigation and is off route. When the device is determined to be off route, the Navigation SDK will automatically request a new route.

Create the OffRouteObserver interface object:

val offRouteObserver = object : OffRouteObserver {
override fun onOffRouteStateChanged(offRoute: Boolean) {

}
}
Note
This interface doesn't work with free-drive mode.

Register the OffRouteObserver object with your already-instantiated MapboxNavigation object.

override fun onStart() {
super.onStart()
mapView.onStart()
mapboxNavigation.registerOffRouteObserver(offRouteObserver)
}

Don’t forget to unregister the OffRouteObserver interface:

override fun onStop() {
super.onStop()
mapView.onStop()
mapboxNavigation.unregisterOffRouteObserver(offRouteObserver)
}

Controller

To customize what conditions trigger off-route conditions, you can provide a custom RerouteController:

mapboxNavigation.setRerouteController(object : RerouteController {
...
})

or to disable off-route detection entirely, pass null:

mapboxNavigation.setRerouteController(null)

Reroute state observer

The controller produces reroute state updates that can be listened to with the RerouteStateObserver:

mapboxNavigation.getRerouteController()?.registerRerouteStateObserver(object : RerouteStateObserver {
...
})