NavigationServiceDelegate
public protocol NavigationServiceDelegate : AnyObject, UnimplementedLogging
A navigation service delegate interacts with one or more NavigationService instances (such as MapboxNavigationService objects) during turn-by-turn navigation. This protocol is the main way that your application can synchronize its state with the SDK’s location-related functionality. Each of the protocol’s methods is optional.
As the user progresses along a route, a navigation service informs its delegate about significant events as they occur, and the delegate has opportunities to influence the route and its presentation. For example, when the navigation service reports that the user has arrived at the destination, your delegate implementation could present information about the destination. It could also customize individual visual or spoken instructions along the route by returning modified instruction objects.
Assign a NavigationServiceDelegate instance to the NavigationService.delegate property of the navigation service before you start the service.
The RouterDelegate protocol defines corresponding methods so that a Router instance can interact with an object that is both a router delegate and a navigation service, which in turn interacts with a navigation service delegate. Additionally, several location-related methods in this protocol have corresponding methods in the NavigationViewControllerDelegate protocol, which can be convenient if you are using the navigation service in conjunction with a NavigationViewController. Normally, you would either implement methods in NavigationServiceDelegate or NavigationViewControllerDelegate but not RouterDelegate.
Seealso
NavigationViewControllerDelegateSeealso
RouterDelegate-
navigationService(_:shouldRerouteFrom:)Default implementationReturns whether the navigation service should be allowed to calculate a new route.
If implemented, this method is called as soon as the navigation service detects that the user is off the predetermined route. Implement this method to conditionally prevent rerouting. If this method returns
true,navigationService(_:willRerouteFrom:)will be called immediately afterwards.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, shouldRerouteFrom location: CLLocation) -> BoolParameters
serviceThe navigation service that has detected the need to calculate a new route.
locationThe user’s current location.
Return Value
True to allow the navigation service to calculate a new route; false to keep tracking the current route.
-
navigationService(_:shouldProactivelyRerouteFrom:to:completion:)Default implementationAsks permission to proceed with found proactive reroute and apply it as main route.
If implemented, this method is called as soon as the navigation service detects route faster than the current one. This only happens if
Router.reroutesProactivelyis set totrue(default). Calling providedcompletionresults in new route to be set, without triggering usual rerouting delegate methods.Default Implementation
Declaration
Swift
func navigationService(_ service: NavigationService, shouldProactivelyRerouteFrom location: CLLocation, to route: Route, completion: @escaping () -> Void)Parameters
serviceThe navigation service that has detected faster new route
locationThe user’s current location.
routeThe route found.
completionCompletion to be called to allow the navigation service to apply a new route; Ignoring calling the completion will ignore the faster route aswell.
-
navigationService(_:willRerouteFrom:)Default implementationCalled immediately before the navigation service calculates a new route.
This method is called after
navigationService(_:shouldRerouteFrom:)is called, simultaneously with theNotification.Name.routeControllerWillReroutenotification being posted, and beforenavigationService(_:modifiedOptionsForReroute:)is called.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, willRerouteFrom location: CLLocation)Parameters
serviceThe navigation service that will calculate a new route.
locationThe user’s current location.
-
navigationService(_:modifiedOptionsForReroute:)Default implementationWhen reroute is happening, navigation service suggests to customize the
RouteOptionsused to calculate new route.This method is called after
navigationService(_:willRerouteFrom:)is called, and beforenavigationService(_:didRerouteAlong:)is called. This method is not called on proactive rerouting.Default implementation does no modifications.
Default Implementation
Declaration
Swift
func navigationService(_ service: NavigationService, modifiedOptionsForReroute options: RouteOptions) -> RouteOptionsParameters
serviceThe navigation service that will calculate a new route.
optionsOriginal
RouteOptions.Return Value
Modified
RouteOptions. -
navigationService(_:didUpdateAlternatives:removedAlternatives:)Default implementationCalled when navigation service has detected a change in alternative routes list.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didUpdateAlternatives updatedAlternatives: [AlternativeRoute], removedAlternatives: [AlternativeRoute])Parameters
serviceThe navigation service reporting an update.
updatedAlternativesArray of actual alternative routes.
removedAlternativesArray of alternative routes which are no longer actual.
-
navigationService(_:didFailToUpdateAlternatives:)Default implementationCalled when navigation service has failed to change alternative routes list.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didFailToUpdateAlternatives error: AlternativeRouteError)Parameters
serviceThe navigation service reporting an update.
errorAn error occured.
-
navigationService(_:didSwitchToCoincidentOnlineRoute:)Default implementationCalled when navigation service has automatically switched to the coincide online route.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didSwitchToCoincidentOnlineRoute coincideRoute: Route)Parameters
serviceThe navigation service reporting an update.
coincideRouteA route taken.
-
navigationService(_:willTakeAlternativeRoute:at:)Default implementationCalled when navigation service has detected user taking an alternative route.
This method is called before updating main route.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, willTakeAlternativeRoute route: Route, at location: CLLocation?)Parameters
serviceThe navigation service that has detected turning to the alternative.
routeThe alternative route which will be taken as new main.
locationThe user’s current location.
-
navigationService(_:didTakeAlternativeRouteAt:)Default implementationCalled when navigation service has finished switching to an alternative route
This method is called after
navigationService(_:willTakeAlternativeRoute:)Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didTakeAlternativeRouteAt location: CLLocation?)Parameters
serviceThe navigation service that switched to the alternative.
locationThe user’s current location.
-
navigationService(_:didFailToTakeAlternativeRouteAt:)Default implementationCalled when navigation service has failed to take an alternative route.
This method is called after
navigationService(_:willTakeAlternativeRoute:).This call would indicate that something went wrong during setting new main route.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didFailToTakeAlternativeRouteAt location: CLLocation?)Parameters
serviceThe navigation service which tried to switch to the alternative.
locationThe user’s current location.
-
navigationService(_:shouldDiscard:)Default implementationCalled when a location has been identified as unqualified to navigate on.
See
CLLocation.isQualifiedfor more information about what qualifies a location.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, shouldDiscard location: CLLocation) -> BoolParameters
serviceThe navigation service that discarded the location.
locationThe location that will be discarded.
Return Value
If
true, the location is discarded and theNavigationServicewill not consider it. Iffalse, the location will not be thrown out. -
navigationService(_:didRerouteAlong:at:proactive:)Default implementationCalled immediately after the navigation service receives a new route.
This method is called after
navigationService(_:modifiedOptionsForReroute:)and simultaneously with theNotification.Name.routeControllerDidReroutenotification being posted.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didRerouteAlong route: Route, at location: CLLocation?, proactive: Bool)Parameters
serviceThe navigation service that has calculated a new route.
routeThe new route.
-
navigationService(_:didFailToRerouteWith:)Default implementationCalled when the navigation service fails to receive a new route.
This method is called after
navigationService(_:modifiedOptionsForReroute:)and simultaneously with theNotification.Name.routeControllerDidFailToReroutenotification being posted.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didFailToRerouteWith error: Error)Parameters
serviceThe navigation service that has calculated a new route.
errorAn error raised during the process of obtaining a new route.
-
navigationService(_:didRefresh:)Default implementationCalled immediately after the navigation service refreshes the route.
This method is called simultaneously with the
Notification.Name.routeControllerDidRefreshRoutenotification being posted.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didRefresh routeProgress: RouteProgress)Parameters
serviceThe navigation service that has refreshed the route.
routeProgressThe route progress updated with the refreshed route.
-
navigationService(_:didUpdate:with:rawLocation:)Default implementationCalled when the navigation service updates the route progress model.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didUpdate progress: RouteProgress, with location: CLLocation, rawLocation: CLLocation)Parameters
serviceThe navigation service that received the new locations.
progressthe RouteProgress model that was updated.
locationthe guaranteed location, possibly snapped, associated with the progress update.
rawLocationthe raw location, from the location manager, associated with the progress update.
-
navigationService(_:shouldPreventReroutesWhenArrivingAt:)Default implementationCalled when the navigation service arrives at a waypoint.
You can implement this method to allow the navigation service to continue check and reroute the user if needed. By default, the user will not be rerouted when arriving at a waypoint.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, shouldPreventReroutesWhenArrivingAt waypoint: Waypoint) -> BoolParameters
serviceThe navigation service that has arrived at a waypoint.
waypointThe waypoint that the controller has arrived at.
Return Value
True to prevent the navigation service from checking if the user should be rerouted.
-
navigationService(_:willArriveAt:after:distance:)Default implementationCalled as the navigation service approaches a waypoint.
This message is sent, once per progress update, as the user is approaching a waypoint. You can use this to cue UI, to do network pre-loading, etc.
Important
This method will likely be called several times as you approach a destination. If only one consumption of this method is desired, then usage of an internal flag is recommended.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, willArriveAt waypoint: Waypoint, after remainingTimeInterval: TimeInterval, distance: CLLocationDistance)Parameters
serviceThe Navigation service that is detecting the destination approach.
waypointThe waypoint that the service is arriving at.
remainingTimeIntervalThe estimated number of seconds until arrival.
distanceThe current distance from the waypoint, in meters.
-
navigationService(_:didArriveAt:)Default implementationCalled when the navigation service arrives at a waypoint.
You can implement this method to prevent the navigation service from automatically advancing to the next leg. For example, you can and show an interstitial sheet upon arrival and pause navigation by returning
false, then continue the route when the user dismisses the sheet. If this method is unimplemented, the navigation service automatically advances to the next leg when arriving at a waypoint.Postcondition
If you return false, you must manually advance to the next leg usingRouter.advanceLegIndex(completionHandler:)method.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didArriveAt waypoint: Waypoint) -> BoolParameters
serviceThe navigation service that has arrived at a waypoint.
waypointThe waypoint that the controller has arrived at.
Return Value
True to advance to the next leg, if any, or false to remain on the completed leg.
-
navigationService(_:didPassVisualInstructionPoint:routeProgress:)Default implementationCalled when the navigation service detects that the user has passed a point at which an instruction should be displayed.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didPassVisualInstructionPoint instruction: VisualInstructionBanner, routeProgress: RouteProgress)Parameters
serviceThe navigation service that passed the instruction point.
instructionThe instruction to be presented.
routeProgressThe route progress object that the navigation service is updating.
-
navigationService(_:didPassSpokenInstructionPoint:routeProgress:)Default implementationCalled when the navigation service detects that the user has passed a point at which an instruction should be spoken.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didPassSpokenInstructionPoint instruction: SpokenInstruction, routeProgress: RouteProgress)Parameters
serviceThe navigation service that passed the instruction point.
instructionThe instruction to be spoken.
routeProgressThe route progress object that the navigation service is updating.
-
navigationServiceDidChangeAuthorization(_:didChangeAuthorizationFor:)Default implementationCalled when the location manager’s accuracy authorization changed.
You can implement this method to allow the navigation service to check if the user changed accuracy authorization, especially if reducedAccuracy is enabled. This method is only relevant for iOS 14 and above.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationServiceDidChangeAuthorization(_ service: NavigationService, didChangeAuthorizationFor locationManager: CLLocationManager)Parameters
serviceThe navigation service that will alert that user that reducedAccuracy is enabled.
managerThe location manager.
-
navigationServiceShouldDisableBatteryMonitoring(_:)Default implementationCalled when the navigation service will disable battery monitoring.
Implementing this method will allow developers to change whether battery monitoring is disabled when
NavigationServiceis deinited.Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationServiceShouldDisableBatteryMonitoring(_ service: NavigationService) -> BoolParameters
serviceThe navigation service that will change the state of battery monitoring.
Return Value
A bool indicating whether to disable battery monitoring when the RouteController is deinited.
-
navigationService(_:willBeginSimulating:becauseOf:)Default implementationCalled when the navigation service is about to begin location simulation.
Implementing this method will allow developers to react when “poor GPS” location-simulation is about to start, possibly to show a “Poor GPS” banner in the UI.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, willBeginSimulating progress: RouteProgress, becauseOf reason: SimulationIntent)Parameters
serviceThe navigation service that will simulate the routes’ progress.
progressthe current RouteProgress model.
reasonThe reason the simulation will be initiated. Either manual or poorGPS.
-
navigationService(_:didBeginSimulating:becauseOf:)Default implementationCalled after the navigation service begins location simulation.
Implementing this method will allow developers to react when “poor GPS” location-simulation has started, possibly to show a “Poor GPS” banner in the UI.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didBeginSimulating progress: RouteProgress, becauseOf reason: SimulationIntent)Parameters
serviceThe navigation service that is simulating the routes’ progress.
progressthe current RouteProgress model.
reasonThe reason the simulation has been initiated. Either manual or poorGPS.
-
navigationService(_:willEndSimulating:becauseOf:)Default implementationCalled when the navigation service is about to end location simulation.
Implementing this method will allow developers to react when “poor GPS” location-simulation is about to end, possibly to hide a “Poor GPS” banner in the UI.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, willEndSimulating progress: RouteProgress, becauseOf reason: SimulationIntent)Parameters
serviceThe navigation service that is simulating the routes’ progress.
progressthe current RouteProgress model.
reasonThe reason the simulation was initiated. Either manual or poorGPS.
-
navigationService(_:didEndSimulating:becauseOf:)Default implementationCalled after the navigation service ends location simulation.
Implementing this method will allow developers to react when “poor GPS” location-simulation has ended, possibly to hide a “Poor GPS” banner in the UI.
Default Implementation
UnimplementedLoggingprints a warning to standard output the first time this method is called.Declaration
Swift
func navigationService(_ service: NavigationService, didEndSimulating progress: RouteProgress, becauseOf reason: SimulationIntent)Parameters
serviceThe navigation service that was simulating the routes’ progress.
progressthe current RouteProgress model.
reasonThe reason the simulation was initiated. Either manual or poorGPS.
Install in Dash
NavigationServiceDelegate Protocol Reference