Navigation SDK for Android
Beta
Beta

The Navigation SDK for Android v1.0.0 is in public beta and is subject to changes.

Navigation SDK for Android
Beta

Current version: v1.0.0-rc.8 View changelog

  • Flexible modularization
  • Free-drive mode
  • Accurate positioning
  • Scaled pricing with no commitments

The Navigation SDK for Android allows you to build a complete navigation experience with the power of the Mapbox Directions API. The Navigation SDK provides a collection of features that are critical when building navigation projects, including:

  • Navigation routes
  • Accurate device location updates
  • Voice instruction announcements
  • Real-time user progress to their destination
  • Off-route detection

Install the Navigation SDK

You'll need to add the Navigation SDK as a dependency before developing your project.

Migrating from the current Navigation SDK

Read Mapbox's migration guide on GitHub if your project is using the current stable "legacy" Mapbox Navigation SDK for Android and you want to transition it to this beta version of the Navigation SDK for Android.

Including the Maps SDK

The core Navigation SDK doesn't include the Mapbox Maps SDK for Android, so if you're planning to display navigation information on top of a map, you'll need to add the Mapbox Maps SDK as a dependency before developing your project. Read the Maps SDK installation docs to get started.

Configure credentials

Before installing the SDK, you will need to gather the appropriate credentials. The SDK requires two pieces of sensitive information from your Mapbox account. If you don't have a Mapbox account: sign up and navigate to your Account page. You'll need:

  • A public access token: From your account's tokens page, you can either copy your default public token or click the Create a token button to create a new public token.
  • A secret access token with the Downloads:Read scope.
    1. From your account's tokens page, click the Create a token button.
    2. From the token creation page, give your token a name and make sure the box next to the Downloads:Read scope is checked.
    3. Click the Create token button at the bottom of the page to create your token.
    4. The token you've created is a secret token, which means you will only have one opportunity to copy it somewhere secure.

You should not expose these access tokens in publicly-accessible source code where unauthorized users might find them. Instead, you should store them somewhere safe on your computer and take advantage of Gradle properties to make sure they're only added when your app is compiled. Once this configuration step has been completed, you will be able to reference your credentials in other parts of your app.

Configure your secret token

To avoid exposing your secret token, add it as an environment variable:

  1. Find or create a gradle.properties file in your Gradle user home folder. The folder can be found at «USER_HOME»/.gradle. Once you have found or created the file, its path should be «USER_HOME»/.gradle/gradle.properties. You can read more about Gradle properties in the official Gradle documentation.
  2. Add your secret token your gradle.properties file:
MAPBOX_DOWNLOADS_TOKEN=PASTE_YOUR_SECRET_TOKEN_HERE

Configure your public token

There are many ways to configure your public access token. Many of the examples and code snippets on this site assume your token is stored in a file in your project with other string values. If you would like to manage your public access token this way, open your project's R.strings.xml file and add the following string resource:

<string name="mapbox_access_token">MAPBOX_ACCESS_TOKEN</string>

If you ever need to rotate your access token, you will need to update the token value in your R.strings.xml file.

Configure permissions

You can use the Manifest merge feature to reduce the need to include any SDK requirements in your application's manifest file. You'll need to add either the Fine or Coarse location permission if you plan to display a user's location on the map or get the user's location information. The user location permission should also be checked during runtime using the PermissionsManager.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Add the dependency

Mapbox provides the Navigation SDK via Maven.

To add the Mapbox Navigation SDK as a dependency, you will need to configure your build to download the Navigation SDK from Mapbox directly. This requires a valid username and password.

  1. Open your project in Android Studio.

  2. Open up your module-level build.gradle file.

  3. The Navigation SDK uses Java 8 features. To enable Java 8 in your project, add the following compileOptions.

    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        ...
    }
    
  4. Make sure that your project's minSdkVersion is at API 19 or higher.

    android {
        ...
        defaultConfig {
            minSdkVersion 19
        }
    }
    
  5. Under dependencies, add a new build rule for the latest mapbox-android-navigation.

    dependencies {
        implementation 'com.mapbox.navigation:core:1.0.0-rc.8'
    }
    
  6. Open up your project-level build.gradle file. Declare the Mapbox Downloads API's releases/maven endpoint in the repositories block. To download the Navigation SDK dependency, you must authenticate your request with a valid username and password. In the previous section, you added the password to a gradle.properties file in your Gradle user home folder. The username field should always be "mapbox". It should not be your personal username used to create the secret token.

    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'] ?: ""
                }
            }
        }
    }
    
  7. Because you've edited your Gradle files, Android Studio will ask you whether you want to sync the Gradle files. You can sync now.

