Horizon traffic

VERSION 2.1.2

The Traffic service delivers real-time traffic information, encompassing traffic incidents, flow, and travel times. The warnings issued are safety-related, designed to inform or alert drivers about potential traffic jams, roadworks, and road closures.

Configuring the horizon engine

Having completed the Retrieving horizon data guide, you are now able to set up the horizon engine and start navigation to retrieve horizon data.

Specifying horizon options

To subscribe to traffic elements, create HorizonOptions via the buildHorizonOptions method and specify TrafficElementType in the list of element types of interest. The horizon path parameters defined within buildHorizonOptions are appropriate for most use cases and can be utilized in both free driving and active guidance scenarios. The buildHorizonOptions method will create a HorizonOptions instance with a main path search distance that is the minimum between 25 km and the remaining route length when navigating with a route, or 10 km when free driving.

val horizonOptions =
buildHorizonOptions(listOf(TrafficElementType))
When using the Extended flavor

The Extended flavor of Maps and Navigation SDK for Android is only available upon request. Contact us to get started.

If you prefer to configure the horizon path parameters manually, you can create the HorizonOptions as follows:

1val horizonOptions =
2 HorizonOptions(
3 elementTypes = listOf(TrafficElementType),
4 mainPathSearchOptions =
5 MainPathSearchOptions(
6 searchDistancePolicy = ExplicitDistancePolicy(
7 searchDistance = PathSearchDistance(maxHorizonLength = Distance.kilometers(5)),
8 ),
9 ),
10 subPathSearchOptions = listOf(
11 SubPathSearchOptions(
12 searchDistance = PathSearchDistance(maxHorizonLength = Distance.meters(100)),
13 ),
14 ),
15 numberOfPaths = 10,
16 )

Registering a horizon updated listener

Before starting navigation, register a HorizonUpdatedListener to listen for horizon updates with the horizon options you have defined.

tomTomNavigation.addHorizonUpdatedListener(horizonOptions, horizonUpdatedListener)

Starting navigation

Next, initiate navigation using a route, as outlined in the Retrieving horizon data guide.

1val routePlan = RoutePlan(route, routePlanningOptions)
2val navigationOptions = NavigationOptions(routePlan)
3tomTomNavigation.start(navigationOptions)

Retrieving traffic data

With the navigation started, you can listen for horizon updates and retrieve traffic data.

If a traffic zone stretches beyond the end of the horizon, the SDK only provides data for the part of the zone within the horizon length. As the horizon is extended ahead of the vehicle while it moves along the traffic zone location, the end offset of the traffic element is updated accordingly.

Filtering traffic

Some retrieved traffic elements may not be relevant to the driver. For example, the driver might not be concerned about traffic caused by road closures. To filter out these irrelevant traffic elements, you can apply additional filters when retrieving the traffic elements:

1private fun HorizonPath.retrieveTrafficElements() = getElements(TrafficElementType)
2 .map { it as TrafficElement }
3 .filter { element ->
4 element.trafficEvent.category == Category.RoadClosure
5 }

Next steps

Now that you know how to retrieve horizon traffic data, here are the recommendations on what to explore next: