Starting navigation

VERSION 1.1.0

The Navigation SDK for Android is only available upon request. Contact us to get started.

This guide explains how to start turn-by-turn navigation and retrieve route progress information such as the remaining travel time and traveled distance.

Project setup

Configure the project as described in the Project setup guide. Then add the following dependencies to the build.gradle.kts file of your application module and synchronize the project.

1val version = "1.1.0"
2implementation("com.tomtom.sdk.location:provider-simulation:$version")
3implementation("com.tomtom.sdk.navigation:navigation-online:$version")
4implementation("com.tomtom.sdk.routing:route-planner-online:$version")

Starting navigation

Before starting navigation with a route, you need to plan the route and set up navigation dependencies:

First, plan a Route between two locations using RoutePlanningOptions. Refer to the Planning a route guide if you want to learn more about how to plan a Route.

1fun planRoute() {
2 routePlanner = OnlineRoutePlanner.create(context = this, apiKey = TOMTOM_API_KEY)
3 val amsterdam = GeoPoint(latitude = 52.37616, longitude = 4.90828)
4 val rotterdam = GeoPoint(latitude = 51.90546, longitude = 4.46662)
5 val itinerary = Itinerary(origin = amsterdam, destination = rotterdam)
6 routePlanningOptions = RoutePlanningOptions(
7 itinerary = itinerary,
8 guidanceOptions = GuidanceOptions(),
9 vehicle = Vehicle.Car()
10 )
11 routePlanner.planRoute(
12 routePlanningOptions,
13 object : RoutePlanningCallback {
14 override fun onSuccess(result: RoutePlanningResponse) {
15 route = result.routes.first()
16 startNavigation()
17 }
18
19 override fun onFailure(failure: RoutingFailure) {
20 Log.e(TAG, "Unable to calculate a route: " + failure.message)
21 }
22
23 override fun onRoutePlanned(route: Route) = Unit
24 }
25 )
26}

After planning the route, create the NavigationTileStore as follows:

1private fun createNavigationTileStore(): NavigationTileStore {
2 val configuration = NavigationTileStoreConfiguration(
3 apiKey = TOMTOM_API_KEY
4 )
5 return NavigationTileStore.create(
6 context = this,
7 navigationTileStoreConfig = configuration
8 )
9}

Then, create and enable the LocationProvider. Refer to the Built-in location providers and the Location module guides for more information.

SimulationLocationProvider is an implementation of the LocationProvider interface that can be used for navigation configuration. The following snippet shows how to create and enable a SimulationLocationProvider. For a list of all the available location providers, refer to the Built-in location providers guide.

1private fun createSimulationLocationProvider(): LocationProvider {
2 val routeGeoLocations = route.geometry.map { GeoLocation(it) }
3 val simulationStrategy = InterpolationStrategy(routeGeoLocations)
4 return SimulationLocationProvider.create(strategy = simulationStrategy).also { simulationLocationProvider ->
5 simulationLocationProvider.enable()
6 }
7}

Now you can configure the navigation using:

1private fun createNavigationConfiguration(): Configuration {
2 val navigationTileStore = createNavigationTileStore()
3 val simulationLocationProvider = createSimulationLocationProvider()
4 return Configuration(
5 context = this,
6 navigationTileStore = navigationTileStore,
7 locationProvider = simulationLocationProvider,
8 routePlanner = routePlanner
9 )
10}

Next, create the TomTomNavigation object with the previous configuration.

val configuration = createNavigationConfiguration()
val tomTomNavigation = OnlineTomTomNavigationFactory.create(configuration)

To receive route progress updates during navigation, implement a ProgressUpdatedListener and add it to the initialized TomTomNavigation object.

tomTomNavigation.addProgressUpdatedListener(progressUpdatedListener)

To start navigation along a route, pass the NavigationOptions object which sets the active route to the above-created route.

1val routePlan = RoutePlan(route, routePlanningOptions)
2val navigationOptions = NavigationOptions(routePlan)
3tomTomNavigation.start(navigationOptions)

Combine all the previous snippets into the following function:

1fun startNavigation() {
2 val configuration = createNavigationConfiguration()
3 val tomTomNavigation = OnlineTomTomNavigationFactory.create(configuration)
4
5 tomTomNavigation.addProgressUpdatedListener(progressUpdatedListener)
6
7 val routePlan = RoutePlan(route, routePlanningOptions)
8 val navigationOptions = NavigationOptions(routePlan)
9 tomTomNavigation.start(navigationOptions)
10}

Once you have started navigation, TomTomNavigation will:

Retrieving route progress information

With navigation started, you can listen to route progress updates and retrieve the travelled distance and the remaining travel time.

1private val progressUpdatedListener = ProgressUpdatedListener { routeProgress ->
2 Log.v(TAG, "Distance along the route: ${routeProgress.distanceAlongRoute}")
3 Log.v(TAG, "Remaining travel time: ${routeProgress.remainingTime}")
4}

In addition to route progress updates, you can also listen to and handle events for route updates, deviations, route and lane guidance, waypoint arrival and reaching a destination. Refer to the Turn-by-turn navigation to find out more about supported navigation events.

Next steps

Now that you know how to start navigation with a route and have learnt about navigation events, here are the recommendations on what to explore next: