Routing Module
The Routing Module enables displaying calculated routes and waypoints on maps with comprehensive functionality for visualizing navigation routes, alternative routes, turn-by-turn guidance, traffic incidents that occur along the calculated path, and route-related interactions.
Initialization
import { TomTomMap, RoutingModule } from '@tomtom-org/maps-sdk/map';
// Initialize mapconst map = new TomTomMap({ mapLibre: { container: 'map' } });
// Initialize Routes module with default configurationconst routingModule = await RoutingModule.get(map);
// Or with custom configurationconst routingModule = await RoutingModule.get(map, { displayUnits: { distance: { type: 'metric' } }, waypointsSource: { entryPoints: 'main-when-available' }});Displaying Routes
Display calculated routes on the map:
import { calculateRoute, geocodeOne } from '@tomtom-org/maps-sdk/services';
// Calculate a route with coordinatesconst route = await calculateRoute({ locations: [ [4.9041, 52.3676], // Amsterdam [4.4777, 51.9244] // Rotterdam ],});
// Or calculate a route with geocoded locationsconst waypoints = await Promise.all([ geocodeOne('Amsterdam, Netherlands'), geocodeOne('Rotterdam, Netherlands')]);
const routes = await calculateRoute({ locations: waypoints });
// Show the routeawait routingModule.showRoutes(route);Displaying Guidance on Map
Request guidance instructions when calculating a route to show them visually:
import { calculateRoute } from '@tomtom-org/maps-sdk/services';
const route = await calculateRoute({ locations: [ [4.9041, 52.3676], // Amsterdam [4.4777, 51.9244] // Rotterdam ], guidance: { type: 'coded' }});
// Display route with guidance on mapawait routingModule.showRoutes(route);Route Selection
Change which route appears as selected when displaying multiple routes:
import { calculateRoute } from '@tomtom-org/maps-sdk/services';
// Calculate route with alternativesconst routes = await calculateRoute({ locations: [ [4.9041, 52.3676], // Amsterdam [4.4777, 51.9244] // Rotterdam ], maxAlternatives: 2,});
// Show the routes (first route selected by default)await routingModule.showRoutes(routes);
// Show the routes with specific selectionawait routingModule.showRoutes(routes, { selectedIndex: 1 });
// Switch to second routeawait routingModule.selectRoute(1);
// Switch back to first routeawait routingModule.selectRoute(0);Waypoints
Display and manage waypoints separately from routes:
// Show custom waypointsawait routingModule.showWaypoints([ [4.9041, 52.3676], // Amsterdam [4.4777, 51.9244] // Rotterdam]);
// Clear waypoints from the mapawait routingModule.clearWaypoints();Custom Styling
Customize route appearance through layer configuration:
import { defaultRouteLayersConfig } from '@tomtom-org/maps-sdk/map';
// Custom layer stylingconst customLayerSpec = { layers: { ...defaultRouteLayersConfig, mainLines: defaultRouteLayersConfig.mainLines.map(layer => ({ ...layer, layerSpec: { ...layer.layerSpec, paint: { ...(layer.layerSpec.paint || {}), 'line-color': '#FF0000', // Red color for main route line 'line-width': 6 } } })) }}
const routingModule = await RoutingModule.get(map, customLayerSpec);Events
Handle user interactions with routes and waypoints:
// Route click eventsroutingModule.events.mainLines.on('click', (feature, lngLat) => { console.log('Route clicked:', feature.properties); console.log('Coordinates:', lngLat);});
// Waypoint eventsroutingModule.events.waypoints.on('click', (feature, lngLat) => { console.log('Waypoint:', feature.properties);});
// Traffic incident eventsroutingModule.events.incidents.on('hover', (feature, lngLat) => { console.log('Traffic delay:', feature.features.map(feature => feature.properties.delayInSeconds));});
// Turn-by-turn instruction eventsroutingModule.events.instructionLines.on('click', (feature, lngLat) => { console.log('Instruction properties:', feature.properties);});
// Summary bubble eventsroutingModule.events.summaryBubbles.on('click', (feature, lngLat) => { console.log('Route summary:', feature.properties);});
// Route section eventsroutingModule.events.ferries.on('click', (feature, lngLat) => { console.log('Ferry section');});
routingModule.events.tollRoads.on('click', (feature, lngLat) => { console.log('Toll road section');});
routingModule.events.tunnels.on('click', (feature, lngLat) => { console.log('Tunnel section');});
routingModule.events.vehicleRestricted.on('click', (feature, lngLat) => { console.log('Vehicle restricted section');});
routingModule.events.chargingStops.on('click', (feature, lngLat) => { console.log('EV charging station');});
// Remove event listenersroutingModule.events.mainLines.off('click', handler);Multiple Module Instances
You can create multiple independent instances of the Routing module, each with its own configuration and styling. This is useful for comparing different route types or displaying routes for multiple users.
// Create separate instances for different route typesconst carRoutes = await RoutingModule.get(map, { customLayerSpec: { layers: { mainLines: [{ layerSpec: { paint: { 'line-color': '#2ECC71' } } }] } }});
const truckRoutes = await RoutingModule.get(map, { customLayerSpec: { layers: { mainLines: [{ layerSpec: { paint: { 'line-color': '#E67E22' } } }] } }});
// Each instance manages its own routes independentlyawait carRoutes.showRoutes(carRoute);await truckRoutes.showRoutes(truckRoute);API Reference
For complete documentation of all Routing Module properties, methods, and types, see the RoutingModule API Reference .
Related Guides and Examples
Services Integration
- Route Planning Parameters - Comprehensive route planning with configuration options
- Locations - Specifying route planning locations
- Geocoding - Convert addresses to coordinates for route waypoints and destinations
Related Examples
- Route - Basic route calculation and display on the map
- Route with Alternatives - Display multiple route options for comparison
- Waypoints - Multi-stop route visualization with interactive waypoints
- Map Route Reconstruction - Interactive route building and editing
Related Map Modules
- Places Module - Display route waypoints and destinations as interactive markers
- User Interaction Events - Handle user interactions with routes and route segments