Skip to main content
GuidesAPI ReferenceExamples

Route Instructions

|

In this guide you will learn how to get the text instructions for a computed route.

This example computes a route and then displays the text instructions for navigation along that route.

Route instruction list

Routing

A route must be calculated similar to RoutingOnMap example. See Routing on Map Example.

MainActivity.kt
class MainActivity : AppCompatActivity()
{
//...

private val routingService = RoutingService(
onStarted = {
progressBar.visibility = View.VISIBLE
},
onCompleted = { routes, errorCode, _ ->
progressBar.visibility = View.GONE
when (errorCode) {
GemError.NoError -> {
if (routes.size == 0) return@onCompleted

// Get the main route from the ones that were found.
displayRouteInstructions(routes[0])
}
GemError.Cancel -> {
// The routing action was cancelled.
}
else -> {
// There was a problem at computing the routing operation.
Toast.makeText(
this@MainActivity,
"Routing service error: ${GemError.getMessage(errorCode)}",
Toast.LENGTH_SHORT
).show()
}
}
}
)
//...
}

Displaying route instructions

MainActivity.kt
class MainActivity : AppCompatActivity()
{
//...
private fun displayRouteInstructions(route: Route)
{
// Get the instructions from the route.
val instructions = SdkCall.execute { route.instructions } ?: arrayListOf()
val imageSize = resources.getDimension(R.dimen.turn_image_size).toInt()
listView.adapter = CustomAdapter(instructions, imageSize, isDarkThemeOn())
//...
}
//...
}

The function displayRouteInstructions(..) passes a list of RouteInstruction to a recyclerview adapter. Inside the onBindViewHolder(..) method of the adapter the key informations are filtered and prepared for display.

MainActivity.kt
    override fun onBindViewHolder(viewHolder: RouteInstructionViewHolder, position: Int)
{
val instruction = dataSet[position]
var text: String
var status: String
var description: String
var turnImage: Bitmap?

SdkCall.execute {
if (instruction.hasTurnInfo())
{
val aInner = if (isDarkThemeOn) Rgba(255, 255, 255, 255) else Rgba(0, 0, 0, 255)
val aOuter = if (isDarkThemeOn) Rgba(0, 0, 0, 255) else Rgba(255, 255, 255, 255)
val iInner = Rgba(128, 128, 128, 255)
val iOuter = Rgba(128, 128, 128, 255)

turnImage = GemUtilImages.asBitmap(instruction.turnDetails?.abstractGeometryImage, imageSize, imageSize, aInner, aOuter, iInner, iOuter)

text = instruction.turnInstruction ?: ""
if (text.isNotEmpty() && text.last() == '.')
{
text.removeSuffix(".")
}

val distance = instruction.traveledTimeDistance?.totalDistance?.toDouble() ?: 0.0

val distText = GemUtil.getDistText(distance.toInt(), SdkSettings.unitSystem)
status = distText.first
description = distText.second
if (status == "0.00")
{
status = "0"
}

postOnMain {
viewHolder.turnImage.setImageBitmap(turnImage)
viewHolder.text.text = text
viewHolder.status.text = status
viewHolder.description.text = description
}
}
}
}