Swiftpack.co - mapbox/mapbox-navigation-ios as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by mapbox.
mapbox/mapbox-navigation-ios v2.2.0
Turn-by-turn navigation logic and UI in Swift on iOS
⭐️ 733
🕓 6 days ago
iOS
.package(url: "https://github.com/mapbox/mapbox-navigation-ios.git", from: "v2.2.0")

Mapbox Navigation SDK for iOS

CircleCI codecov SPM compatible CocoaPods

Mapbox Navigation SDK

Mapbox Navigation gives you all the tools you need to add turn-by-turn navigation to your iOS application.

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

Documentation

Requirements

The Mapbox Navigation SDK and Core Navigation are compatible with applications written in Swift 5 in Xcode 12.4 and above. The Mapbox Navigation and Mapbox Core Navigation frameworks run on iOS 11.0 and above.

The Mapbox Navigation SDK is also available for Android.

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.2.0 as the minimum version requirement. Click Next.

To install the MapboxNavigation framework in another package rather than an application, run swift package init to create a Package.swift, then add the following dependency:

// Latest stable release
.package(name: "MapboxNavigation", url: "https://github.com/mapbox/mapbox-navigation-ios.git", from: "2.2.0")
// Latest prerelease
.package(name: "MapboxNavigation", url: "https://github.com/mapbox/mapbox-navigation-ios.git", .exact("2.2.0-rc.1"))

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 'MapboxCoreNavigation', '~> 2.2'
    pod 'MapboxNavigation', '~> 2.2'
    # Latest prerelease
    pod 'MapboxCoreNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :tag => 'v2.2.0-rc.1'
    pod 'MapboxNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :tag => 'v2.2.0-rc.1'
    
  3. Run pod repo update && pod install and open the resulting Xcode workspace.

Using Carthage

To install the MapboxNavigation framework using Carthage v0.38 or above:

  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. (Optional) Clear your Carthage caches:

    rm -rf ~/Library/Caches/carthage/ ~/Library/Caches/org.carthage.CarthageKit/binaries/{MapboxCommon-ios,MapboxNavigationNative,mapbox-ios-sdk-dynamic}
    
  3. Create a Cartfile with the following dependency:

    # Latest stable release
    github "mapbox/mapbox-navigation-ios" ~> 2.2
    # Latest prerelease
    github "mapbox/mapbox-navigation-ios" "v2.2.0-rc.1"
    
  4. Run carthage bootstrap --platform iOS --use-xcframeworks --cache-builds --use-netrc.

  5. Follow the rest of Carthage’s iOS integration instructions. Your application target’s Embed Frameworks build phase should include MapboxCoreNavigation.xcframework, MapboxNavigationNative.xcframework, and MapboxCommon.xcframework.

MapboxNavigation no longer supports Carthage as of v2.0.0-alpha.1. However, if you are building a user interface from scratch, you can install just the MapboxCoreNavigation framework using Carthage.

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.

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

Consult the API reference for further details.

Examples

The API reference includes example code for accomplishing common tasks. You can run these examples as part of the navigation-ios-examples project.

This repository also contains a testbed application that exercises a variety of navigation SDK features. See the contributing guide for instructions on installing and running this application.

Customization

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:

let navigationOptions = NavigationOptions(styles: [CustomStyle()])
NavigationViewController(for: routeResponse, routeIndex: routeIndex, routeOptions: routeOptions, navigationOptions: navigationOptions)

Starting from scratch

If your application needs something totally custom, such as a voice-only experience or an unconventional user interface, consult the Core Navigation installation guide.

Contributing

We welcome feedback and code contributions! Please see CONTRIBUTING.md for details.

License

The Mapbox Navigation SDK for iOS is released under the Mapbox Terms of Service. See LICENSE.md for details.

GitHub

link
Stars: 733
Last commit: 25 minutes ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v2.2.0
6 days ago

Changes since v2.1.1:

Packaging

Map

  • Added NavigationMapView.showsRestrictedAreasOnRoute property which allows displaying on UI parts of a route which lie on restricted roads. This overlay is customisable through NavigationMapView.routeRestrictedAreaColor, NavigationMapViewDelegate.navigationMapView(_:, restrictedAreasShapeFor:) and NavigationMapView.navigationMapView(_:, routeRestrictedAreasLineLayerWithIdentifier:, sourceIdentifier:) methods. (#3603)
  • Fixed an issue where changing color of NavigationMapView.maneuverArrowColor and NavigationMapView.maneuverArrowStrokeColor did not work. (#3633)
  • Fixed an issue where the route line blinks when NavigationMapView.showsRestrictedAreasOnRoute is turned on during active navigation, and when NavigationMapView.routeLineTracksTraversal is set to true. (#3654)
  • Updated RoutesPresentationStyle to support the ability to present routes based on custom camera options. (#3678)

Location tracking

  • Fixed an issue where customized .puck2D and .puck3D of NavigationMapView.userLocationStyle is not shown during simulated active navigation. (#3674)
  • Added the NavigationLocationProvider.didUpdateLocations(locations:) to send locations update to MapView and notify its LocationConsumer. (#3674)
  • When rerouting the user, if none of the new routes is very similar to the original route selection, the Router now follows the most optimal route, not a route that is only marginally similar. (#3664)
  • Exposed map matching status using new MapMatchingResult object which can be obtained through RouteController.routeControllerProgressDidChange and PassiveLocationManager.passiveLocationManagerDidUpdate notifications under mapMatchingResultKey. (#3669)
  • Fixed an issue where dismissing NavigationViewController could cause RouteController to crash or PassiveLocationProvider to behave like active turn-by-turn navigation. It is a programmer error to have more than one alive NavigationViewController, NavigationService or RouteController simultaneously. (#3652)

Banners and guidance instructions

  • In landscape orientation, NavigationViewController’s top and bottom banners take up less space, leaving more room for the map. (#3643)

CarPlay

  • Added the CarPlayManagerDelegate.carPlayManagerWillEndNavigation(_:byCanceling:) and CarPlayNavigationViewControllerDelegate.carPlayNavigationViewControllerWillDismiss(_:byCanceling:) methods for determining when CarPlayNavigationViewController is about to be dismissed. (#3676)

Other Changes

  • Extracted MapboxNavigationNative_Private usage into a type alias to fix a compilation in Xcode 12.4. (#3662)
  • Fixed a bug where tapping NavigationMapView while it transitions the camera to or from following/overview states would leave it in transitioning state, and thus blocking switching to either mode. (#3685)
  • Fixed an issue where building extrusion highlighting was covering other items located on the map like POI and destination/arrival icons. (#3692)

Documentation is available online or within Xcode.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics