Mapbox Navigation SDK for iOS
The Mapbox Navigation SDK gives you all the tools you need to add turn-by-turn navigation to your application. It takes just a few minutes to drop a full-fledged turn-by-turn navigation view controller into your application. Or use the Core Navigation framework directly to build something truly custom.
The Mapbox Navigation SDK and Core Navigation are compatible with applications written in Swift 5 in Xcode 10.2. The Mapbox Navigation and Mapbox Core Navigation frameworks run on iOS 10.0 and above.
Installation
Using Swift Package Manager
To install the MapboxNavigation framework in an application using Swift Package Manager:
Go to your Mapbox account dashboard and create an access token that has the
DOWNLOADS:READ
scope. PLEASE NOTE: This is not the same as your production Mapbox API token. Make sure to keep it private and do not insert it into any Info.plist file. Create a file named.netrc
in your home directory if it doesn’t already exist, then add the following lines to the end of the file:machine api.mapbox.com login mapbox password PRIVATE_MAPBOX_API_TOKEN
where PRIVATE_MAPBOX_API_TOKEN is your Mapbox API token with the
DOWNLOADS:READ
scope.In Xcode, go to File ‣ Swift Packages ‣ Add Package Dependency.
Enter
https://github.com/mapbox/mapbox-navigation-ios.git
as the package repository and click Next.Set Rules to Version, Up to Next Major, and enter
2.6.0
as the minimum version requirement. Click Next.
To install the MapboxCoreNavigation framework in another package rather than an application, run swift package init
to create a Package.swift, then add the following dependency:
// Latest prerelease
.package(name: "MapboxNavigation", url: "https://github.com/mapbox/mapbox-navigation-ios.git", from: "2.6.0")
Using CocoaPods
To install the MapboxNavigation framework using CocoaPods:
Go to your Mapbox account dashboard and create an access token that has the
DOWNLOADS:READ
scope. PLEASE NOTE: This is not the same as your production Mapbox API token. Make sure to keep it private and do not insert it into any Info.plist file. Create a file named.netrc
in your home directory if it doesn’t already exist, then add the following lines to the end of the file:machine api.mapbox.com login mapbox password PRIVATE_MAPBOX_API_TOKEN
where PRIVATE_MAPBOX_API_TOKEN is your Mapbox API token with the
DOWNLOADS:READ
scope.Create a Podfile with the following specification:
# Latest stable release pod 'MapboxNavigation', '~> 2.6' # Latest prerelease pod 'MapboxCoreNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :tag => 'v2.6.0' pod 'MapboxNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :tag => 'v2.6.0'
Run
pod repo update && pod install
and open the resulting Xcode workspace.
Configuration
Mapbox APIs and vector tiles require a Mapbox account and API access token. In the project editor, select the application target, then go to the Info tab. Under the “Custom iOS Target Properties” section, set
MBXAccessToken
to your access token. You can obtain an access token from the Mapbox account page. Usage of Mapbox APIs is billed together based on monthly active users (MAU) rather than individually by HTTP request.In order for the SDK to track the user’s location as they move along the route, set
NSLocationWhenInUseUsageDescription
to:Shows your location on the map and helps improve the map.
Users expect the SDK to continue to track the user’s location and deliver audible instructions even while a different application is visible or the device is locked. Go to the Signing & Capabilities tab. Under the Background Modes section, enable “Audio, AirPlay, and Picture in Picture” and “Location updates”. (Alternatively, add the
audio
andlocation
values to theUIBackgroundModes
array in the Info tab.)
Now import the relevant modules and present a new NavigationViewController
. You can also push to a navigation view controller from within a storyboard if your application’s UI is laid out in Interface Builder.
import MapboxDirections
import MapboxCoreNavigation
import MapboxNavigation
// Define two waypoints to travel between
let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House")
// Set options
let routeOptions = NavigationRouteOptions(waypoints: [origin, destination])
// Request a route using MapboxDirections
Directions.shared.calculate(routeOptions) { [weak self] (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let strongSelf = self else {
return
}
// Pass the generated route response to the the NavigationViewController
let viewController = NavigationViewController(for: response, routeIndex: 0, routeOptions: routeOptions)
viewController.modalPresentationStyle = .fullScreen
strongSelf.present(viewController, animated: true, completion: nil)
}
}
Starting points
This SDK is divided into two frameworks: the Mapbox Navigation framework (MapboxNavigation
) is the ready-made turn-by-turn navigation UI, while the Mapbox Core Navigation framework (MapboxCoreNavigation
) is responsible for the underlying navigation logic.
Mapbox Navigation
NavigationViewController
is the main class that encapsulates the entirety of the turn-by-turn navigation UI, orchestrating the map view, various UI elements, and the route controller. Your application would most likely present an instance of this class. The NavigationViewControllerDelegate
protocol allows your application to customize various aspects of the UI and react to location-related events as they occur.
NavigationMapView
is the map view at the center of the turn-by-turn navigation UI. You can also use this class independently of NavigationViewController
, for example to display a route preview map. The NavigationMapViewDelegate
protocol allows your application to customize various aspects of the map view’s appearance. PassiveLocationProvider
is an optional alternative to CLLocationManager
for use with any standalone MapView
or NavigationMapView
.
CarPlayManager
is the class that manages the CarPlay screen if your application is CarPlay-enabled. It provides a main map for browsing, a search interface that can be powered by the Mapbox Search SDK for iOS or MapboxGeocoder.swift, and a turn-by-turn navigation UI similar to the one provided by NavigationViewController
. Your UIApplicationDelegate
subclass can conform to the CarPlayManagerDelegate
protocol to manage handoffs between NavigationViewController
and the CarPlay device, as well as to customize some aspects of the CarPlay navigation experience. To take advantage of CarPlay functionality, your application must have a CarPlay navigation application entitlement and be built in Xcode 10 or above, and the user’s iPhone or iPad must have iOS 12 or above installed.
Core Navigation
MapboxNavigationService
is responsible for receiving user location updates and determining their relation to the route line. If you build a completely custom navigation UI, this is the class your code would interact with directly. The NavigationServiceDelegate
protocol allows your application to react to location-related events as they occur. Corresponding Notification
s from the NavigationService
‘s RouteController
are also posted to the shared NotificationCenter
. These notifications indicate the current state of the application in the form of a RouteProgress
object.
For further details, consult the guides and examples included with this API reference. If you have any questions, please see our help page. We welcome your bug reports, feature requests, and contributions.
Changes in version 2.6.0
Packaging
- MapboxNavigation now requires MapboxMaps v10.6.x. (#3955)
- MapboxCoreNavigation now requires MapboxNavigationNative v106.x. (#3955)
- MapboxCoreNavigation now requires MapboxDirections v2.6.x. (#3990)
Routing
- Added
Router.finishRouting()
method to finish routing session without dismissing related UI and logic components. (#3880) - Added reporting
AlternativeRoute
during navigation sessions. Subscribe toRouterDelegate.router(_:didUpdateAlternatives:removedAlternatives:)
orNotification.Name.routeControllerDidUpdateAlternatives
notification to receive updates on actual alternative routes list. Switching to the alternative route could be monitored by observingRouterDelegate.router(_:willTakeAlternativeRoute:at:)
andRouterDelegate.router(_:didTakeAlternativeRouteAt:)
methods orNotification.Name.routeControllerWillTakeAlternativeRoute
andNotification.Name.routeControllerDidTakeAlternativeRoute
notifications. To configure alternative routes detection, setupNavigationSettings.AlternativeRouteDetectionStrategy
before starting navigation. (#3833) - During turn-by-turn navigation, the map view displays any alternative routes that are currently available. Tapping an alternative route switches to it, just like in a standalone
NavigationMapView
. (#3850) - Implemented refreshing
RouteLeg.incidents
during a route refreshing. (#3931) - The
HistoryRecording.stopRecordingHistory(writingFileWith:)
method can be called safely from the main thread. (#3913) - Snapped locations are spaced more evenly when the application is connected to an external GPS or CarPlay device capable of high-frequency location updates and the user rounds a corner. (#3913)
- Fixed an issue where the user’s location would be snapped to the nearest road while the user moved around in a parking garage where the parking aisles have not been mapped in detail. (#3913)
- Fixed an issue where
RouteController
sometimes took too long to detect that the user went off the route after making a turn or taking an off-ramp. (#3913) - When proactive reroute happens, new departure voice instruction will not be pronounced anymore. (#3937)
- Fixed an issue where a departure instruction (e.g., “Head west”) was announced periodically in the middle of a trip due to proactive rerouting. (#3937)
Camera
- Fixed an issue where camera stops updating when
centerUpdatesAllowed
,zoomUpdatesAllowed
,bearingUpdatesAllowed
,pitchUpdatesAllowed
orpaddingUpdatesAllowed
disabled inOverviewCameraOptions
andFollowingCameraOptions
. (#3946)
Pricing
- When calling
MapboxNavigationService.start()
andMapboxNavigationService.stop()
billing session will be resumed and paused respectively. (#3928)
Map
- Added
NavigationMapView.tolerance
property to configure the tolerance of map sources for route line, maneuver arrow, and restricted areas. The property controls the level of simplification by specifying the maximum allowed distance between the original line point and the simplified point. A higher tolerance value results in higher simplification. (#3891) - Added the
layerPosition
parameter to theNavigationMapView.show(_:layerPosition:legIndex:)
method for controlling the position of the main route layer while presenting routes. (#3897) - Fixed an issue where camera kept receiving updates even after stopping
MapboxNavigationService
. (#3928) - Added
NavigationViewController.showsContinuousAlternatives
andCarPlayNavigationViewController.showsContinuousAlternatives
flags to toggle displaying alternative route’s lines during navigation session. UseNavigationMapView.show(continuousAlternatives:)
andNavigationMapView.removeContinuousAlternativesRoutes()
methods for fine control over displayed routes. (#3850)
CarPlay
- Fixed an issue where route shields disappeared when the user enters a tunnel. (#3882)
- The map automatically chooses the night style when “Always Show Dark Maps” is enabled in the Appearance section of Settings. (#3882)
- Renamed the
VisualInstruction.carPlayManeuverLabelAttributedText(bounds:shieldHeight:window:)
to theVisualInstruction.carPlayManeuverLabelAttributedText(bounds:shieldHeight:window:traitCollection:instructionLabelType:)
to have the ability to change color of the shield icons depending on provided trait collection. (#3882) - Fixed an issue where a
StyleManager
for CarPlay would update the appearance on both CarPlay and the iOS device simultaneously. (#3914) - Fixed an issue where an appearance change of CarPlay would cause an appearance change of iOS as well. Refer to
DayStyle
while implementing custom styles usingStyle
class. (#3922) - Fixed an issue where maneuver arrow wasn’t removed after passing visual instruction on CarPlay. (#3987)
- Fixed an issue where
LaneView
uses incorrect styling on CarPlay. (#3975)
Other changes
- Reduced peak memory usage.
- Fixed an issue when alternative route lines overlapped the main route line during navigation. (#3947)
- Fixed an issue when rerouting to the route which does not originate on current user location, route line and camera jumped to route origin. (#3943)
- All logs that Navigation SDK produces are now sent to the
MapboxCommon
framework. You can intercept these logs in your own code usingLogConfiguration.registerLogWriterBackend(forLogWriter:)
method fromMapboxCommon
framework. (#3944) - Fixed an issue where popped window doesn’t get updated in appearance when style changes on phone. (#3954)
- Fixed an issue where detailed feedback items don’t change color in different style. (#3954)
- Update method deprecation for
HistoryRecording
protocol. Static methods are now preferred over instance ones. (#3960) - Fixed a bug which sometimes caused the crash when new continuous alternatives arrive. (#3986)
- Fixed an issue where
UserPuckCourseView
is drawn in incorrect position if its location is outside of the bounds ofMapView
. (#3988)