Traffic incidents module
The Traffic Incidents Module controls the map layers that display real-time traffic events, such as accidents, road closures, construction, and weather disruptions. Events are displayed across the entire map area and each incident shows its type, severity, affected road segments, and expected delay impact.
What this module owns: whether the style's incident layers and icons are shown, and which incidents — visibility and filters. The colour of each severity and the line width are Styling module knobs (traffic.incidents.*). Incidents you fetched yourself are drawn by the Traffic Incident Overlay Module instead. See Who owns what.
Looking for something else?
- For traffic speed conditions (color-coded congestion) across the map, see Traffic Flow Module
- For traffic incidents along a specific route, see Routing Module
- For rendering incidents you fetched via the
trafficIncidentDetails()REST service as a developer-controlled overlay, see Traffic Incident Overlay Module
Initialization
import { TomTomMap, TrafficIncidentsModule } from '@tomtom-org/maps-sdk/map';
const map = new TomTomMap( { mapLibre: { container: 'map' } } );
const trafficIncidentsModule = await TrafficIncidentsModule.get(map);
Visibility
Traffic incidents are hidden by default: the style parts the SDK adds — traffic flow, traffic incidents and the hillshade — are hidden until you show them.
You can make them visible during initialization:
const trafficIncidentsModule = await TrafficIncidentsModule.get(map, { visible: true });
You can change visibility later using the setVisible method, and read the setting back with
isVisible(), which is false until something shows the incidents.
trafficIncidentsModule.setVisible(true);
trafficIncidentsModule.isVisible(); // true
Icon visibility
Icons can be shown or hidden separately from incident areas, so you can show only the icons, or only the lines, at any time. They always show the same incidents as the lines: the module's filters apply to both.
// Initialize with visibility set in the configuration
const trafficIncidentsModule = await TrafficIncidentsModule.get(map, {
icons: {
visible: false,
},
});
// Show/hide incident icons but keep incident areas visible
trafficIncidentsModule.setIconsVisible(true);
trafficIncidentsModule.setIconsVisible(false);
// Check icon visibility
if (trafficIncidentsModule.anyIconLayersVisible()) {
console.log('Incident icons are visible');
}
Filtering
A filter can be applied either during module initialization or later using the filter method. It narrows the incident lines and their icons alike.
// Initialize with filter
const trafficIncidentsModule = await TrafficIncidentsModule.get(map, {
filters: ...,
});
// Apply or update filter later
trafficIncidentsModule.filter(...);
Incident category
Display only specific types of incidents:
// Show only accidents and road closures
trafficIncidentsModule.filter({
any: [{
incidentCategories: {
show: 'only',
values: ['accident', 'road-closed']
}
}]
});
// Hide weather-related incidents
incidents.filter({
any: [{
incidentCategories: {
show: 'all-except',
values: ['rain', 'fog', 'frost', 'wind']
}
}]
});
Available Incident Categories:
accident- Traffic accidentsroad_closed- Complete road closureslane_closed- Lane closuresroad_works- Construction and maintenancejam- Traffic jamsfog,rain,ice,wind,flooding- Weather conditionsdangerous_conditions- Hazardous road conditionsbroken_down_vehicle- Vehicle breakdownsunknown- Incidents with unknown type
Severity
Filter incidents based on their delay magnitude:
// Show moderate and major delays
incidents.filter({
any: [{
magnitudes: {
show: 'only',
values: ['major', 'moderate']
}
}]
});
Available Magnitudes:
minor- Small delaysmoderate- Moderate delaysmajor- Significant delaysindefinite- Road closures with no estimated delayunknown- Unknown or no delay information
Delay duration
Filter incidents by minimum delay:
// Show incidents with at least 5 minutes delay
incidents.filter({
any: [{
delays: {
mustHaveDelay: true,
minDelayMinutes: 5
}
}]
});
Road category
Display incidents only on specific road types:
// Show incidents only on highways
incidents.filter({
any: [{
roadCategories: {
show: 'only',
values: ['motorway', 'trunk']
}
}]
});
// Show incidents only on major local roads within tertiary category
incidents.filter({
any: [{
roadCategories: {
show: 'only',
values: ['tertiary']
},
roadSubCategories: {
show: 'only',
values: ['major_local']
}
}]
});
Available Road Categories:
motorway- Major highwaystrunk- Major roadsprimary- Primary roadssecondary- Secondary roadstertiary- Tertiary roadsstreet- Local streets
Available Road Subcategories:
tertiaryconnecting- Roads connecting different areasmajor_local- Major roads within local areas
streetlocal- Standard local streetsminor_local- Minor residential streets
Multiple filters
Combine multiple filter criteria:
// Show major accidents OR road closures
incidents.filter({
any: [
{
incidentCategories: { show: 'only', values: ['accident'] },
magnitudes: { show: 'only', values: ['major'] }
},
]
});
Reset filters
Remove all filters and show all incidents:
incidents.filter(undefined);
Events
Handle user interactions with traffic incidents:
// Click events
incidents.events.on('click', (feature) => {
...
});
// Hover events
incidents.events.on('hover', (feature) => {
...
});
// Long hover events
incidents.events.on('long-hover', (feature) => {
...
});
// Remove event listeners
incidents.events.off('click', handler);
Querying visible features
Use getShown() to retrieve all traffic incident features currently rendered in the visible map area, including both incident areas/lines and icon markers:
const shown = trafficIncidentsModule.getShown();
console.log(`Visible incidents: ${shown.trafficIncidents.length}`);
shown.trafficIncidents.forEach(incident => {
console.log('Incident:', incident.properties.category, '-', incident.properties.magnitudeOfDelay);
});
Filter and analyze visible incidents by category or severity:
const { trafficIncidents } = trafficIncidentsModule.getShown();
const accidents = trafficIncidents.filter(
incident => incident.properties.category === 'accident'
);
console.log(`Accidents in view: ${accidents.length}`);
Every setting as data
trafficIncidentsKnobCatalogue lists the module's plain-valued settings with their kind, range or options and, where one value holds everywhere, their default, readable with no map loaded:
import { trafficIncidentsKnobCatalogue } from '@tomtom-org/maps-sdk/map';
// [{ id: 'visible', kind: 'toggle', description: '…', default: false },
// { id: 'icons.visible', kind: 'toggle', description: '…' }, …,
// { id: 'filters.any.0.magnitudes.values', kind: 'enums', description: '…', options: ['unknown', 'minor', 'moderate', 'major', 'indefinite'] }, …]
| Id | Kind |
|---|---|
visible |
toggle, false by default |
icons.visible |
toggle; unset, the icons follow visible |
filters.any.0.roadCategories.show, filters.any.0.roadSubCategories.show, filters.any.0.incidentCategories.show, filters.any.0.magnitudes.show |
enum: only or all-except |
filters.any.0.roadCategories.values |
enums: the road categories |
filters.any.0.roadSubCategories.values |
enums: the street, then the service road subcategories |
filters.any.0.incidentCategories.values |
enums: fullTrafficIncidentCategories |
filters.any.0.magnitudes.values |
enums: the delay magnitudes |
filters.any.0.delays.mustHaveDelay |
toggle, false by default |
filters.any.0.delays.minDelayMinutes |
number, 0 to 120 minutes |
- The
filters.any.0knobs set the first of the filter alternatives; an incident matching any further alternative, which only the configuration sets, shows too. Eachshowknob goes with itsvaluesknob, and unset, nothing is filtered. trafficIncidentsKnobIdslists the ids,TrafficIncidentsKnobIdis their union, andTrafficIncidentsKnobValueOf<ID>the plain value one takes. The entry shape and its kinds are the modules guide's.- Not in it:
events. The look of the incidents is the styling catalogue'straffic.incidents.*knobs.
API reference
For complete documentation of all Traffic Incidents Module properties, methods, and types, see the TrafficIncidentsModule API Reference.
Related guides and examples
Traffic overview
- Traffic Overview - Overview of all traffic visualization options including flow, incidents, and route-specific traffic
Related traffic modules
- Traffic Flow Module - Display real-time traffic speed conditions
- Routing Module - Display routes with traffic incidents along the path
Related examples
- Traffic Incidents - Interactive traffic incidents visualization with filtering
- Traffic Config Playground - Explore traffic configuration options