Routing Quickstart

The Routing Service calculates routes between locations with support for multiple waypoints, various vehicle types, and real-time traffic integration.

import { TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { calculateRoute } from '@tomtom-org/maps-sdk/services';
import { API_KEY } from './config';

TomTomConfig.instance.put({ apiKey: API_KEY });

(async () => {
    const response = await calculateRoute({
        locations: [
            [4.89066, 52.37317],
            [4.47059, 51.92291],
        ],
    });
    console.log(JSON.stringify(response.features[0].properties, null, 4));
})();

Initialization

Configure the SDK with your API key:

import { TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { calculateRoute } from '@tomtom-org/maps-sdk/services';
TomTomConfig.instance.put({
apiKey: 'YOUR_API_KEY_HERE'
});

Calculating a Route

Calculate a route between two locations:

const route = await calculateRoute({
locations: [
[4.9041, 52.3676], // Amsterdam
[2.3522, 48.8566] // Paris
]
});

Calculate a route between two geocoded locations:

import { geocodeOne } from '@tomtom-org/maps-sdk/services';
const waypoints = await Promise.all([
geocodeOne('Amsterdam, Netherlands'),
geocodeOne('Paris, France')
]);
const route = await calculateRoute({ locations: waypoints });

Calculate a route with extra options:

const routes = await calculateRoute({
locations: [[4.9041, 52.3676], [2.3522, 48.8566]],
costModel: {
avoid: ['tollRoads'],
},
when: {
option: 'departAt',
date: new Date(Date.now() + 15 * 60 * 1000), // 15 minutes from now
},
maxAlternatives: 2
});

Accessing Route Information

const route = await calculateRoute({
locations: [[4.9041, 52.3676], [2.3522, 48.8566]]
});
console.log(`Distance: ${route.features[0].properties.summary.lengthInMeters}m`);
console.log(`Duration: ${route.features[0].properties.summary.travelTimeInSeconds}s`);

Showing a Route on the Map

Display the calculated route using the Routing Module:

import { TomTomMap, RoutingModule } from '@tomtom-org/maps-sdk/map';
// Initialize map
const map = new TomTomMap({ mapLibre: { container: 'map' } });
// Initialize Routing Module
const routingModule = await RoutingModule.get(map);
// Calculate route
const route = await calculateRoute({
locations: [[4.9041, 52.3676], [2.3522, 48.8566]]
});
// Display on map
await routingModule.showRoutes(route);

API Reference

For complete documentation of all route calculation properties and types, see the calculateRoute API Reference .

Map Integration