20 lines
611 B
JavaScript
20 lines
611 B
JavaScript
// utils/geometryUtils.js
|
|
|
|
export const findClosestPoints = (coordinates, newPoint, map) => {
|
|
if (!map) {
|
|
console.error("Map is not defined. Cannot find closest points.");
|
|
return [];
|
|
}
|
|
|
|
let minDist = Infinity;
|
|
let closestPair = [];
|
|
for (let i = 1; i < coordinates.length; i++) {
|
|
const dist = L.LineUtil.pointToSegmentDistance(map.latLngToLayerPoint(newPoint), map.latLngToLayerPoint(coordinates[i - 1]), map.latLngToLayerPoint(coordinates[i]));
|
|
if (dist < minDist) {
|
|
minDist = dist;
|
|
closestPair = [coordinates[i - 1], coordinates[i], i];
|
|
}
|
|
}
|
|
return closestPair;
|
|
};
|