Skip to content

Routing

In this guide you will learn how to render an interactive map, compute and render a route on the map, and fly to the route.

Setup

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

Download the Maps & Navigation SDK for Android archive file

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

Routing example Android screenshot

This example computes and draws a route on the map and flies to it.
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 the route is computed and drawn on the map.

 1// Kotlin code
 2private fun calculateRoute() {
 3  val waypoints = arrayListOf(
 4    Landmark("London", Coordinates(51.5073204, -0.1276475)),
 5    Landmark("Paris", Coordinates(48.8566932, 2.3514616))
 6  )
 7  routingService.calculateRoute(waypoints)
 8}
 9private fun flyToRoute(route: Route) {
10  val animation = Animation()
11  animation.setType(EAnimation.Fly)
12
13  // Center the map on a specific route using the provided animation.
14  mainMapView?.centerOnRoute(route, Rect(), animation)
15}
16
17private fun formatAndAddRoutes(routes: ArrayList<Route>) {
18  for (routeIndex in 0 until routes.size) {
19    val route = routes[routeIndex]
20    // Format the route name.
21    val routeName = Util.formatRouteName(route)
22    /*
23    Create an image list that will be added to the route name
24    and then be displayed in the route's bubble.
25    */
26    val imageList = arrayListOf<Image>()
27
28    // Add the route to the map (along with the image list).
29    mainMapView?.preferences()?.routes()?.add(
30      route, routeIndex == 0, routeName, imageList
31    )
32  }
33}
34
35override fun onCreate(savedInstanceState: Bundle?) {
36  super.onCreate(savedInstanceState)
37  setContentView(R.layout.activity_main)
38  progressBar = findViewById(R.id.progressBar)
39  routingService.onCompleted = onCompleted@{ routes, reason, hint ->
40    progressBar.visibility = View.GONE
41
42    when (val gemError = SdkError.fromInt(reason)) {
43      SdkError.NoError -> {
44        SdkCall.execute {
45          // Format route names and add them to the map.
46          formatAndAddRoutes(routes)
47
48          // Get the main route from the ones that were found.
49          val mainRoute: Route? = if (routes.size > 0) {
50            routes[0]
51          } else {
52            null
53          }
54          if (mainRoute != null) {
55            flyToRoute(mainRoute)
56          }
57        }
58      }
59    }
60  }
61}

In the calculateRoute() function, 2 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.

Next, the routing service computes the route, and calls the onCompleted callback with the resulting routes, as there could be more than one: routingService.calculateRoute(waypoints) onCompleted is set here in the onCreate() function

Then, formatAndAddRoutes(routes) adds the routes to the map so they are rendered.

If there is at least one route, flyToRoute(mainRoute) calls the centerOnRoute() API function to fly to the selected route, such that its bounding box fits in the viewport.

Android Examples

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