I am working with Mapbox to navigate users to some places. There are times when I need to update the route on the fly, for example when users should take a detour to a waypoint I set.
My Route Calculation works well. I create a navigationViewController and give it a routeResponse, a routeIndex, routeOptions and navigationOptions, then start the navigation with that.
navigationViewController = NavigationViewController(for: myRouteResponse, routeIndex: 0, routeOptions: myNavigationRouteOptions, navigationOptions: NavigationOptions())
navigationViewController.modalPresentationStyle = .fullScreen
view.present(navigationViewController, animated: true, completion: nil)
As stated, this works well and starts the navigation as it should.
Later on, when some event triggers, I calculate new Waypoints and a new route with waypoints. I then want to update the currently running NavigationViewController with the new route, which I am trying to do as can be seen in the following snippet:
let routeOptions = NavigationRouteOptions(waypoints: waypoints, profileIdentifier: .automobileAvoidingTraffic)
print("Calculating RouteResponse...")
_ = Directions.shared.calculate(routeOptions, completionHandler: { (waypoints, result) in
switch result {
case .success(let response):
guard let route = response.routes?.first else { return }
self.newRoute = route
print("""
!!!
Route Calculation Result:
Found \(String(describing: response.routes?.count)) routes.
!!!
""")
print("Updating Navigation...")
self.navigationViewController = NavigationViewController(for: response, routeIndex: 0, routeOptions: self.myNavigationRouteOptions)
case .failure(let error): print(error)
}
})
Although this is not throwing any errors, it does not do what I expect either. I have sometimes managed to get two instances running, but never to replace the current one. How can that be done? I basically want to use the rerouting feature mapbox itself uses when users leave the recommended way, but with a completely new route. That shouldn't be all that hard, but I can't seem to get a response from the Mapbox team regarding this issue.