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 CocoaPods
To install Mapbox Navigation using CocoaPods:
Go to your Mapbox account dashboard and create an access token that has the
DOWNLOADS:READscope. 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.netrcin 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_TOKENwhere PRIVATE_MAPBOX_API_TOKEN is your Mapbox API token with the
DOWNLOADS:READscope.Create a Podfile with the following specification:
pod 'MapboxNavigation', '~> 1.2'Run
pod repo update && pod installand open the resulting Xcode workspace.
Using Carthage
Alternatively, to install Mapbox Navigation using Carthage:
Go to your Mapbox account dashboard and create an access token that has the
DOWNLOADS:READscope. 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.netrcin 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_TOKENwhere PRIVATE_MAPBOX_API_TOKEN is your Mapbox API token with the
DOWNLOADS:READscope.(Optional) Clear your Carthage caches:
rm -rf ~/Library/Caches/carthage/ ~/Library/Caches/org.carthage.CarthageKit/binaries/{MapboxAccounts,MapboxCommon-ios,MapboxNavigationNative,mapbox-ios-sdk-dynamic}Create a Cartfile with the following dependency:
github "mapbox/mapbox-navigation-ios" ~> 1.2
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
MGLMapboxAccessTokento 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
NSLocationWhenInUseUsageDescriptionto: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
audioandlocationvalues to theUIBackgroundModesarray 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 route = response.routes?.first, let strongSelf = self else {
return
}
// Pass the generated route to the the NavigationViewController
let viewController = NavigationViewController(for: route, 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. PassiveLocationManager is an optional alternative to CLLocationManager for use with any standalone MGLMapView 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 powered by 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 Notifications 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 1.2.0
Packaging
- Increased the minimum versions of
MapboxNavigationNativeto v29.0,MapboxCommonto v9.1.0 andMapboxDirectionsto v1.2. (#2694, #2770, #2781) - Installing MapboxCoreNavigation using CocoaPods no longer overrides the
EXCLUDED_ARCHSbuild setting of your application’s target. Installing MapboxNavigation still overrides this setting. (#2770) - Added a Ukrainian localization. (#2735)
Map
- Added the ability to customize the floating buttons in navigation view. The floating buttons could be edited with
NavigationViewController.floatingButtons, the position of the floating buttons could be edited withNavigationViewController.floatingButtonsPosition. (#2763) - Fixed an issue which was causing clear map button disappearance in the example app when selecting the route. (#2718)
- Fixed an issue where maneuver icon was not shown after selecting specific step. (#2728)
- Added the ability to style each route line differently using such delegate methods (#2719):
NavigationMapViewDelegate.navigationMapView(_:mainRouteStyleLayerWithIdentifier:source:)to style the main route.NavigationMapViewDelegate.navigationMapView(_:mainRouteCasingStyleLayerWithIdentifier:source:)to style the casing of the main route.NavigationMapViewDelegate.navigationMapView(_:alternativeRouteStyleLayerWithIdentifier:source:)to style alternative route.NavigationMapViewDelegate.navigationMapView(_:alternativeRouteCasingStyleLayerWithIdentifier:source:)to style the casing of alternative route.
- Fixed an issue where the route line periodically peeked out from behind the user puck even though
NavigationViewController.routeLineTracksTraversalwas enabled. (#2737) - Created the
UserHaloCourseViewsimilar toUserCourseViewfor approximate location on iOS 14 during the navigation to represent user location. Allow the switch betweenUserHaloCourseViewandUserCourseViewwhen precise mode is changed. (#2664)
Instruction banners
- Fixed an issue which was preventing the ability to customize the bottom banner height. (#2705)
- Fixed an issue which was preventing the ability to scroll between instructions cards on iOS 14 using workaround. (#2755)
- Fixed an instructions cards layout issue that arose when changing orientation (portrait to landscape). (#2755)
- Fixed swiping for right-to-left languages for the traditional top banner to be more intuitive. (#2755)
Location tracking
- Fixed potential crashes when using
PassiveLocationManagerorPassiveLocationDataSource. (#2694) - Fixed repeated rerouting when traveling alongside a freeway off-ramp. (#2694)
- Fixed repeated rerouting when starting a new leg while the user is too far from the new leg’s origin. (#2781)
RouteControllermore reliably detects when the user has gone off-route. (#2781)- Fixed an issue where
RouteControllersnapped the user’s location to the opposite side of a divided highway. (#2694) - Fixed an issue where
RouteControllergot stuck after making a U-turn. (#2694)
Other changes
- The user can now report feedback about an incorrect speed limit in the speed limit view. (#2725)
- Added the
RouteProgress.upcomingRouteAlertsproperty to track upcoming points along the route experiencing conditions that may require the user’s attention. TheUpcomingRouteAlertInfo.alertproperty contains one of the following types with more details about the alert:Incident,TunnelInfo,BorderCrossingInfo,TollCollection, andRestStop. (#2694) - Added a new
NavigationMapView.roadClassesWithOverriddenCongestionLevelsproperty. For any road class in it all route segments with anCongestionLevel.unknowntraffic congestion level and a matchingIntersection.outletMapboxStreetsRoadClasswill be replaced with theCongestionLevel.lowcongestion level. (#2741) - Added a new
RouteLeg.streetsRoadClassesproperty, which allows to get a collection ofMapboxStreetsRoadClassobjects for specificRouteLeg. (#2741) NavigationAnnotationwas made public to provide a way to detect annotations created byNavigationMapView. (#2769)
Install in Dash
Mapbox Navigation SDK for iOS Reference