Traffic
This guide provides instructions on how to enable or disable traffic incidents on your route, along with guidance on requesting a list of traffic incidents specific to your route. Additionally, it elaborates on the various types of traffic incidents that are supported and may be encountered during your journey.
Project setup
- Before proceeding, follow the Quickstart guide for Maps and the Quickstart guide for Navigation to create the
TomTomMapandTomTomNavigationobjects, which are essential for using the Navigation Visualization module that displays on-route traffic incidents. - Add the Navigation Visualization dependency to the
build.gradlefile of your application module and synchronize the project:implementation("com.tomtom.sdk.maps.visualization:navigation:2.1.2") - Use the
NavigationVisualizationFactory.createmethod to create theNavigationVisualizationobject:navigationVisualization =NavigationVisualizationFactory.create(tomTomMap, tomtomNavigation)
Displaying traffic incidents on the route
To display traffic incidents on the route, call the displayRoutePlan function with a RoutePlan:
navigationVisualization.displayRoutePlan(routePlan)
By default, traffic incidents on the route are enabled. However, if you want to disable them, use trafficIncidentsEnabled. To retrieve the list of displayed TrafficIncident objects on a route, use getTrafficIncidents:
navigationVisualization.traffic.getTrafficIncidents(route)
Traffic incident types
We currently support four types of traffic incidents that may occur on a route:
Jam incidents denote regular traffic congestion. They are identifiable by a numeric marker indicating the delay in minutes. These delays fall into three categories:
Minor
Moderate
Major
Road works may appear in one of four magnitudes:
Unknown
Minor
Moderate
Major
Although closed roads are typically avoided during routing, users might deviate from the suggested path and enter closed roads. The RoadClosed incident type does not have an associated magnitude.
Incidents that do not fall into the above categories are grouped under the Unknown category. These can also be classified by magnitude:
Unknown
Minor
Moderate
Major
Traffic style customization
While creating the NavigationVisualization object using NavigationVisualizationFactory.create, you can pass in a StyleConfiguration to define custom styling. If not specified, the default style is applied. To define a custom style for traffic, use RouteTrafficIncidentStyle. Pass the custom style object like this:
1navigationVisualization =2 NavigationVisualizationFactory.create(3 tomTomMap,4 tomtomNavigation,5 styleConfiguration = StyleConfiguration(routeTrafficIncident = customTrafficStyle),6 )
Next steps
Since you have learned how to display traffic incidents on a route, here are recommendations for the next steps: