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

Route dragging plugin

RouteDragging lets people edit a route by hand. They grab a stop's pin to move it, or pull the route line to add a stop, and the route re-plans while the pointer moves.

npm install @tomtom-org/maps-sdk maplibre-gl @tomtom-org/maps-sdk-plugin-route-dragging

@tomtom-org/maps-sdk and lodash-es are peer dependencies. lodash-es is one of the SDK's own, so it installs with the SDK.

Getting started

Create it over a RoutingModule, then give it the stops:

import { RoutingModule } from '@tomtom-org/maps-sdk/map';
import { RouteDragging } from '@tomtom-org/maps-sdk-plugin-route-dragging';

const routing = await RoutingModule.create(map);
const dragging = new RouteDragging(routing, {
    onRoutes: (routes) => showSummary(routes.features[0].properties.summary),
});

await dragging.setWaypoints([
    [4.8897, 52.3731], // Amsterdam
    [5.1214, 52.0907], // Utrecht
    [4.4777, 51.9244], // Rotterdam
]);

Re-planning while you drag

While a drag lasts, the route re-plans every planIntervalMS, 500 milliseconds by default, and once more on the drop.

By default it plans with calculateRoute({ locations, signal }). To route with your own options, pass plan:

new RouteDragging(routing, {
    plan: (locations, signal) =>
        calculateRoute({ locations, signal, travelMode: 'truck', maxAlternatives: 2 }),
});

Answers in order

The plans overlap rather than cancel each other, so a route that takes longer than the interval to calculate still shows during the drag.

The same rule is in the services as createLatestRequest({ supersede: 'on-answer' }), for any request that starts faster than it answers.

Your own state

When your app keeps the stops in a store of its own, as a React or Vue app does, set showOnMap: false. The plugin then shows nothing itself:

const dragging = new RouteDragging(routing, {
    showOnMap: false,
    onWaypointsChange: (waypoints, { type, kind, waypointIndex, position }) => {
        if (kind === 'insert' && type === 'dragstart') store.insertStop(waypointIndex, position);
        else store.moveStop(waypointIndex, position);
    },
    onRoutes: (routes) => store.setLiveRoutes(routes),
});

The route planner works this way. It edits its stops from the changes, shows the routes planned during a drag, and goes back to its own planning once the stops settle:

API reference

For complete documentation see the RouteDragging API Reference.

Related guides and examples