×
Route Instructions | Maps SDK for Android | General Magic Route Instructions — Maps & Navigation SDKs documentation
Skip to content

Route Instructions

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

Setup

First, get an API key token, see the Getting Started guide.

Download the Maps & Navigation SDK for Android archive file

Download the RouteInstructions project archive file or clone the project with Git

See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files

An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the android device.
Android example screenshot
This example computes a route and then displays the text instructions for navigation along that route.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how the route is computed and the text instructions are obtained.

1// Kotlin code
2private val routingService = RoutingService()
3
4val wayPoints = arrayListOf(
5    Landmark("Frankfurt am Main", Coordinates(50.11428, 8.68133)),
6    Landmark("Karlsruhe", Coordinates(49.0069, 8.4037)),
7    Landmark("Munich", Coordinates(48.1351, 11.5820))
8)
9routingService.calculateRoute(wayPoints)

A private val routingService = RoutingService() instance is declared.

Two Landmark instances are defined, one for the departure, and one for the destination coordinates of the route endpoints. A route must have at least 2 Landmark instances(waypoints), but optionally can have more, for any optional additional waypoints along the route.

Then the routingService.calculateRoute(wayPoints) function is used to compute the route.

 1routingService.onCompleted = onCompleted@{ routes, reason, _ ->
 2    when (val gemError = SdkError.fromInt(reason))
 3    {
 4        SdkError.NoError ->
 5        {
 6            // No error encountered, we can handle the results.
 7            SdkCall.execute
 8            {
 9                // Get the main route from the ones that were found.
10                mainRoute = if (routes.size > 0)
11                {
12                    routes[0]
13                } else {
14                    null
15                }
16                postOnMain { mainRoute?.let { displayRouteInstructions(it) } }
17            }
18        }

When the route computation is complete, routingService.onCompleted = onCompleted@{ the first route, index 0, is selected routes[0]

 1fun displayRouteInstructions(route: Route)
 2{
 3    val instructions = arrayListOf<RouteInstruction>()
 4    SdkCall.execute
 5    {
 6         // Get all the route segments.
 7         val segmentList = route.getSegments()
 8         if (segmentList != null)
 9         {
10             // For each segment, get the route instructions.
11             for (segment in segmentList)
12             {
13                 val instructionList = segment.getInstructions() ?: continue
14                 for (instruction in instructionList)
15                 {
16                     instructions.add(instruction)
17                 }
18             }
19         }
20    }
The segments of the selected route are obtained:
val segmentList = route.getSegments()
The instructions for each segment are obtained:
val instructionList = segment.getInstructions() ?: continue
The instructions are added to a list:
instructions.add(instruction)

Android Examples

Maps SDK for Android Examples can be downloaded or cloned with Git