Mapbox Navigation SDK for iOS

Mapbox Navigation SDK

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:

  1. 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.

  2. In Xcode, go to File ‣ Swift Packages ‣ Add Package Dependency.

  3. Enter https://github.com/mapbox/mapbox-navigation-ios.git as the package repository and click Next.

  4. Set Rules to Version, Up to Next Major, and enter 2.5.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.5.0")

Using CocoaPods

To install the MapboxNavigation framework using CocoaPods:

  1. 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.

  2. Create a Podfile with the following specification:

    # Latest stable release
    pod 'MapboxNavigation', '~> 2.5'
    # Latest prerelease
    pod 'MapboxCoreNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :tag => 'v2.5.0'
    pod 'MapboxNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :tag => 'v2.5.0'
    
  3. Run pod repo update && pod install and open the resulting Xcode workspace.

Configuration

  1. 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.

  2. 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.

  3. 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 and location values to the UIBackgroundModes 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 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 2.5.0

Packaging

User interface

  • Added the CarPlayManagerDelegate.carPlayManager(_:shouldUpdateNotificationFor:with:in:) and CarPlayManagerDelegate.carPlayManager(_:shouldShowNotificationFor:in:) methods for controlling the display of user notifications while the application is in the background in CarPlay. (#3828)
  • Fixed an issue where shields disappeared after the application returns to the foreground. (#3840)
  • The top banner now shows shields that are more consistent with the map and current road name label. (#3864)
  • Fixed an issue where exit views and generic shields in the top banner got outdated when the style changed during turn-by-turn navigation. (#3864)
  • Fixed an issue where the current road name label used a generic white circle instead of the correct shield to represent a state route in the United States. (#3870)
  • Lane guidance remains visible while the step table is visible. (#3805)
  • Removed a transparent gap that appeared between the top banner and step table if the user completed a maneuver while the step table was visible. (#3805)
  • Fixed an issue where a gray dashed line, which normally indicates a restricted-access road, appeared at the beginning of the route line even if there was no restriction. (#3811)
  • Lane guidance views will now be shown even when all lanes a valid for the current route. (#3903)
  • Fixed the crash of nonstop road shield updates when no valid road information contained in guidance instruction. (#3911)

User feedback

Location tracking

  • Added the Router.initialManeuverAvoidanceRadius property for adjusting the likelihood that the user will be rerouted onto a cross street very close to the current location. (#3754)
  • UserPuckCourseView no longer desaturates its color based on the age of the last location update. RouteController simulates location updates whenever Location Services is unable to receive real location updates. To ensure a steady stream of location updates outside of turn-by-turn navigation, install a PassiveLocationManager. (#3836)
  • Fixed an issue where UserPuckCourseView’s color desaturated during turn-by-turn navigation even as the location was being updated. (#3836)
  • Fixed an issue where the PassiveLocationManager(directions:systemLocationManager:eventsManagerType:userInfo:datasetProfileIdentifier:) initializer’s datasetProfileIdentifier argument was ignored. (#3867)
  • Fixed an issue where the user location was sometimes snapped to a parallel street just before it merges with the actual street. (#3862)
  • Fixed the possible crash after rerouting when routeLineTracksTraversal enabled. (#3896)

Routing

  • Renamed routingProvider to customRoutingProvider within the NavigationService class and Router protocol. You can continue to implement a RoutingProvider to customize how the router calculates a new route when rerouting, but the default value is now nil when using the built-in rerouting behavior. (#3754)
    • Renamed the NavigationService(routeResponse:routeIndex:routeOptions:routingProvider:credentials:locationSource:eventsManagerType:simulating:routerType:) initializer to NavigationService(routeResponse:routeIndex:routeOptions:customRoutingProvider:credentials:locationSource:eventsManagerType:simulating:routerType:).
    • Renamed the NavigationService(routeResponse:routeIndex:routeOptions:routingProvider:credentials:locationSource:eventsManagerType:simulating:routerType:) initializer to NavigationService(routeResponse:routeIndex:routeOptions:customRoutingProvider:credentials:locationSource:eventsManagerType:simulating:routerType:).
    • Replaced the NavigationService.routingProvider property with NavigationService.customRoutingProvider.
    • Renamed the Router(alongRouteAtIndex:in:options:routingProvider:dataSource:) initializer to Router(alongRouteAtIndex:in:options:customRoutingProvider:dataSource:)
    • Replaced the Router.routingProvider property with Router.customRoutingProvider.
  • Renamed the NavigationSettings.initialize(directions:tileStoreConfiguration:) method to NavigationSettings.initialize(directions:tileStoreConfiguration:routingProviderSource:). This method allows you to control whether the rerouting uses the network or offline routing data. (#3754, #3824)
  • Fixed an issue where a failure to calculate a route offline could result in a successful result being passed to a Directions.RouteCompletionHandler.

CarPlay

  • Fixed an issue where an active navigation using CarPlay application with route that contains multiple legs would cause a memory leak. (#3877)

Other Changes

  • During turn-by-turn navigation, incidents along the route are now refreshed periodically along with traffic congestion.
  • When the user passes a named toll collection point, the TollCollection object obtained through the Notification.Name.electronicHorizonDidPassRoadObject notification now has the TollCollection.name property set.