All examplesRouting
Get directions
Point-to-point directions over the Canada road graph. route() returns the distance in metres, the duration in seconds, and a GeoJSON LineString you can add as a source.
live result
the code that produced it
import { Unmap } from "@unmap/sdk";
const unmap = new Unmap({
key: "um_live_...",
container: "map",
center: [-114.03, 51.07],
zoom: 11,
});
const route = await unmap.router.route([-114.0719, 51.0447], [-113.9871, 51.0899], { mode: "auto" });
unmap.map!.on("load", () => {
unmap.map!.addSource("route", {
type: "geojson",
data: { type: "Feature", properties: {}, geometry: route.geometry },
});
unmap.map!.addLayer({
id: "route-line",
type: "line",
source: "route",
layout: { "line-cap": "round", "line-join": "round" },
paint: { "line-color": "#FF3E9A", "line-width": 3 },
});
});Coordinates are [lng, lat]. There are no intermediate waypoints: a request carries exactly two points. The first and last coordinates of the geometry are snapped to the nearest routable edge, so they sit near the points you asked for rather than exactly on them.
The road network covers Canada only. A point in the United States, or a route that would have to leave Canada, fails. Iqaluit is not road-connected to the mainland.
The Routing API documents every parameter, including the truck vehicle profile. @unmap/routing is the same client with no map dependency.