Skip to content

Fly To Traffic

In this guide you will learn how to render an interactive map, and fly to a section of a route with heavy traffic.


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

Download the Maps & Navigation SDK for Android archive file

Download the FlyToTraffic 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 traffic example Android screenshot

This example flies to a section of a route with a traffic event, such as heavy traffic. The route is drawn in blue on the map, and the traffic event section of the route is in red.
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 traffic event section of a route.

When the routingService finishes computing the route, the routingService.onCompleted callback function is called.

 1when (val gemError = SdkError.fromInt(reason))
 3    SdkError.NoError -> {
 4        // No error encountered, we can handle the results.
 5        SdkCall.execute {
 6            // Get the main route from the ones that were found.
 7            val mainRoute: Route? = if (routes.size > 0)
 8            {
 9                routes[0]
10            } else {
11                null
12            }
13            // Get the traffic events along the main route.
14            val trafficEventsLists = mainRoute?.getTrafficEvents() ?: ArrayList()
15            // Get the first traffic event from the main route.
16            val trafficEvent = if (trafficEventsLists.size == 0)
17            {
18                null
19            } else {
20                trafficEventsLists[0]
21            }
22            if (trafficEvent == null || mainRoute == null)
23            {
24                //"No traffic events!"
25                return@execute
26            }
27            // Add the main route to the map so it can be displayed.
28            mainMapView?.preferences()?.routes()?.add(mainRoute, true)
29            flyToTraffic(trafficEvent)
30        }
31    }
32    SdkCall.execute
33    {
34        val waypoints = arrayListOf(
35            Landmark("London", Coordinates(51.5073204, -0.1276475)),
36            Landmark("Paris", Coordinates(48.8566932, 2.3514616))
37        )
38        routingService.calculateRoute(waypoints)
39    }

The route departure and destination endpoints are defined using a Landmark instance for each, with Coordinates for London and Paris in this case, respectively.

Then routingService.calculateRoute(waypoints) is used to calculate the route and the routingService.onCompleted callback is called when the route is computed.

If there is at least 1 route in the resulting routes list, the first route is autoselected and set as the mainRoute. This is added to the map so it can be displayed.

If there is at least 1 traffic event in the trafficEventsLists for the route, then the first traffic event is autoselected.

1// Kotlin code
2private fun flyToTraffic(trafficEvent: RouteTrafficEvent)
4    val animation = Animation()
5    animation.setType(EAnimation.AnimationLinear)
7    // Center the map on a specific traffic event using the provided animation.
8    mainMapView?.centerOnRouteTrafficEvent(trafficEvent, -1, Rect(), animation)

Next, flyToTraffic flies the camera to that traffic event such that the entire traffic event area fits in the viewport, using the API function centerOnRouteTrafficEvent()

The mainMapView?.centerOnRouteTrafficEvent() API function flies the camera to the bounding box region containing the given RouteTrafficEvent.

Android Examples

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