Note: You might have mismatching Gradle dependencies once you add the Mapbox Maps SDK for Android. If necessary, you can use exclude group to remove certain dependencies:

implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:1.0.0-rc.8'){
    exclude group: 'group_name', module: 'module_name'
}

Additionally, running gradle app_module_name_here:dependencies in your command line will print a list of dependencies. ./gradlew app:dependencies works if you have a Gradle wrapper. They are helpful for troubleshooting nimble Gradle configurations when various libraries are included in a single project. You can see the dependencies that specific libraries are bringing and where conflicts might be happening.

Using nightly builds and beta versions

You can also use a SNAPSHOT version of the Navigation SDK if you want access to bug fixes or features that have not been packaged in an official release yet. Find more information about how to do this inside the Navigation SDK's GitHub repository.

Handle location permissions

The Navigation SDK makes use of the Android manifest merge feature. The Navigation SDK's manifest file already has both the course and fine location permission lines, which means you don't have to declare any location permissions inside your project's manifest file or in some other way.

If your project targets Android API 23 or higher, make sure to check the user location permission in runtime using the Mapbox Core Library for Android's PermissionsManager interface.

Set NavigationOptions

The NavigationOptions provides MapboxNavigation with parameters used for turn-by-turn navigation. This is where you can customize navigation with your own LocationEngine or the ReplayLocationEngine, specify the device profile, and change where routing tiles will be stored.

The simplest way to create NavigationOptions is to use the defaultNavigationOptionsBuilder(). It includes all the defaults you will need to get started.

val navigationOptions = MapboxNavigation
    .defaultNavigationOptionsBuilder(this, accessToken)
    .build()

Use the NavigationOptions.Builder for further customization. It includes defaults for many of the options that are not required and allows you to override every option.

val navigationOptions NavigationOptions.Builder(context)
    .accessToken(accessToken)
    .deviceProfile(AutomobileProfile())
    .onboardRouterOptions(OnboardRouterOptions.Builder()
        .filePath(customFilePath)
        .build())
    .timeFormatType(TimeFormat.TWENTY_FOUR_HOURS)
    .distanceFormatter(MapboxDistanceFormatter.builder()
        .withUnitType(VoiceUnit.METRIC)
        .withRoundingIncrement(Rounding.INCREMENT_ONE_HUNDRED)
        .build(context))

Create a MapboxNavigation object

Creating a MapboxNavigation object is the first step in interacting with a navigation session. It's used to request routes, register various Navigation SDK observers, and make other navigation-related decisions. Using the NavigationOptions, create the MapboxNavigation object.

val navigationOptions = MapboxNavigation
    .defaultNavigationOptionsBuilder(context, accessToken)
    .build()
val mapboxNavigation = MapboxNavigation(navigationOptions)

Create and use only one instance of this class in your project.

Expect MapboxNavigation to have a longer lifecycle than an Activity. This will allow users to navigate while using other applications. To help maintain one instance you can use the MapboxNavigationProvider. The instance can be created early in the Application lifecycle, such as the Application#onCreate.

class NavigationApplication : Application() {
    override fun onCreate() {
        MapboxNavigationProvider.create(MapboxNavigation
            .defaultNavigationOptionsBuilder(context, accessToken)
            .build())
    }
}

This allows you to access the MapboxNavigation instance anywhere in your application using the retrieve() function.

class MyRouteProgressObserver : RouteProgressObserver {
    fun start() {
        if (MapboxNavigationProvider.isCreated()) {
            MapboxNavigationProvider.retrieve().registerRouteProgressObserver(this)
        }
    }

    fun stop() {
        if (MapboxNavigationProvider.isCreated()) {
            MapboxNavigationProvider.retrieve().unregisterRouteProgressObserver(this)
        }
    }

    override fun onRouteProgressChanged(routeProgress: RouteProgress) {
        // My route progress logic
    }
}

Pricing

Please see the Pricing by products guide for more information on pricing and billing. Depending on what you're building with the Navigation SDK for Android, your project may be eligible for alternative pricing options.

Was this page helpful?