Skip to content

Fly To Route Instruction

In this guide you will learn how to render an interactive map, and fly to a specific instruction along a computed route rendered on the map.

Setup

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

Download the Maps & Navigation SDK for Android archive file

Download the FlyToRouteInstruction 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.

Fly to route instruction example Android screenshot

This example flies to a specific instruction along a computed route rendered on the map.
Displays an interactive map which is fully 3D, supporting pan, pinch-zoom, rotate and tilt.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how to fly to a specific instruction along a computed route on the map.

 1// Kotlin code
 2val calculateRoute =
 3{
 4    SdkCall.execute
 5    {
 6        if (!isMapReady) return@execute
 7        val waypoints = arrayListOf(
 8            Landmark("London", Coordinates(51.5073204, -0.1276475)),
 9            Landmark("Paris", Coordinates(48.8566932, 2.3514616))
10        )
11        routingService.calculateRoute(waypoints)
12    }
13}

Coordinates are defined using a Landmark for the departure and another for the destination, then the routingService instance calculates the route using calculateRoute.

1private fun flyToInstruction(routeInstruction: RouteInstruction)
2{
3     val animation = Animation()
4     animation.setType(EAnimation.AnimationLinear)
5     // Center the map on a specific route instruction using the provided animation.
6     mainMapView?.centerOnRouteInstruction(routeInstruction, -1, Xy(), animation)
7}

The centerOnRouteInstruction(routeInstruction, -1, Xy(), animation) function is used to fly to a specific instruction along the computed route.

 1routingService.onCompleted = onCompleted@{ routes, reason, _ ->
 2    progressBar.visibility = View.GONE
 3    when (val gemError = SdkError.fromInt(reason))
 4    {
 5        SdkError.NoError ->
 6        {
 7            // No error encountered, we can handle the results.
 8            SdkCall.execute
 9            {
10                // Get the main route from the ones that were found.
11                val mainRoute: Route? = if (routes.size > 0)
12                {
13                    routes[0]
14                } else {
15                    null
16                }
17                // Get the first route instruction from the main route.
18                val routeInstruction =
19                    mainRoute?.getSegments()?.get(0)?.getInstructions()?.get(0)
20
21                if (routeInstruction == null)
22                {
23                    postOnMain
24                    {
25                        Toast.makeText(
26                            this@MainActivity,
27                            "No route instructions found!",
28                            Toast.LENGTH_SHORT
29                        )
30                            .show()
31                    }
32                    return@execute
33                }
34                // Add the main route to the map so it can be displayed.
35                mainMapView?.preferences()?.routes()?.add(mainRoute, true)
36                flyToInstruction(routeInstruction)
37            }
38        }
The selected route instruction is index 0
mainRoute?.getSegments()?.get(0)?.getInstructions()?.get(0)
(the first instruction) from the route at index 0,
routes[0] (the first route) from the resulting list of routes between the departure and destination points.

Android Examples

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