Horizon traffic
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
If you prefer to configure the horizon path parameters manually, you can create the HorizonOptions as follows:
val horizonOptions = HorizonOptions( elementTypes = listOf(TrafficElementType), mainPathSearchOptions = MainPathSearchOptions( searchDistancePolicy = ExplicitDistancePolicy( searchDistance = PathSearchDistance(maxHorizonLength = Distance.kilometers(5)), ), ), subPathSearchOptions = listOf( SubPathSearchOptions( searchDistance = PathSearchDistance(maxHorizonLength = Distance.meters(100)), ), ), numberOfPaths = 10, )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.
val routePlan = RoutePlan(route, routePlanningOptions)val navigationOptions = NavigationOptions(routePlan)tomTomNavigation.start(navigationOptions)Retrieving traffic data
With the navigation started, you can listen for horizon updates and retrieve traffic data.
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 :
private fun HorizonPath.retrieveTrafficElements() = getElements(TrafficElementType) .map { it as TrafficElement } .filter { element -> element.trafficEvent.category == Category.RoadClosure }Next steps
Now that you know how to retrieve horizon traffic data, here are the recommendations on what to explore next: