Skip to content

50,000 free calls a month, card-free. Get an API key →

Documentation menu
docs / examples / matrix
Example

Distance and duration matrix

Many-to-many travel times and distances: the workhorse behind "which of my drivers is genuinely closest, by road, in a lorry". Unreachable pairs come back as honest nulls, never a sentinel cost.

ts
import { PlacesLayer } from "@mapmap/maps";

const stores = new PlacesLayer(map, {
  places: [
    { id: "bhm-01", name: "Birmingham", lat: 52.4862, lon: -1.8904 },
    { id: "man-01", name: "Manchester", lat: 53.4808, lon: -2.2426 },
  ],
});

// Straight-line nearest, no network call.
stores.nearest({ lat: 51.5, lon: -0.13 }, 3);

// Nearest by real driven time, through POST /matrix, in truck costing.
// Sorted by durationS, driven distanceM attached, unreachable places dropped.
const closest = await stores.nearestByDriveTime(
  { lat: 51.5, lon: -0.13 },
  { n: 3, costing: "truck" },
);