Driving with a route
Driving with a route
The GuidanceManager provides navigation guidance during active trips. It delivers turn-by-turn instructions and lane guidance to help drivers navigate their routes.
Starting navigation
Before observing guidance information, start navigation on a planned trip:
1import android.util.Log2import com.tomtom.automotive.integration.client.api.trip.TripManager3import com.tomtom.automotive.integration.client.api.trip.startnavigation.StartNavigationCallback4import com.tomtom.automotive.integration.client.api.trip.startnavigation.StartNavigationFailure5import com.tomtom.automotive.integration.client.api.trip.startnavigation.StartNavigationParameters6import com.tomtom.automotive.integration.client.api.trip.startnavigation.StartNavigationResponse7import com.tomtom.automotive.integration.client.common.Callback8import com.tomtom.automotive.integration.client.common.Result9import com.tomtom.automotive.integration.client.common.SdkReleasable1011private const val TAG = "Navigation"1213fun startNavigation(tripManager: TripManager, tripId: String) {14 tripManager.startNavigation(15 StartNavigationParameters(tripId),16 object : StartNavigationCallback {17 override fun onNavigatedTrip(response: Result<StartNavigationResponse, StartNavigationFailure>) {18 when (response) {19 is Result.Success -> {20 val trip = response.value.navigatedTrip21 Log.d(TAG, "Navigation started for trip: ${trip.id}")22 }23 is Result.Failure -> {24 Log.e(TAG, "Failed to start navigation: ${response.failure}")25 }26 }27 }2829 override fun onFunctionalityUnavailable(reason: Callback.Reason) {30 Log.e(TAG, "Navigation unavailable: ${reason.devMessage}")31 }32 }33 )34}
Observing next instruction
Subscribe to receive updates about the next maneuver:
1import android.util.Log2import com.tomtom.automotive.integration.client.api.guidance.GuidanceManager3import com.tomtom.automotive.integration.client.api.guidance.observenextinstruction.InstructionInfo4import com.tomtom.automotive.integration.client.api.guidance.observenextinstruction.NextInstructionListener5import com.tomtom.automotive.integration.client.api.model.guidance.InstructionInformation6import com.tomtom.automotive.integration.client.common.Callback7import com.tomtom.automotive.integration.client.common.SdkReleasable89private const val TAG = "Guidance"10private var observeNextInstructionReleasable: SdkReleasable? = null1112fun observeNextInstruction(guidanceManager: GuidanceManager) {13 observeNextInstructionReleasable = guidanceManager.observeNextInstruction(14 object : NextInstructionListener {15 override fun onNextInstructionChange(instructionInfo: InstructionInfo) {16 val instruction = instructionInfo.instruction17 if (instruction != null) {18 val distance = instruction.distanceToNextInstruction19 val info = instruction.instruction20 Log.d(TAG, "Next instruction in ${distance.meters}m: ${getInstructionDescription(info)}")2122 // Get road shields if available23 info.getRoadShields()?.forEach { shield ->24 Log.d(TAG, "Road shield: ${shield.roadNumber}")25 }26 }27 }2829 override fun onFunctionalityUnavailable(reason: Callback.Reason) {30 Log.e(TAG, "Instruction updates unavailable: ${reason.devMessage}")31 }32 }33 )34}3536private fun getInstructionDescription(info: InstructionInformation): String {37 return when (info) {38 is InstructionInformation.TurnInstruction -> "Turn ${info.direction}"39 is InstructionInformation.ForkInstruction -> "Fork ${info.direction}"40 is InstructionInformation.RoundaboutInstruction -> "Roundabout exit ${info.roundabout.exitNumber}"41 is InstructionInformation.ExitHighwayInstruction -> "Exit highway ${info.direction}"42 is InstructionInformation.DestinationArrivalInstruction -> "Arrive at destination"43 else -> info.toString()44 }45}4647fun unObserveNextInstruction() {48 observeNextInstructionReleasable?.release()49 observeNextInstructionReleasable = null50}
Instruction types
The InstructionInformation sealed interface has the following implementations:
TurnInstruction- Turn at a road junctionRoundaboutInstruction- Enter and navigate a roundaboutExitRoundaboutInstruction- Exit a roundaboutForkInstruction- Fork in a bifurcationFollowRoadInstruction- Continue following the current roadMergeInstruction- Merge onto highway/expresswayExitHighwayInstruction- Take a highway exitSwitchHighwayInstruction- Switch highways via exit rampTollgateInstruction- Approaching toll booth or ETC terminalDestinationArrivalInstruction- Arrival at destinationWaypointArrivalInstruction- Arrival at waypointDepartureInstruction- Starting point of tripBorderCrossingInstruction- Crossing international borderEntryAutoTransportInstruction- Enter vehicle transport (ferry, car train)ExitAutoTransportInstruction- Exit vehicle transportEnterCarpoolLaneInstruction- Enter carpool/HOV laneExitCarpoolLaneInstruction- Exit carpool/HOV lane
Fetching road shield assets
Road shields can be displayed as drawable resources:
1import android.util.Log2import com.tomtom.automotive.integration.client.api.asset.AssetManager3import com.tomtom.automotive.integration.client.api.asset.getdrawable.GetDrawableCallback4import com.tomtom.automotive.integration.client.api.asset.getdrawable.GetDrawableFailure5import com.tomtom.automotive.integration.client.api.asset.getdrawable.GetDrawableParameters6import com.tomtom.automotive.integration.client.api.asset.getdrawable.GetDrawableResponse7import com.tomtom.automotive.integration.client.api.model.drawable.Density8import com.tomtom.automotive.integration.client.api.model.guidance.RoadShield9import com.tomtom.automotive.integration.client.common.Callback10import com.tomtom.automotive.integration.client.common.Result1112private const val TAG = "Guidance"1314fun fetchRoadShieldAsset(assetManager: AssetManager, roadShield: RoadShield) {15 val drawableHandle = roadShield.getDrawableHandle(Density(2f))1617 assetManager.getDrawable(18 GetDrawableParameters(drawableHandle),19 object : GetDrawableCallback {20 override fun onResult(result: Result<GetDrawableResponse, GetDrawableFailure>) {21 when (result) {22 is Result.Success -> {23 val drawableResource = result.value.drawable24 Log.d(TAG, "Road shield fetched: $drawableResource")25 }26 is Result.Failure -> {27 Log.e(TAG, "Failed to fetch road shield: ${result.failure}")28 }29 }30 }3132 override fun onFunctionalityUnavailable(reason: Callback.Reason) {33 Log.e(TAG, "Asset manager unavailable: ${reason.devMessage}")34 }35 }36 )37}
Audio announcements
The Instruction object includes an audioAnnouncementPhase field that indicates the timing phase of the audio guidance:
1import android.util.Log2import com.tomtom.automotive.integration.client.api.guidance.GuidanceManager3import com.tomtom.automotive.integration.client.api.guidance.observenextinstruction.InstructionInfo4import com.tomtom.automotive.integration.client.api.guidance.observenextinstruction.NextInstructionListener5import com.tomtom.automotive.integration.client.api.model.guidance.AudioAnnouncementPhase6import com.tomtom.automotive.integration.client.common.Callback7import com.tomtom.automotive.integration.client.common.SdkReleasable89private const val TAG = "Guidance"10private var observeAudioReleasable: SdkReleasable? = null1112fun observeAudioAnnouncements(guidanceManager: GuidanceManager) {13 observeAudioReleasable = guidanceManager.observeNextInstruction(14 object : NextInstructionListener {15 override fun onNextInstructionChange(instructionInfo: InstructionInfo) {16 val instruction = instructionInfo.instruction17 if (instruction != null) {18 val phase = instruction.audioAnnouncementPhase19 when (phase) {20 AudioAnnouncementPhase.FOLLOW -> {21 Log.d(TAG, "Audio: Follow instruction")22 // Play follow announcement23 }24 AudioAnnouncementPhase.FAR_AWAY -> {25 Log.d(TAG, "Audio: Far away announcement")26 // Play far away announcement27 }28 AudioAnnouncementPhase.EARLY -> {29 Log.d(TAG, "Audio: Early announcement")30 // Play early announcement31 }32 AudioAnnouncementPhase.MAIN -> {33 Log.d(TAG, "Audio: Main announcement")34 // Play main announcement35 }36 AudioAnnouncementPhase.CONFIRMATION -> {37 Log.d(TAG, "Audio: Confirmation announcement")38 // Play confirmation announcement39 }40 AudioAnnouncementPhase.UNHANDLED, null -> {41 Log.d(TAG, "Audio: No announcement phase")42 }43 }44 }45 }4647 override fun onFunctionalityUnavailable(reason: Callback.Reason) {48 Log.e(TAG, "Audio announcements unavailable: ${reason.devMessage}")49 }50 }51 )52}5354fun unObserveAudioAnnouncements() {55 observeAudioReleasable?.release()56 observeAudioReleasable = null57}
Audio announcement phases
The AudioAnnouncementPhase enum represents different timing phases for audio guidance:
FOLLOW- Announcement at any distance beyond far awayFAR_AWAY- Earliest announcement about upcoming maneuverEARLY- Prepare for upcoming maneuverMAIN- Short time before the maneuverCONFIRMATION- At the time of the maneuver itself
Lane guidance
Lane guidance helps drivers choose the correct lane for upcoming maneuvers:
1import android.util.Log2import com.tomtom.automotive.integration.client.api.guidance.GuidanceManager3import com.tomtom.automotive.integration.client.api.guidance.observenextlaneguidance.LaneGuidanceInfo4import com.tomtom.automotive.integration.client.api.guidance.observenextlaneguidance.NextLaneGuidanceListener5import com.tomtom.automotive.integration.client.common.Callback6import com.tomtom.automotive.integration.client.common.SdkReleasable78private const val TAG = "Guidance"9private var observeNextLaneGuidanceReleasable: SdkReleasable? = null1011fun observeNextLaneGuidance(guidanceManager: GuidanceManager) {12 observeNextLaneGuidanceReleasable = guidanceManager.observeNextLaneGuidance(13 object : NextLaneGuidanceListener {14 override fun onNextLaneGuidanceChange(laneGuidanceInfo: LaneGuidanceInfo) {15 val lanes = laneGuidanceInfo.lanes16 Log.d(TAG, "Lane guidance: ${lanes.size} lanes")1718 lanes.forEachIndexed { index, lane ->19 lane.laneDirections.forEach { direction ->20 val marking = direction.laneMarking21 val shouldFollow = direction.hasToBeFollowed22 Log.d(TAG, "Lane $index: $marking (follow: $shouldFollow)")23 }24 }25 }2627 override fun onFunctionalityUnavailable(reason: Callback.Reason) {28 Log.e(TAG, "Lane guidance unavailable: ${reason.devMessage}")29 }30 }31 )32}3334fun unObserveNextLaneGuidance() {35 observeNextLaneGuidanceReleasable?.release()36 observeNextLaneGuidanceReleasable = null37}
Lane marking types
The LaneMarking enum represents the direction arrow or marking for each lane:
STRAIGHTSLIGHT_RIGHTRIGHTSHARP_RIGHTRIGHT_U_TURNSLIGHT_LEFTLEFTSHARP_LEFTLEFT_U_TURN