Skip to main content

Routing Guide

The routing package provides route calculation and navigation capabilities. It uses under the hood our Routing API

Features

  • Route Calculation: Calculate routes between two points using various travel modes
  • Multiple Itineraries: Get multiple route options sorted by preference
  • Travel Modes: Support for walking, driving, transit, and more
  • Wheelchair Support: Options to avoid stairs and escalators for accessibility

Getting Started

import { Router, Coordinates } from '@wemap/routing';
import { core } from '@wemap/core';

// Initialize core SDK first
await core.init({ emmid: '...', token: '...' });

// Create router and calculate route
const router = new Router();

const origin = new Coordinates(48.8566, 2.3522);
const destination = new Coordinates(48.8606, 2.3376);

const itineraries = await router.directions(
origin,
destination,
'WALK',
{ isWheelchair: true }
);

// Use the first (usually best) itinerary
const bestRoute = itineraries[0];
console.log(`Distance: ${bestRoute.distance}m`);
console.log(`Duration: ${bestRoute.duration}s`);

Travel Modes

  • WALK: Pedestrian routes
  • BIKE: Bicycle routes
  • CAR: Car routes
  • TRANSIT: Public transportation routes

Route Options

  • isWheelchair: Avoid stairs and escalators (Person with Reduced Mobility)
  • departureDate: Date — specify departure time (for transit routes)
  • itineraryRules: string[] — extra routing rules to apply

See the RouteOptions API reference for the full type.

Integration with Positioning

You can integrate the routing package with the positioning package to create a navigation including map matching and navigation informations like next steps, distance to destination, etc.

Learn more about the Positioning package →

import { Router, Coordinates, ItineraryInfoManager } from '@wemap/routing';
import { GnssWifiLocationSource, UserPosition, MapMatching, type Pose } from '@wemap/positioning';

const router = new Router();
const locationSource = new GnssWifiLocationSource();
await locationSource.start();

const destination = new Coordinates(48.8606, 2.3376);

let userPosition: UserPosition | null = null;

locationSource.onUpdate((pose: Pose) => {
userPosition = pose.position ?? null;
});

/*
* Later in your code, once you have a user position.
* Calculate route from user position to destination.
*/
if (!userPosition) {
throw new Error('Waiting for a first position fix before routing');
}

const itineraries = await router.directions(userPosition, destination, 'WALK');
const itinerary = itineraries[0];

MapMatching.setItinerary(itinerary);

const itineraryInfoManager = new ItineraryInfoManager();

// Set itinerary for ItineraryInfoManager
itineraryInfoManager.itinerary = itinerary;

const navigationInfo = itineraryInfoManager.getInfo(userPosition);
// ^? ItineraryInfo<U extends Coordinates = Coordinates> = {
// nextStep: Step | null;
// previousStep: Step | null;
// projection: GraphProjection<U>;
// leg: Leg;
// traveledDistance: number;
// remainingDistance: number;
// traveledPercentage: number;
// remainingPercentage: number;
// };