Skip to main content
Version: 7.3.0

nearestPointOnLine

描述

🌐 Description

返回直线上距离给定点最近的点。

🌐 Returns the nearest point on a line to a given point.

如果输入线路字符串中的任何线段是对蹠的,因此弧未定义,该函数将返回该点位于该线上。

🌐 If any of the segments in the input line string are antipodal and therefore have an undefined arc, this function will instead return that the point lies on the line.

参数

🌐 Parameters

名称类型描述
linesGeometry | Feature<LineString | MultiLineString>对齐线
ptGeometry | Feature<Point> | Array<number>指向要捕捉的点
options?Object可选参数 (默认 {})
options.units?Units支持所有有效的 Turf 单位 (默认 '公里')

返回

🌐 Returns

Feature<Point> line 上到 point 的最近点。属性对象将包含四个值:index:最近点位于第 n 条线段上,multiFeatureIndex:最近点位于 MultiLineString 的第 n 条线,dist:pt 与最近点之间的距离,location:从起点到最近点沿线的距离。

示例

🌐 Examples

var line = turf.lineString([
[-77.031669, 38.878605],
[-77.029609, 38.881946],
[-77.020339, 38.884084],
[-77.025661, 38.885821],
[-77.021884, 38.889563],
[-77.019824, 38.892368],
]);
var pt = turf.point([-77.037076, 38.884017]);

var snapped = turf.nearestPointOnLine(line, pt, { units: "miles" });

安装

🌐 Installation

$ npm install @turf/nearest-point-on-line

import { nearestPointOnLine } from "@turf/nearest-point-on-line";
const result = nearestPointOnLine(...);
$ npm install @turf/turf

import * as turf from "@turf/turf";
const result = turf.nearestPointOnLine(...);