Back to all examples

Reachable range service NodeJS

This example on GitHub

Areas reachable from a geocoded address within a drive time and a distance, summarised by a Node.js process

Reachable range service NodeJS
import { type BBox, budgetUnits, TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { calculateReachableRanges, geocodeOne } from '@tomtom-org/maps-sdk/services';
import { API_KEY } from './config';

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

const formatBBox = (bbox: BBox) => bbox.map((value) => value.toFixed(3)).join(', ');

(async () => {
    const origin = await geocodeOne('Domplein 9, Utrecht');
    console.log(`Origin: ${origin.properties.address.freeformAddress}\n`);

    const ranges = await calculateReachableRanges([
        { origin, budget: { type: 'timeMinutes', value: 30 } },
        { origin, budget: { type: 'distanceKM', value: 25 } },
    ]);

    for (const range of ranges.features) {
        const { budget } = range.properties;
        const vertices = range.geometry.coordinates.flat(1).length;
        console.log(
            `${budget.value} ${budgetUnits[budget.type]}: ${vertices} vertices, bbox [${formatBBox(range.bbox)}]`,
        );
    }

    if (ranges.bbox) console.log(`\nBoth ranges fit in [${formatBBox(ranges.bbox)}]`);
})();

An address is geocoded, and the areas reachable from it within a drive time and within a distance print as a vertex count and a bounding box each.

  • geocodeOne returns a place that calculateReachableRanges takes as its origin unchanged.
  • calculateReachableRanges answers each budget with a GeoJSON polygon whose properties echo the request, so its budget labels the line through budgetUnits.
  • Each polygon, and the collection, arrives with its bbox already computed, so nothing walks the vertices to find it.

Related examples