用 JavaScript、TypeScript 或 PHP 快速上手
提供道路线和 GPS 点,就能逐点得到贴合道路的路径。npm 包可以直接在普通 JavaScript 中通过 import 或 require 使用,也能用 script 标签免构建加载,并自带 TypeScript 类型。
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,
);
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);
直接使用地图的矢量瓦片作为道路
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);
JavaScript 与 PHP 的 API 参考
每个函数在 JavaScript、TypeScript 和 PHP 中名称相同、结果相同。坐标格式为 [lng, lat],单位为度。
| JavaScript 和 TypeScript | PHP | 返回值 |
|---|---|---|
buildNetwork(roads) | Network::build($roads) | 可重复用于多条轨迹的道路图。 |
matchPoints(points, network, options) | Matcher::matchPoints($points, $network, $options) | 每个输入点对应一项:position、它之前的道路 path、skipped 和 matched。 |
matchCoordinates(coordinates, network, options) | Matcher::matchCoordinates(...) | 匹配后的线,以 [lng, lat] 坐标对表示,可直接绘制。 |
snapToRoad(network, [lng, lat], radius) | Matcher::snapToRoad(...) | 道路上最近的点,或 null。 |
createTileSource({ url }) | – | 下载、缓存并解码 Mapbox 矢量瓦片,然后进行匹配。 |
isLeftHandTraffic(lng, lat) | LeftHandTraffic::contains(...) | 该位置是否靠左行驶。 |
| 选项 | 默认值 | 作用 |
|---|---|---|
radius | 50 m | 道路距离定位点的最大范围。附近没有道路时会扩大到 100 m 和 200 m。 |
include | 所有点 | 布尔数组或函数。把停车或静止时的定位点排除在匹配之外。 |
leftHandTraffic | 'auto' | 在 75 个国家和地区中为每个定位点判断行驶方向。传入 true、false、数组或函数可以覆盖。 |
轨迹如何变成路线
两种语言运行同样的四个阶段,并用生产环境车队追踪系统的真实行程调校过。
-
候选点
每个定位点附近最多六个道路位置,按距离、航向、道路等级、单行规则和行驶方向打分。
-
转移
有界 Dijkstra 搜索测量候选点之间的道路距离。长度接近 GPS 直线跳跃距离的路线得分最高。
-
Viterbi
整段行程中代价最低的候选链胜出,因此单个坏点无法把路线拉到平行的街道上。
-
清理
为断档补全路线,拉直绕路,去掉错误的掉头,避免信号丢失和多路径效应表现为锯齿。
40 × 40 街区城市网格上的性能测试
在笔记本电脑上,Node.js 取七次运行的中位数,PHP 取三次,共 3,280 段道路,GPS 噪声 7 m。输出与原始引擎逐点一致。
| GPS 点数 | 原始引擎 | Node.js 22 | 提速 |
|---|---|---|---|
| 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 点数 | PHP 8.4 | PHP 8.4 开启 JIT |
|---|---|---|
| 1,000 | 89 ms | 55 ms |
| 5,000 | 529 ms | 268 ms |
| 20,000 | 2,006 ms | 931 ms |
运行 npm run bench 和 composer bench,在你自己的硬件上测量。