Swift Ruby Other
Switch branches/tags

README.md

Mapbox Navigation SDK for iOS

Build Status Carthage compatible CocoaPods

Mapbox Navigation SDK

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

Installation

To install Mapbox Navigation using CocoaPods:

  1. Specify the following dependency in your Podfile:
    pod 'MapboxNavigation', '~> 0.9'
  2. 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:

  1. Specify the following dependency in your Cartfile:

    github "mapbox/mapbox-navigation-ios" ~> 0.9
    
  2. Run carthage update --platform iOS to build just the iOS dependencies.

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

  1. Clone the repository or download the .zip file
  2. Run carthage update --platform ios to build just the iOS dependencies
  3. Open MapboxNavigation.xcodeproj
  4. Sign up or log in to your Mapbox account and grab a Mapbox Access Token
  5. Open the Info.plist for either Example-Swift or Example-Objective-C and paste your Mapbox Access Token into MGLMapboxAccessToken
  6. Build and run the Example-Swift or Example-Objective-C target

Usage

API reference

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 = RouteOptions(forNavigationWithWaypoints: [origin, destination])
options.routeShapeResolution = .full
options.includesSteps = true

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)
}

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.

  1. Add a MGLMapboxAccessToken key and paste your Mapbox Access Token
  2. Add a NSLocationWhenInUseUsageDescription key if you haven't already
  3. If you need voice guidance while your app is in the background, you'll also need to add the audio and location value to the UIBackgroundModes array. You can also do this by navigating to the Capabilities 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.

class CustomStyle: DayStyle {

    required init() {
        super.init()
        mapStyleURL = URL(string: "mapbox://styles/mapbox/satellite-streets-v9")!
        styleType = .nightStyle
    }

    override func apply() {
        super.apply()
        ManeuverView.appearance().backgroundColor = .darkGray
        RouteTableViewHeaderView.appearance().backgroundColor = .darkGray
    }
}

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 taps Cancel. You are responsible for dismissing the UI.
  • navigationViewController:shouldRerouteFromLocation:: Fired when SDK detects that a user has gone off route. You can return false 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

Installing Mapbox Core Navigation

Carthage compatible CocoaPods

To install Mapbox Core Navigation using CocoaPods:

  1. Specify the following dependency in your Podfile:

    pod 'MapboxCoreNavigation', '~> 0.7.0'
  2. 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 Core Navigation using Carthage v0.19.0 or above:

  1. Specify the following dependency in your Cartfile:

    github "mapbox/mapbox-navigation-ios" ~> 0.7.0
    
  2. Run carthage update --platform iOS to build just the iOS dependencies.

  3. Follow the rest of Carthage’s iOS integration instructions. Your application target’s Embedded Frameworks should include MapboxCoreNavigation.framework.

Route Controller

RouteController is given a route. Internally RouteController matches the user's current location to the route while looking at 3 principle pieces:

  1. Is the user on or off the route?
  2. How far along the step is the user?
  3. 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 NSNotification.

Guidance Notifications

This library relies heavily on NSNotifications 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 route
    • RouteControllerProgressDidChangeNotificationLocationKey - CLLocation - Current location
    • RouteControllerProgressDidChangeNotificationSecondsRemainingOnStepKey - Double - Given users speed and location, this is the number of seconds left to the end of the step

RouteControllerAlertLevelDidChange

  • Emitted when the alert level changes. This indicates the user should be notified about the upcoming maneuver. See [Alerts](#Alert levels). Notification contains 3 keys:
    • RouteControllerProgressDidChangeNotificationProgressKey - RouteProgress - Current progress along route
    • RouteControllerAlertLevelDidChangeNotificationDistanceToEndOfManeuverKey - CLLocationDistance - The users snapped distance to the end of the route.

RouteControllerShouldReroute

  • Emitted when the user is off the route and should be rerouted. Notification contains 1 key:
    • RouteControllerNotificationShouldRerouteKey - CLLocation - Last location of user

Alert levels

Alert levels indicate the type of announcement that should be given. The enum types available are:

  • none
  • depart - Emitted while departing origin
  • low - Emitted directly after completing the maneuver
  • medium - Emitted when the user has 70 seconds remaining on the route.
  • high - Emitted when the user has 15 seconds remaining on the route.
  • arrive - Emitted when the user arrives at destination

Rerouting

In the event of a reroute, it's necessary to update the current route with a new route. Once fetched, you can update the current route by:

var navigation: RouteController!
navigation.routeProgress = RouteProgress(route: newRoute)

License

Mapbox Navigation SDK for iOS is released under the ISC License. See LICENSE for details.