Skip to main content

Route updates and rerouting

The Navigation SDK has built-in logic to make sure your users are on the best route as traffic conditions change or the user goes off route. This includes:

  • Off-route detection to get users back on track if they have strayed from the current route;
  • Route refresh to make sure the current route is still a viable route;
  • Alternative routes to offer users alternative routes as available.

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 automatically requests a redirected route by default. If the reroute request is successful, the new route will be delivered via RoutesObserver.

If you want to observe when the puck goes route, you can observe the off-route event using OffRouteObserver.

Observe off-route events

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.

To listen for changes in the off-route state, create the OffRouteObserver interface object:

val offRouteObserver = object : OffRouteObserver {
override fun onOffRouteStateChanged(offRoute: Boolean) {
// do something when the off route state changes
}
}
Note
This interface doesn't work in Free-drive because it requires to set route to Navigation SDK.

Then, register the OffRouteObserver object with your already-instantiated MapboxNavigation object.

    mapboxNavigation.registerOffRouteObserver(offRouteObserver)
}

Don’t forget to unregister the OffRouteObserver interface:

  	mapboxNavigation.unregisterOffRouteObserver(offRouteObserver)
}

Toggle off-route

You can disable or enable (by default) rerouting:

mapboxNavigation.setRerouteEnabled(false)

Observe changes to reroute states

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

mapboxNavigation.getRerouteController()?.registerRerouteStateObserver(object : RerouteStateObserver {
// do something when the reroute state changes
})

Route refresh

Traffic conditions along a route can change while you're navigating, causing the initial route information to be out of date. The Navigation SDK can refresh a route to make sure your users stay on track as they progress along a route.

Route refresh interval is configured with the RouteRefreshOptions.intervalMillis parameter. By default, the SDK will refresh the current route every five minutes, and if the refresh request is successful, an updated route will be delivered via RoutesObserver.

Note
The Navigation SDK will only refresh a route when RouteOptions.enableRefresh is true.

Alternative routes

Traffic conditions along a route can change while you're navigating, causing the initial route to no longer be as fast as it was when you departed. The Navigation SDK can provide alternative routes to make sure your users are on the best route.

The Navigation SDK is continuously looking for route alternatives and triggers request based on two internal conditions:

  1. Passing a fork point with an existing alternative route - the existing alternative becomes invalid and the Navigation SDK requests new alternative routes.
  2. There are no tracked alternative routes - the Navigation SDK requests alternatives with the interval RouteAlternativesOptions.intervalMillis until it gets successful response with valid alternatives, default interval is 5 minutes.

Alternative routes tracking

You do not need to track alternatives manually, the Navigation SDK tracks alternatives and brings online routes instead offline for you.

Updated route lines on the map

When a reroute event occurs it's important the route line on the map gets updated to communicate the route change to the user.

It's common to have a RoutesObserver registered in an activity or fragment. This observer gets called when the route has changed. You can update the route line in this observer to keep the line on the map in sync with the route used by core navigation.

Was this page helpful?