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.
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);
<script type="module">
import { buildNetwork, matchCoordinates } from
'https://cdn.jsdelivr.net/npm/route-map-matching@1/dist/index.js';
const network = buildNetwork([
[[106.822, -6.175], [106.830, -6.174]],
[[106.830, -6.174], [106.830, -6.180]],
]);
const route = matchCoordinates(gpsPoints, network);
</script>
import { buildNetwork, matchPoints } from 'route-map-matching';
import type { MatchedPoint, RoadInput } from 'route-map-matching';
const roads: RoadInput[] = [
{ coordinates: [[106.822, -6.175], [106.830, -6.174]], oneway: 1 },
[[106.830, -6.174], [106.830, -6.180]],
];
const network = buildNetwork(roads);
const matched: MatchedPoint[] = matchPoints(
gpsPoints.map(([lng, lat]) => ({ lng, lat })),
network,
);
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);
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 TypeScript | PHP | Returns |
|---|---|---|
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. |
| Option | Default | What it does |
|---|---|---|
radius | 50 m | How far from a fix a road may be. Widens to 100 m and 200 m when nothing is closer. |
include | every point | Booleans 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.
-
Candidates
Up to six road positions near each fix, scored by distance, heading, road class, one-way rules and the side traffic keeps to.
-
Transitions
A bounded Dijkstra search measures the road distance between candidates. Routes about as long as the straight GPS hop score best.
-
Viterbi
The cheapest chain of candidates across the whole trip wins, so one bad fix cannot drag the route onto a parallel street.
-
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 points | Original engine | Node.js 22 | Speed-up |
|---|---|---|---|
| 1,000 | 71 ms | 16 ms | 4.5× |
| 5,000 | 315 ms | 47 ms | 6.7× |
| 20,000 | 1,212 ms | 150 ms | 8.1× |
| GPS points | PHP 8.4 | PHP 8.4 with JIT |
|---|---|---|
| 1,000 | 89 ms | 55 ms |
| 5,000 | 529 ms | 268 ms |
| 20,000 | 2,006 ms | 931 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