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

Traffic incidents module

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

The module owns the visibility, filters and look of the style's incident layers and icons, including the colour of each severity and the line width, and whether it shows the incidents under way, the planned ones or both. The Traffic Incident Details Module draws incidents you fetched yourself. See Who owns what.

To show every live incident in view, call get with visible: true. Move the center to see another city's incidents:

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 you show them. isVisible() returns the visible setting, false by default.

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

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

Icon visibility

You can hide the icons and the lines separately, but the filters narrow both to the same incidents.

// 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. To check whether a style layer itself is shown, call map.mapLibreMap.getLayoutProperty(layerId, 'visibility').

Planned incidents

timeValidity picks which incidents the tiles bring. present ones are under way, and future ones are planned, such as scheduled roadworks and closures. If you leave it unset or empty, only the present ones show.

// Planned incidents alongside the ones under way
trafficIncidentsModule.updateConfig({ timeValidity: ['present', 'future'] });

// Planned ones only
trafficIncidentsModule.updateConfig({ timeValidity: ['future'] });

Filtering

Set filters at get() or later through updateConfig to narrow which incidents show, lines and icons alike. Like the flow filters, the fields form one filter. Every field you 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, including the tile-only animals-on-road and narrow-lanes.

Severity

The playground below filters by delay magnitude with 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: { required: true, minMinutes: 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, widthFactor (0.5 to 2) multiplies their width, and opacityFactor (0 to 1) their opacity.

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');
setKnob(trafficIncidentsModule, trafficIncidentsKnobCatalogue, 'opacityFactor', 0.5);

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 incident lines and icons rendered in the current viewport:

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 their default where one value holds everywhere. You can read it 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. If unset, the icons follow visible
timeValidity enums: present, future. ['present'] by default
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.required toggle, false by default
filters.delays.minMinutes 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
opacityFactor factor 0–1, 1 by default

API reference

TrafficIncidentsModule, TrafficIncidentsModuleConfig and TrafficIncidentsModuleFeature.

Related guides and examples

Related guides

Related examples