> For the complete documentation index, see [llms.txt](https://docs.mapbox.com/ios/ja/navigation/llms.txt)

# 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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/navigationcontroller/rerouting/) publisher publishes rerouting events and progress.

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/rerouteconfig/detectsreroute/) to `false`.

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

### Customization

The [`SessionController.currentSession.state`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/session/state-swift.enum/) 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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/navigationrouteoptions/) for reroute requests:

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/navigationcontroller/fasterroutes/) publisher publishes a new event when a faster route was detected and when it was applied.

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/routingconfig/fasterroutedetectionconfig/) to `nil`.

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/fasterroutedetectionconfig/proactivereroutinginterval/) to the [`CoreConfig`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/coreconfig/).

```swift
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.

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/navigationcontroller/continuousalternatives/) publisher publishes update events to the list of known continuous alternatives.

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/routingconfig/alternativeroutesdetectionconfig/) to `nil`.

```swift
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:

```swift
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`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/fasterroutedetectionconfig/proactivereroutinginterval/) to the [`CoreConfig`](https://docs.mapbox.com/ios/navigation/api/3.28.2/navigation/documentation/mapboxnavigationcore/coreconfig/). The value should be not less than 30 seconds.

```swift
let mapboxNavigationProvider = MapboxNavigationProvider(
    coreConfig: .init(
        routingConfig: .init(
            alternativeRoutesDetectionSettings: .init(
                refreshIntervalSeconds: 350.0
            )
        )
    )
)
```