This is the documentation of version 0.62.1. Go to the latest version, 1.0.0-rc.5.

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 Details Module instead. See Who owns what.

Looking for something else?

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 the lines shown and the icons hidden
const trafficIncidentsModule = await TrafficIncidentsModule.get(map, {
    visible: true,
    icons: {
        visible: false,
    },
});
trafficIncidentsModule.isIconsVisible(); // false

// Show/hide incident icons but keep incident areas visible
trafficIncidentsModule.setIconsVisible(true);
trafficIncidentsModule.setIconsVisible(false);

// Show only the icons
trafficIncidentsModule.setVisible(false);
trafficIncidentsModule.setIconsVisible(true);
trafficIncidentsModule.isIconsVisible(); // true

// setVisible clears icons.visible: the icons follow visible again
trafficIncidentsModule.setVisible(true);
trafficIncidentsModule.getConfig(); // { visible: true }

isIconsVisible() reports the setting. To read whether a style layer itself is shown, use map.mapLibreMap.getLayoutProperty(layerId, 'visibility').

Filtering

A filter can be applied either during module initialization or later through updateConfig. 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.updateConfig({ filters: ... });

Incident category

Display only specific types of incidents:

// Show only accidents and road closures
trafficIncidentsModule.updateConfig({
    filters: {
        any: [{
            incidentCategories: {
                show: 'only',
                values: ['accident', 'road-closed']
            }
        }]
    }
});

// Hide weather-related incidents
incidents.updateConfig({
    filters: {
        any: [{
            incidentCategories: {
                show: 'all-except',
                values: ['rain', 'fog', 'frost', 'wind']
            }
        }]
    }
});

Available Incident Categories:

Severity

Filter incidents based on their delay magnitude. The playground below filters by magnitude from checkboxes:

// Show moderate and major delays
incidents.updateConfig({
    filters: {
        any: [{
            magnitudes: {
                show: 'only',
                values: ['major', 'moderate']
            }
        }]
    }
});

Available Magnitudes:

Delay duration

Filter incidents by minimum delay:

// Show incidents with at least 5 minutes delay
incidents.updateConfig({
    filters: {
        any: [{
            delays: {
                mustHaveDelay: true,
                minDelayMinutes: 5
            }
        }]
    }
});

Road category

Display incidents only on specific road types:

// Show incidents only on highways
incidents.updateConfig({
    filters: {
        any: [{
            roadCategories: {
                show: 'only',
                values: ['motorway', 'trunk']
            }
        }]
    }
});

// Show incidents only on major local roads within tertiary category
incidents.updateConfig({
    filters: {
        any: [{
            roadCategories: {
                show: 'only',
                values: ['tertiary']
            },
            roadSubCategories: {
                show: 'only',
                values: ['major_local']
            }
        }]
    }
});

Available Road Categories:

Available Road Subcategories:

Multiple filters

Combine multiple filter criteria:

// Show major accidents OR road closures
incidents.updateConfig({
    filters: {
        any: [
            {
                incidentCategories: { show: 'only', values: ['accident'] },
                magnitudes: { show: 'only', values: ['major'] }
            },
        ]
    }
});

Reset filters

Remove all filters and show all incidents:

incidents.updateConfig({ filters: 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

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

Related traffic modules

Related examples