Open source · MIT · JavaScript, TypeScript and PHP

Snap GPS traces to the roads they were driven on

A hidden Markov model turns noisy fixes into the route a vehicle took. It runs in the browser, Node.js and PHP with the same results, no routing server and no dependencies.

Over 100,000 points per second in Node.js 22, up to eight times faster than the engine it grew out of.

Click or tap the map to place GPS points in the order they were driven. Drag a point to move it. The route updates after every change.

Points–
Matched in–
Typical error–

Quick start in JavaScript, TypeScript or PHP

Give it road lines and GPS points; get back the path on the road, point by point. The npm package runs from plain JavaScript with import or require, from a script tag with no build step, and ships its own TypeScript types.

import { buildNetwork, matchCoordinates } from 'route-map-matching';

const network = buildNetwork([
  {
    coordinates: [[106.822, -6.175], [106.830, -6.174]],
    oneway: 1,
  },
  [[106.830, -6.174], [106.830, -6.180]],
]);

const route = matchCoordinates(gpsPoints, network);
PHP
use RouteMapMatching\Matcher;
use RouteMapMatching\Network;

$network = Network::build([
    [
        'coordinates' => [[106.822, -6.175], [106.830, -6.174]],
        'oneway' => 1,
    ],
    [[106.830, -6.174], [106.830, -6.180]],
]);

$route = Matcher::matchCoordinates($gpsPoints, $network);
Roads straight from your map's vector tiles
import { createTileSource } from 'route-map-matching/tiles';

const tiles = createTileSource({ url: 'https://tiles.example.org/{z}/{x}/{y}.pbf' });
const { coordinates, complete } = await tiles.match(gpsPoints);

API reference for JavaScript and PHP

Every function has the same name and the same result in JavaScript, TypeScript and PHP. Coordinates are [lng, lat] in degrees.

JavaScript and TypeScriptPHPReturns
buildNetwork(roads)Network::build($roads)A road graph you can reuse for many traces.
matchPoints(points, network, options)Matcher::matchPoints($points, $network, $options)One entry per input point: position, the road path before it, skipped and matched.
matchCoordinates(coordinates, network, options)Matcher::matchCoordinates(...)The matched line as [lng, lat] pairs, ready to draw.
snapToRoad(network, [lng, lat], radius)Matcher::snapToRoad(...)The nearest point on a road, or null.
createTileSource({ url })–Downloads, caches and decodes Mapbox vector tiles, then matches.
isLeftHandTraffic(lng, lat)LeftHandTraffic::contains(...)Whether traffic keeps left at that spot.
OptionDefaultWhat it does
radius50 mHow far from a fix a road may be. Widens to 100 m and 200 m when nothing is closer.
includeevery pointBooleans or a function. Leave parked or stationary fixes out of the match.
leftHandTraffic'auto'Looks up the side of the road for each fix in 75 countries and territories. Pass true, false, an array or a function to override.

How a trace becomes a route

The same four stages run in both languages, tuned on trips from a production fleet tracker.

  1. Candidates

    Up to six road positions near each fix, scored by distance, heading, road class, one-way rules and the side traffic keeps to.

  2. Transitions

    A bounded Dijkstra search measures the road distance between candidates. Routes about as long as the straight GPS hop score best.

  3. Viterbi

    The cheapest chain of candidates across the whole trip wins, so one bad fix cannot drag the route onto a parallel street.

  4. Clean-up

    Gaps are routed, detours straightened and false U-turns removed, which keeps signal loss and multipath from showing up as zigzags.

Benchmarks on a 40 by 40 block city grid

Median of seven runs in Node.js and three in PHP on a laptop, 3,280 road segments, GPS noise of 7 m. Output matches the original engine point for point.

GPS pointsOriginal engineNode.js 22Speed-up
1,00071 ms16 ms4.5×
5,000315 ms47 ms6.7×
20,0001,212 ms150 ms8.1×
GPS pointsPHP 8.4PHP 8.4 with JIT
1,00089 ms55 ms
5,000529 ms268 ms
20,0002,006 ms931 ms

Run npm run bench and composer bench to measure on your own hardware.

Keeps left where traffic keeps left

On a divided road the two carriageways sit metres apart, closer than GPS noise. In the shaded countries the matcher leans toward the left lane, everywhere else it stays neutral. The boundaries come from Natural Earth and hold up at the Hong Kong and Shenzhen, Macau and Zhuhai, and Singapore and Johor borders.

  • 75 countries and territories
  • 32 KB gzipped, decoded on first use
  • 2 µs per lookup