Overview

Off-Route Detection

A default off-route detection class, OffRouteDetector, is included in the Navigation SDK. This class uses our internal route-following library to run a series of calculations using the incoming Location and the current DirectionsRoute to determine if a user has strayed too far from the route.

The OffRouteListener can be used to handle reroute events. Listen for when a user goes off route using the OffRouteListener, fetch a new DirectionsRoute, and use MapboxNavigation#startNavigation(DirectionsRoute) with the fresh route to restart navigation.

navigation.addOffRouteListener(new OffRouteListener() {
@Override
public void userOffRoute(Location location) {
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(newOrigin)
.destination(destination)
.build().getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// Update MapboxNavigation here with new route
// MapboxNavigation#startNavigation
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
}
});
}
});

If you would like to provide your own implementation for OffRoute detection, you can replace the default OffRouteDetector class. To do this, create your own class that extends from OffRoute and set this new class using MapboxNavigation#setOffRouteEngine(OffRoute):

OffRoute myOffRouteEngine = new OffRoute() {
@Override
public boolean isUserOffRoute(Location location, RouteProgress routeProgress, MapboxNavigationOptions options) {
// User will never be off-route
return false;
}
};
// MapboxNavigation
navigation.setOffRouteEngine(myOffRouteEngine);