经纬志
首页 / Turf.js / API 文档

nearestPoint

先看结论

在点集合里找到离目标最近的一个,直接返回该要素。

最后验证 2026-11-03 · Turf.js 7.x · 更新 2026-10-27 · 引用本文
turf.nearestPoint(targetPoint, points) → Feature

参数

参数类型说明
targetPointCoord / Feature目标点
pointsFeatureCollection候选点集合

返回值

最近的 Point 要素,属性里带上 dist(公里)与 featureIndex(在原集合中的下标)。集合为空时返回 undefined。

示例

var stations = turf.featureCollection([
  turf.point([116.42, 39.91], { name: 'A' }),
  turf.point([116.35, 39.94], { name: 'B' }),
  turf.point([116.48, 39.88], { name: 'C' })
]);

var near = turf.nearestPoint(turf.point([116.41, 39.92]), stations);
console.log(near.properties.name, near.properties.dist);

只支持点集

候选集合里混入线或面时,结果不可预期。 找最近的线上的位置用 nearestPointOnLine, 找最近的面需要自己遍历并计算 pointToPolygonDistance。

结果对象是原要素的副本。修改返回值的属性不会影响原集合, 需要回写下标时用 featureIndex。

引用本文

经纬志(2026)。《nearestPoint》。https://www.jingweizhi.com/turf/api/nearest-point.html(访问日期 2026-11-03)。