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

Traffic incidents module

TrafficIncidentsModule controls the style's live traffic-incident layers: the affected stretch of road as a line coloured by delay, and a category icon per incident — accidents, closures, roadworks, weather — across the whole map.

What this module owns: whether the style's incident layers and icons are shown, which incidents, and how they look — visibility, filters, and the colour of each severity and the line width. Incidents you fetched yourself are drawn by the Traffic Incident Details Module instead. See Who owns what.

Initialization

import { TomTomMap, TrafficIncidentsModule } from '@tomtom-org/maps-sdk/map';

const map = new TomTomMap({ mapLibre: { container: 'map' } });
const trafficIncidentsModule = await TrafficIncidentsModule.get(map);

Visibility

Incidents are hidden until shown. isVisible() reads the visible setting back, false while unset.

const trafficIncidentsModule = await TrafficIncidentsModule.get(map, { visible: true });

trafficIncidentsModule.setVisible(false);
trafficIncidentsModule.isVisible(); // false

Icon visibility

The icons show or hide apart from the lines, and always show the same incidents: the filters narrow both.

// Lines without icons
const trafficIncidentsModule = await TrafficIncidentsModule.get(map, { visible: true, icons: { visible: false } });
trafficIncidentsModule.isIconsVisible(); // false

// Icons alone
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. Whether a style layer itself is shown is map.mapLibreMap.getLayoutProperty(layerId, 'visibility').

Filtering

filters narrows which incidents show, lines and icons alike, at get() or later through updateConfig. It works as the flow filters do: the fields are one filter, every field set must match, and each entry of its any list is an alternative.

Incident category

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

// Every incident but the weather
trafficIncidentsModule.updateConfig({
    filters: { incidentCategories: { show: 'all-except', values: ['rain', 'fog', 'frost', 'wind', 'flooding'] } },
});

The values are the incident categories of fullTrafficIncidentCategories, the tile-only animals-on-road and narrow-lanes included.

Severity

The playground below filters by delay magnitude from checkboxes:

trafficIncidentsModule.updateConfig({
    filters: { magnitudes: { show: 'only', values: ['major', 'moderate'] } },
});

The values are the delay magnitudes: unknown, minor, moderate, major and indefinite.

Delay duration

// At least 5 minutes of delay; incidents reporting no delay drop out too
trafficIncidentsModule.updateConfig({
    filters: { delays: { mustHaveDelay: true, minDelayMinutes: 5 } },
});

Road category

roadCategories and roadSubCategories take the road categories and subcategories of the flow filters:

trafficIncidentsModule.updateConfig({
    filters: { roadCategories: { show: 'only', values: ['motorway', 'trunk'] } },
});

Multiple filters

// Major accidents, or a road closure of any magnitude
trafficIncidentsModule.updateConfig({
    filters: {
        incidentCategories: { show: 'only', values: ['accident'] },
        magnitudes: { show: 'only', values: ['major'] },
        any: [{ incidentCategories: { show: 'only', values: ['road-closed'] } }],
    },
});

Reset filters

trafficIncidentsModule.updateConfig({ filters: undefined });

Appearance

colors recolours the incident lines per delay severity, and widthFactor (0.5 to 2) multiplies their width.

import { setKnob, TrafficIncidentsModule, trafficIncidentsKnobCatalogue } from '@tomtom-org/maps-sdk/map';

const trafficIncidentsModule = await TrafficIncidentsModule.get(map, {
    visible: true,
    colors: { major: '#b91c1c', closed: '#111827' },
    widthFactor: 1.5,
});
setKnob(trafficIncidentsModule, trafficIncidentsKnobCatalogue, 'colors.minor', '#facc15');

Events

events.on takes click, hover, long-hover and the other user events; the handler receives the incident as a TrafficIncidentsModuleFeature.

const unsubscribe = trafficIncidentsModule.events.on('click', (incident, lngLat) => {
    const { id, category, magnitudeOfDelay, delayInSeconds, description } = incident.properties;
});

Querying visible features

getRenderedFeatures() returns the incidents rendered in the current viewport, lines and icons:

const { trafficIncidents } = trafficIncidentsModule.getRenderedFeatures();
const accidents = trafficIncidents.filter((incident) => incident.properties.category === 'accident');

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.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.roadCategories.show, filters.roadSubCategories.show, filters.incidentCategories.show, filters.magnitudes.show enum: only or all-except
filters.roadCategories.values enums: the road categories
filters.roadSubCategories.values enums: the street, then the service road subcategories
filters.incidentCategories.values enums: fullTrafficIncidentCategories
filters.magnitudes.values enums: the delay magnitudes
filters.delays.mustHaveDelay toggle, false by default
filters.delays.minDelayMinutes number, 0 to 120 minutes
colors.minor, colors.moderate, colors.major, colors.closed colour, the style's own while unset
widthFactor factor 0.5–2, 1 by default

API reference

TrafficIncidentsModule, TrafficIncidentsModuleConfig and TrafficIncidentsModuleFeature.

Related guides and examples

Related guides

Related examples