A simple Expo wrapper for Mapbox's navigation SDK's on Android and iOS
This package relies on the installtion of @rnmapbox/maps
, so you'll have to install and setup as explained in their installtion instructions. The current version of Expo Mapbox Navigation package was developed and tested for Mapbox Maps version 11.7.0
, so that is the recommended version when setting up the maps package. It is possible that everything will work with a later version, but unlikely with a lower version. However, make sure you explcitly set the version with RNMapboxMapsVersion
because this package relies on this being set to work
Note
Make sure to follow the instructions carefully and include the proper token in the config plugin and to call the Mapbox.setAccessToken
function. You can be sure everything is good when you're able to render a map.
Run this command to add the package
npx expo install @youssefhenna/expo-mapbox-navigation
In your app.json
or app.js
, you'll need to add a plugin for the package under the plugins
entry with a valid mapbox access token (The one that starts with pk.
). For the mapbox maps version, you have to use the same version set in the setup steps of @rnmapbox/maps
.
"plugins": [
...other plugins
[
"@youssefhenna/expo-mapbox-navigation",
{
"accessToken": "<YOUR_TOKEN>",
"mapboxMapsVersion": "<MAPBOX_MAPS_VERSION>"
}
]
]
Additionally you need to enable useFrameworks
in your ios config. This can be achieved by using the expo-build-properties
package and plugin as follows:
"plugins": [
...other plugins
[
"expo-build-properties",
{
"ios": {
"useFrameworks": "static"
}
}
],
]
After getting location permissions you can render the navigation map as follows anywhere in the app
<MapboxNavigationView
style={{ flex: 1 }}
coordinates={coordinates} />
An array of {latitude: number, longitude: number}
objects. This creates a route passing through all the given coordinates. Requires at least 2 points for start and end destinations.
An array of indices representing which of the coordinates is to be considered a waypoint/destination. By default all coordinates are considered waypoints (and show a waypoint indicator), passing this prop allows choosing which of the coordinates are waypoints. At least the first and last element need to be included in the array for a route to render.
When enabled, uses Mapbox's Map Matching API to generate the route instead of the regular navigation route generation APIs. Enable this when you want a more explicit path determined by the coordinates. Mapbox Map Matching API
A string representation of a locale/language code that adjusts the Map labels, directions, and voice where possible. By default, uses the devices locale.
The profile to use for route generation. mapbox/driving-traffic
by default. See here for details
An array of road types and locations to exclude from the route. See the exclude
param here for details.
The style of the Mapbox map. See here for details.
Whether the navigation audio is initially muted.
Called when the user's progress on the route changes.
Provides an object in the nativeEvent
of type:
{
distanceRemaining: number;
distanceTraveled: number;
durationRemaining: number;
fractionTraveled: number;
}
Called when the cancel navigation button is clicked. The library does not handle cancellation automatically, use this callback to handle the cancellation behaviour.
Called when arrived at one of the given waypoints.
On Android ONLY provides an object in the nativeEvent
of type:
{
distanceRemaining: number;
distanceTraveled: number;
durationRemaining: number;
fractionTraveled: number;
}
Called when arrived at the final destination
Called when the route changes or reroutes
Called when the user goes off route
Called when routes are finished loading.
- Run
yarn
on the root directory cd example
- Run
yarn
on the example app - Run the app using
npx expo run:android
/npx expo run:ios
The Android implmentation uses the gradle dependencies and can easily be updated to newer versions.
iOS, however, bundles the Mapbox Navigation SDK into the expo module to be used. As of the date of writing this, the Mapbox Navigation SDK v3 does not support cocoapods and only supports Swift package manager. There is no obvious way on how one can include a package from the Swift package manager into an Expo module, so I opted to bundle the .xcframework
package instead.
Note
Cocoapods support is apparently comming soon. When that day comes, this process can be made much easier.
First make sure .netrc is configured with your mapbox credentials.
Clone https://github.com/mapbox/mapbox-navigation-ios from the desired version branch.
Use this modified Package.swift
and update the versions according to the cloned branch. Checksum for navNativeChecksum
will be incorrect. Run swift build -c release
which will fail and give you the correct checksum to update with. After updating, proceed with steps, do not re-run.
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let (navNativeVersion, navNativeChecksum) = ("314.0.0", "1494d2fb0c522b8279f4486c43d123b9a51417259979c54d00b42c9c03485eec")
let mapsVersion: Version = "11.5.0"
let commonVersion: Version = "24.5.0"
let mapboxApiDownloads = "https://api.mapbox.com/downloads/v2"
let package = Package(
name: "MapboxNavigation",
defaultLocalization: "en",
platforms: [.iOS(.v14)],
products: [
.library(
name: "MapboxNavigationUIKit",
targets: ["MapboxNavigationUIKit"]
),
.library(
name: "MapboxNavigationCore",
targets: ["MapboxNavigationCore"]
)
],
dependencies: [
.package(url: "https://github.com/mapbox/mapbox-maps-ios.git", exact: mapsVersion),
.package(url: "https://github.com/mapbox/mapbox-common-ios.git", exact: commonVersion),
.package(url: "https://github.com/mapbox/turf-swift.git", exact: "2.8.0")
],
targets: [
.target(
name: "MapboxNavigationUIKit",
dependencies: [
"MapboxNavigationCore",
],
exclude: ["Info.plist"],
resources: [
.copy("Resources/MBXInfo.plist"),
.copy("Resources/PrivacyInfo.xcprivacy")
]
),
.target(name: "_MapboxNavigationHelpers"),
.target(
name: "MapboxNavigationCore",
dependencies: [
.product(name: "MapboxCommon", package: "mapbox-common-ios"),
"MapboxNavigationNative",
"MapboxDirections",
"_MapboxNavigationHelpers",
.product(name: "MapboxMaps", package: "mapbox-maps-ios"),
],
resources: [
.process("Resources")
]
),
.target(
name: "MapboxDirections",
dependencies: [
.product(name: "Turf", package: "turf-swift"),
]
),
navNativeBinaryTarget(
name: "MapboxNavigationNative",
version: navNativeVersion,
checksum: navNativeChecksum
),
]
)
private func navNativeBinaryTarget(name: String, version: String, checksum: String) -> Target {
let url = "\(mapboxApiDownloads)/dash-native/releases/ios/packages/\(version)/MapboxNavigationNative.xcframework.zip"
return .binaryTarget(name: name, url: url, checksum: checksum)
}
Clone https://github.com/giginet/Scipio/ inside the cloned mapbox-navigation-ios repo. cd
inside that repo and run swift build -c release
.
While still in the Scipio repo run
swift run -c release scipio create ../ -f \
--platforms iOS \
--only-use-versions-from-resolved-file \
--enable-library-evolution \
--support-simulators \
--embed-debug-symbols \
--verbose