Skip to main content

Rerouting

Rerouting is when a new route is generated after the user's progress along a route has already begun. There are a few reasons rerouting happens including the user going off-route and the Navigation SDK determining there is a faster route to the next waypoint from the user's current location.

Off-route detection

The Navigation SDK provides information about whether a user's device is on the route that was generated. If a user is off-route, you can provide additional instructions to the user and generate a new route.

Subscription

By default, if a user is off-route, a new route is generated from their current location to the next waypoint.

The rerouting publisher publishes rerouting events and progress.

let navigation = mapboxNavigationProvider.mapboxNavigation.navigation()
navigation.rerouting
.filter { $0.event is ReroutingStatus.Events.Fetched }
.sink { _ in
print("reroute was applied")
}.store(in: &lifetimeSubscriptions)
  1. ReroutingStatus.Events.FetchingRoute is posted after the SDK detects the need for a reroute, but before receiving the new route.
  2. Then ReroutingStatus.Events.Fetched is posted once you receive the new route.

You can use these events to customize built-in behavior and synchronize your application behavior with what's happening with navigation logic.

Opting out

You can opt out of off route detection by setting rerouteConfig.detectsReroute to false.

var coreConfig = CoreConfig()
coreConfig.routingConfig.rerouteConfig.detectsReroute = false
let mapboxNavigationProvider = MapboxNavigationProvider(coreConfig: coreConfig)

Customization

The SessionController.currentSession.state allows you to understand if a user is on or off the current route by comparing state == .activeGuidance(.offRoute).

It is possible to customize NavigationRouteOptions for reroute requests:

coreConfig.routingConfig.rerouteConfig.optionsCustomization = .init { options in
var newOptions = options
// possible customizations
newOptions.initialManeuverAvoidanceRadius = 50.0
return newOptions
}

Faster route detection

By default, the Navigation SDK checks for a faster route every two minutes by making a request to the Direction API and comparing the response to the current route.

Subscription

The fasterRoutes publisher publishes a new event when a faster route was detected and when it was applied.

let navigation = mapboxNavigationProvider.mapboxNavigation.navigation()
navigation.fasterRoutes
.filter { $0.event is FasterRoutesStatus.Events.Detected }
.sink { _ in
print("faster route were detected")
}.store(in: &lifetimeSubscriptions)

Opting out

You can opt out of faster route detection by setting routingConfig.fasterRouteDetectionConfig to nil.

var coreConfig = CoreConfig()
coreConfig.routingConfig.fasterRouteDetectionConfig = nil
let mapboxNavigationProvider = MapboxNavigationProvider(coreConfig: coreConfig)

Customization

You can customize the interval at which to check for a faster route by setting the FasterRouteDetectionConfig.proactiveReroutingInterval to the CoreConfig.

let mapboxNavigationProvider = MapboxNavigationProvider(
coreConfig: .init(
routingConfig: .init(
fasterRouteDetectionSettings: .init(
proactiveReroutingInterval: 60.0
)
)
)
)

You can determine if the faster route should be accepted. By default, all the faster routes are accepted automatically.

var coreConfig = CoreConfig()
coreConfig.routingConfig.fasterRouteDetectionConfig?.fasterRouteApproval = .manually(
{ (location, navigationRoute) in
// detect if new route should be applied
return true
}
)
let mapboxNavigationProvider = MapboxNavigationProvider(coreConfig: coreConfig)

Alternative routes detection

By default, the Navigation SDK checks for alternative routes every five minutes. The SDK also checks if some alternatives already exist, and if so, it'll update the alternatives only after the deviation point.

Subscription

The continuousAlternatives publisher publishes update events to the list of known continuous alternatives.

let navigation = mapboxNavigationProvider.mapboxNavigation.navigation()
navigation.continuousAlternatives
.compactMap { $0.event as? AlternativesStatus.Events.Updated }
.sink { event in
print("new alternatives were detected \(event.actualAlternativeRoutes)")
// you can
}.store(in: &lifetimeSubscriptions)

Opting out

You can opt out of alternative routes detection by setting routingConfig.alternativeRoutesDetectionConfig to nil.

var coreConfig = CoreConfig()
coreConfig.routingConfig.alternativeRoutesDetectionConfig = nil
let mapboxNavigationProvider = MapboxNavigationProvider(coreConfig: coreConfig)

Customization

You can customize what kind of alternative routes will be applied, for example you can allow only faster alternative routes to be applied:

var coreConfig = CoreConfig()
coreConfig.routingConfig.alternativeRoutesDetectionConfig?.acceptionPolicy = .fasterRoutes
let mapboxNavigationProvider = MapboxNavigationProvider(coreConfig: coreConfig)

You can also customize the interval at which to check for a refreshed alternative route by setting the AlternativeRoutesDetectionConfig.refreshIntervalSeconds to the CoreConfig. The value should be not less than 30 seconds.

let mapboxNavigationProvider = MapboxNavigationProvider(
coreConfig: .init(
routingConfig: .init(
alternativeRoutesDetectionSettings: .init(
refreshIntervalSeconds: 350.0
)
)
)
)
Was this page helpful?