Get started
Kotlin SDK
Geo and navigation

Turn-by-turn navigation

Turn-by-turn navigation

A navigation follows a route you hand it, from positions you or the device give it. RouteFollowingEngine runs on every target. On Android, DeviceNavigationEngine adds the device's location and its voice.

Read the route

readItineraryRoute joins a saved itinerary's segments into one route to follow:

import com.mountaya.client.Mountaya
import com.mountaya.geo.NavigationRoute
import com.mountaya.geo.readItineraryRoute

suspend fun tourRoute(mountaya: Mountaya, itineraryId: String): NavigationRoute {
    val itinerary = mountaya.studio.itineraries.getItinerary(itineraryId).data
    return readItineraryRoute(itinerary.segments, itinerary.waypoints) ?: error("$itineraryId has no route to follow.")
}

Follow the route

RouteFollowingEngine is the navigation on every target: hand it positions, and read its state:

import com.mountaya.client.Mountaya
import com.mountaya.geo.RouteFollowingEngine
import com.mountaya.geo.readItineraryRoute
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first

suspend fun follow(scope: CoroutineScope, mountaya: Mountaya): RouteFollowingEngine<String> {
    val navigation =
        RouteFollowingEngine<String>(scope) { itineraryId ->
            val itinerary = mountaya.studio.itineraries.getItinerary(itineraryId).data
            readItineraryRoute(itinerary.segments, itinerary.waypoints) ?: error("No route to follow.")
        }
    navigation.start("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
    // Wait for the route, then hand each position your app reads, [longitude, latitude].
    navigation.state.first { it.route != null }
    navigation.setPosition(listOf(6.8712, 45.9231))
    return navigation
}

Use the device's location

On Android, DeviceNavigationEngine reads the device's satellite position while your app holds ACCESS_FINE_LOCATION:

import android.content.Context
import com.mountaya.client.Mountaya
import com.mountaya.geo.readItineraryRoute
import com.mountaya.navigation.DeviceLocationSource
import com.mountaya.navigation.DeviceNavigationEngine
import com.mountaya.navigation.LocationRate
import kotlinx.coroutines.CoroutineScope

fun guided(scope: CoroutineScope, context: Context, mountaya: Mountaya): DeviceNavigationEngine<String> =
    DeviceNavigationEngine(
        scope = scope,
        route = { itineraryId ->
            val itinerary = mountaya.studio.itineraries.getItinerary(itineraryId).data
            readItineraryRoute(itinerary.segments, itinerary.waypoints) ?: error("No route to follow.")
        },
        // A walker's rate: a fix every two seconds once the device moved 5 m.
        location = DeviceLocationSource(context, rate = LocationRate.Walking),
    )

Ask for location access in your app's own flow first. The engine never asks.

Speak the prompts

SpeechNavigationVoice says each maneuver through the platform's text-to-speech, in your words:

import android.content.Context
import com.mountaya.geo.NavigationInstruction
import com.mountaya.geo.NavigationInstructionKey
import com.mountaya.navigation.SpeechNavigationVoice
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

fun frenchVoice(scope: CoroutineScope, context: Context): SpeechNavigationVoice {
    val voice = SpeechNavigationVoice(context, "fr-FR") { instruction -> sentence(instruction) }
    // Learn before the first maneuver whether the speech engine starts.
    scope.launch { voice.prepare() }
    return voice
}

// Your words for each instruction. One you answer null for is not said.
fun sentence(instruction: NavigationInstruction): String? =
    when (instruction.key) {
        NavigationInstructionKey.TurnLeft -> "Tournez à gauche sur ${instruction.name}"
        NavigationInstructionKey.TurnLeftPlain -> "Tournez à gauche"
        NavigationInstructionKey.Waypoint -> "Passez par ${instruction.name}"
        NavigationInstructionKey.Goal, NavigationInstructionKey.GoalPlain -> "Vous êtes arrivé"
        else -> null
    }

Hand it to the engine as voice = frenchVoice(scope, context). The engine says each maneuver once, 50 m before it.

Show progress

The state holds what is left to the next maneuver and to the arrival:

import com.mountaya.geo.NavigationEngine
import com.mountaya.geo.NavigationStatus
import com.mountaya.geo.UnitPreferences
import com.mountaya.geo.formatDistance
import com.mountaya.geo.formatDuration

suspend fun showProgress(navigation: NavigationEngine<String>) {
    val units = UnitPreferences("en")
    navigation.state.collect { state ->
        val progress = state.progress
        when {
            state.status == NavigationStatus.Arrived -> println("Arrived")
            progress != null -> println("${formatDistance(progress.toManeuver.distance, units)} to the next turn, ${formatDuration(progress.toArrival.duration, units)} left")
            else -> println(state.status)
        }
    }
}

Replay a route

ReplayLocationSource walks the route for you, at the speed you name, for a demo or a test:

import com.mountaya.geo.NavigationRoute
import com.mountaya.navigation.DeviceNavigationEngine
import com.mountaya.navigation.ReplayLocationSource
import kotlinx.coroutines.CoroutineScope
import kotlin.time.Duration.Companion.seconds

fun replay(scope: CoroutineScope, route: NavigationRoute): DeviceNavigationEngine<Unit> {
    val navigation =
        DeviceNavigationEngine<Unit>(
            scope = scope,
            route = { route },
            location = ReplayLocationSource(scope, metersPerSecond = 10.0, interval = 1.seconds),
        )
    navigation.start(Unit)
    return navigation
}

Next steps

Last updated