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

Traffic flow module

TrafficFlowModule controls the style's live traffic-flow layers, which colour every road by its speed relative to free flow, from free-flowing to stationary, with closed roads outlined.

What this module owns: whether the style's traffic-flow layers are shown, for which roads, and how they look — visibility, filters, and the colour of each flow level and the line width. See Who owns what.

Initialization

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

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

Visibility

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

const trafficFlowModule = await TrafficFlowModule.get(map, { visible: true });

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

Filtering

filters narrows which roads show flow, at get() or later through updateConfig:

Road category

The playground below filters by road category from checkboxes:

// Major roads only
trafficFlowModule.updateConfig({
    filters: { roadCategories: { show: 'only', values: ['motorway', 'motorway_link', 'trunk'] } },
});

// Every road but residential streets
trafficFlowModule.updateConfig({
    filters: { roadSubCategories: { show: 'all-except', values: ['residential'] } },
});
roadCategories value Road
motorway, motorway_link High-capacity highways with restricted access, and their ramps
trunk, trunk_link Major inter-city roads
primary, primary_link Primary through routes
secondary, secondary_link Secondary through routes
tertiary, tertiary_link Connecting roads
street Local streets, with a subcategory
service Service roads, with a subcategory
track Unpaved or agricultural tracks

Road closures

trafficFlowModule.updateConfig({ filters: { roadClosures: 'only' } });       // closed roads only
trafficFlowModule.updateConfig({ filters: { roadClosures: 'all-except' } }); // every road but them

Multiple filters

// Motorways, or a closed road of any category
trafficFlowModule.updateConfig({
    filters: {
        roadCategories: { show: 'only', values: ['motorway'] },
        any: [{ roadClosures: 'only' }],
    },
});

Reset filters

trafficFlowModule.updateConfig({ filters: undefined });

Appearance

colors recolours each flow level, and widthFactor (0.5 to 2) multiplies the width of the flow lines.

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

const trafficFlowModule = await TrafficFlowModule.get(map, {
    visible: true,
    colors: { slow: '#f59e0b', stationary: '#7f1d1d' },
    widthFactor: 1.5,
});
setKnob(trafficFlowModule, trafficFlowKnobCatalogue, 'colors.free', '#16a34a');

getStyleColor reads the level colours by their map-wide ids, such as traffic.flow.colors.slow, with or without the module, and so does the map effects plugin's bloom scope.

Events

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

const unsubscribe = trafficFlowModule.events.on('click', (segment, lngLat) => {
    console.log(segment.properties.relativeSpeed, segment.properties.roadCategory);
});

on returns the function that removes the handler; events.off('click') removes every click handler of the module (Cleanup).

Querying visible features

getRenderedFeatures() returns the flow segments rendered in the current viewport:

const { trafficFlow } = trafficFlowModule.getRenderedFeatures();
const closedRoads = trafficFlow.filter((segment) => segment.properties.roadClosure);
const congested = trafficFlow.filter((segment) => segment.properties.relativeSpeed < 0.5);

What the flow data can and cannot tell you

Orbis traffic flow is vector tiles only, so getRenderedFeatures() is the whole of flow as data: there is no service for a road, a point or an area.

For historical speed and congestion aggregated over an area, use the Traffic Area Analytics service.

Every setting as data

trafficFlowKnobCatalogue lists the module's plain-valued settings with their kind, options and, where one holds, their default, readable with no map loaded:

import { trafficFlowKnobCatalogue } from '@tomtom-org/maps-sdk/map';

// [{ id: 'visible', kind: 'toggle', description: '…', default: false },
//  { id: 'filters.roadCategories.show', kind: 'enum', description: '…', options: ['only', 'all-except'] },
//  { id: 'filters.roadCategories.values', kind: 'enums', description: '…', options: ['motorway', …, 'track'] }, …]
Id Kind
visible toggle, false by default
filters.roadCategories.show, filters.roadSubCategories.show, filters.roadClosures enum: only or all-except
filters.roadCategories.values enums: the road categories
filters.roadSubCategories.values enums: the street, then the service road subcategories
colors.free, colors.slow, colors.queueing, colors.stationary, colors.closed colour, the style's own while unset
widthFactor factor 0.5–2, 1 by default

API reference

TrafficFlowModule, TrafficFlowModuleConfig and TrafficFlowModuleFeature.

Related guides and examples

Related guides

Related examples