Mapbox Navigation SDK for iOS
Mapbox Navigation gives you all the tools you need to add turn-by-turn navigation to your apps.
Get up and running in a few minutes with our drop-in turn-by-turn navigation NavigationViewController
, or build a completely custom turn-by-turn navigation app with our core components for routing and navigation.
Features
- Drop-in turn-by-turn navigation UI
- Automotive, cycling, and walking directions
- Traffic avoidance
- Maneuver announcements
- Text instructions
- Text to speech support via AVSpeechSynthesizer or Amazon Polly
- Automatic rerouting
- Snap to route
Requirements:
- >= Xcode 9
- Swift 4. If you’d like to use Swift 3.2, the last supported release is v0.10.1.
Installation
To install Mapbox Navigation using CocoaPods:
- Specify the following dependency in your Podfile:
ruby pod 'MapboxNavigation', '~> 0.12'
- Run
pod install
and open the resulting Xcode workspace.
Note, you may need to run pod repo update
before pod install
if your Cocoapods sources haven’t been updated in a while.
Alternatively, to install Mapbox Navigation using Carthage v0.19.0 or above:
Specify the following dependency in your Cartfile:
github "mapbox/mapbox-navigation-ios" ~> 0.12
Run
carthage update --platform iOS
to build just the iOS dependencies.Follow the rest of Carthage’s iOS integration instructions. Your application target’s Embedded Frameworks should include MapboxNavigation.framework and MapboxCoreNavigation.framework.
Running the example project
- Clone the repository or download the .zip file
- Run
carthage update --platform ios
to build just the iOS dependencies. - Open
MapboxNavigation.xcodeproj
. - Sign up or log in to your Mapbox account and grab a Mapbox Access Token.
- Open the Info.plist for either
Example-Swift
orExample-Objective-C
and paste your Mapbox Access Token intoMGLMapboxAccessToken
. (Alternatively, if you plan to use this project as the basis for a public project on GitHub, place the access token in a plain text file named.mapbox
ormapbox
in your home directory instead of adding it to Info.plist.) - Build and run the
Example-Swift
orExample-Objective-C
target.
Usage
import MapboxDirections
import MapboxNavigation
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")
let options = NavigationRouteOptions(waypoints: [origin, destination])
Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first else { return }
let viewController = NavigationViewController(for: route)
self.present(viewController, animated: true, completion: nil)
}
The example app is also a great resource for referencing various APIs.
Required Info.plist Keys
Mapbox Navigation requires a few additions to your Info.plist
. Be sure to sign up or log in to your Mapbox account and grab a Mapbox Access Token.
- Add a
MGLMapboxAccessToken
key and paste your Mapbox Access Token - Add a
NSLocationWhenInUseUsageDescription
key if you haven’t already - If you need voice guidance while your app is in the background, you’ll also need to add the
audio
andlocation
value to theUIBackgroundModes
array. You can also do this by navigating to theCapabilities
tab ->Background Modes
and enabling the following:Audio, AirPlay, and Picture in Picture
Location updates
Styling
You can customize the appearance in order to blend in with the rest of your app. Checkout DayStyle.swift
for all styleable elements.
class CustomStyle: DayStyle {
required init() {
super.init()
mapStyleURL = URL(string: "mapbox://styles/mapbox/satellite-streets-v9")!
styleType = .nightStyle
}
override func apply() {
super.apply()
BottomBannerView.appearance().backgroundColor = .orange
}
}
then initialize NavigationViewController
with your style or styles:
NavigationViewController(for: route, styles: [CustomStyle()])
NavigationViewController Delegate Methods
navigationViewController:didArriveAtDestination:
: Fired when the user arrives at their destination. You are responsible for dismissing the UI.navigationViewControllerDidCancelNavigation
: Fired when the user tapsCancel
. You are responsible for dismissing the UI.navigationViewController:shouldRerouteFromLocation:
: Fired when SDK detects that a user has gone off route. You can returnfalse
here to either prevent a reroute from occuring or if you want to rerequest an alternative route manually.navigationViewController:willRerouteFromLocation:
: Fired just before the SDK requests a new route.navigationViewController:didRerouteAlongRoute:
: Fired as soon as the SDK receives a new route.navigationViewController:didFailToRerouteWithError:
: Fired when SDK receives an error instead of a new route.
Building your own custom navigation UI
If you need additional flexibility, you can use the following building blocks to build your own custom navigation UI:
- Interactive map SDK for iOS and macOS
- Mapbox Studio
- Design custom maps with live traffic overlays
- MapboxDirections.swift (also compatible with macOS, tvOS, and watchOS)
- Automotive, cycling, and walking directions
- Traffic-influenced driving directions
- Mapbox Core Navigation (
MapboxCoreNavigation
module) (also compatible with watchOS)- Route controller
- Progress calculations
- Location snapping
- Guidance notifications
- Current progress along a route
- Departure and arrival notifications
- Upcoming maneuver notifications
- Rerouting notifications
- Geometry functions
- Distance formatter
- OSRM Text Instructions for Swift (also compatible with macOS, tvOS, and watchOS)
- Localized guidance instructions
Route Controller
RouteController
is given a route. Internally RouteController
matches the user’s current location to the route while looking at 3 principle pieces:
- Is the user on or off the route?
- How far along the step is the user?
- Does the user need to be alerted about an upcoming maneuver?
The library compares the user from the route and decides upon each one of these parameters and acts accordingly. The developer is told what is happening behind the scenes via notifications.
Guidance Notifications
This library relies heavily on notifications for letting the developer know when events have occurred.
.routeControllerProgressDidChange
- Emitted when the user moves along the route. Notification contains 3 keys:
RouteControllerProgressDidChangeNotificationProgressKey
-RouteProgress
- Current progress along routeRouteControllerProgressDidChangeNotificationLocationKey
-CLLocation
- Current locationRouteControllerProgressDidChangeNotificationSecondsRemainingOnStepKey
-Double
- Given users speed and location, this is the number of seconds left to the end of the step
.routeControllerDidPassSpokenInstructionPoint
- Emitted when user passes a point where instructions should be spoken. This indicates the user should be notified about the upcoming maneuver. You can find the instruction narrative in the
RouteProgress
object. Notification contains 1 key:RouteControllerDidPassSpokenInstructionPointRouteProgressKey
-RouteProgress
- Current progress along route
.routeControllerShouldReroute
- Emitted when the user is off the route and should be rerouted. Notification contains 1 key:
RouteControllerNotificationShouldRerouteKey
-CLLocation
- Last location of user
Looking for a more advanced use case? See our installation guide of MapboxCoreNavigation.
Language support
See languages.md for more information.
Contributing
We welcome feedback and code contributions! Please see CONTRIBUTING.md for details.
License
Mapbox Navigation SDK for iOS is released under the ISC License. See LICENSE.md for details.