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

Traffic flow module

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

The module owns the visibility, filters and look of the style's traffic-flow layers, including the colour of each flow level and the line width. See Who owns what.

To show live flow over a whole city, call get with visible: true. Move the center to see another city's traffic:

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

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

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

Filtering

Set filters at get() or later through updateConfig to narrow which roads show flow.

Road category

The playground below filters by road category with 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, widthFactor (0.5 to 2) multiplies the width of the flow lines, and opacityFactor (0 to 1) their opacity, so you can fade the flow under a route of your own.

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,
    opacityFactor: 0.6,
});
setKnob(trafficFlowModule, trafficFlowKnobCatalogue, 'colors.free', '#16a34a');

getStyleColor and the bloom scope of the map effects plugin read the level colours by their map-wide ids, such as traffic.flow.colors.slow, with or without the module.

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 comes only as vector tiles. There is no service for a road, a point or an area, so getRenderedFeatures() is your only source of flow data.

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 any default that holds. You can read it 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 by default
widthFactor factor 0.5–2, 1 by default
opacityFactor factor 0–1, 1 by default

API reference

TrafficFlowModule, TrafficFlowModuleConfig and TrafficFlowModuleFeature.

Related guides and examples

Related guides

Related examples