Routing Quickstart
The Routing Service calculates routes between locations with support for multiple waypoints, various vehicle types, and real-time traffic integration.
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.
Related Guides and Examples
Related Guides
- Route Planning Parameters - Comprehensive route planning with configuration options
- Locations - Specifying route planning locations
- Route Object - Understanding route structure
- Long Distance EV Routing - Electric vehicle routing with charging stops
Map Integration
- Routing Module - Display and interact with routes on the map
Related Examples
- Route with waypoints - Basic route calculation and display
- Route with alternatives - Multiple route options
- Waypoints - Multi-stop routing
- Node.js Routing - Server-side routing