react-native-mapbox-turn-by-turn-navigation
React Native module for Mapbox Navigation SDK, providing full turn-by-turn navigation functionality.
- Turn-by-Turn Navigation: Provides a complete turn-by-turn navigation UI and experience.
- Customizable UI: The navigation view can be customized with your own colors and styles.
- Expo Support: Seamless integration with Expo projects using a config plugin.
- Nitro Modules: Built with Nitro Modules for efficient native module development.
To use this library, you need to install it along with react-native-nitro-modules:
npm install react-native-mapbox-turn-by-turn-navigation react-native-nitro-modulesNote:
react-native-nitro-modulesis a peer dependency and is required for this library to work.
This library supports Expo through a config plugin. The plugin automates the configuration process for both Android and iOS.
To enable automatic prebuild configuration, add the following to your app.json or app.config.js:
{
"expo": {
"ios": {
"infoPlist": {
"NSLocationWhenInUseUsageDescription": "This app requires access to your location for navigation purposes.",
"UIBackgroundModes": [
"audio",
"location"
]
}
},
"android": {
"permissions": [
"ACCESS_FINE_LOCATION",
"ACCESS_COARSE_LOCATION",
"FOREGROUND_SERVICE"
]
},
"plugins": [
"react-native-mapbox-turn-by-turn-navigation",
{
"mapboxPublicToken": "<YOUR_MAPBOX_PUBLIC_TOKEN>",
"mapboxDownloadsToken": "<YOUR_MAPBOX_DOWNLOADS_TOKEN>"
}
]
}
}This will automatically configure the necessary settings for Mapbox integration when you run npx expo prebuild.
-
Place your secret token in a .netrc file in your OS root directory.
machine api.mapbox.com login mapbox password <INSERT SECRET TOKEN> -
Install pods
cd ios && pod install -
Place your public token in your Xcode project's
Info.plistand add aMBXAccessTokenkey whose value is your public access token. -
Add the
UIBackgroundModeskey toInfo.plistwithaudioandlocationif it is not already present. This will allow your app to deliver audible instructions while it is in the background or the device is locked.<key>UIBackgroundModes</key> <array> <string>audio</string> <string>location</string> </array> -
Add NSLocationWhenInUseUsageDescription to
Info.plistwith a description of why your app needs access to the user's location.<key>NSLocationWhenInUseUsageDescription</key> <string>This app requires access to your location for navigation purposes.</string>
-
Place your secret token in your android app's top level
gradle.propertiesor«USER_HOME»/.gradle/gradle.propertiesfileMAPBOX_DOWNLOADS_TOKEN=<YOUR_MAPBOX_DOWNLOADS_TOKEN> -
Open up your project-level
build.gradlefile. Declare the Mapbox Downloads API'sreleases/mavenendpoint in the allprojectsrepositoriesblock.allprojects { repositories { maven { url 'https://api.mapbox.com/downloads/v2/releases/maven' authentication { basic(BasicAuthentication) } credentials { // Do not change the username below. // This should always be `mapbox` (not your username). username = "mapbox" // Use the secret token you stored in gradle.properties as the password password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: "" } } } }
-
Add Resources
To do so create a new string resource file in your app module(e.g. app/src/main/res/values/mapbox_access_token.xml)with your public Mapbox API token:<?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools"> <string name="mapbox_access_token" translatable="false" tools:ignore="UnusedResources">YOUR_MAPBOX_ACCESS_TOKEN</string> </resources>
-
Add the following permissions to your
AndroidManifest.xmlfile:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
For more details installation you can read the Official docs of Mapbox.
Here's a basic example of how to use the navigation view in your component:
import { MapboxTurnByTurnNavigationView } from "react-native-mapbox-turn-by-turn-navigation";
// ...
<MapboxTurnByTurnNavigationView
style={{ flex: 1 }}
origin={{
longitude: 5.058566,
latitude: 51.560985,
}}
destination={{
longitude: 5.082325,
latitude: 51.558588,
}}
/>Please refer to the example project for more detailed usage and configuration options.
For detailed documentation please refer to the docs directory.
Contributions are welcome! Please see the contributing guide to learn how to contribute to the repository and the development workflow.
This project is licensed under the MIT License - see the LICENSE file for details.