Off-route detection

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.

Navigation UI SDK

The Navigation UI SDK's RouteListener interface is the main way to interact with off route and rerouting logic.

  • allowRerouteFrom() will trigger in an off route scenario. Given the Point the user has gone off route, this listener can return true or false. Returning true will allow the SDK to proceed with the re-route process and fetch a new route with this given off route Point. Returning false will stop the re-route process and the user will continue without a new route in the direction they are traveling.
  • onOffRoute will triggered only if allowRerouteFrom() returns true. This serves as the official off route event and will continue the process to fetch a new route with the given off route Point.
  • onRerouteAlong will trigger when a new DirectionsRoute has been retrieved post off route. This is the new route the user will be following until another off route event is triggered.
  • onFailedReroute() will trigger if the request for a new DirectionsRoute fails.

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)
}
Was this page helpful?