feat: Refactor utils.js into specialized utility modules
- Split utils.js into three separate files to enhance modularity and maintainability: 1. geometryUtils.js: Contains geometry-related functions like parsePoint and findClosestPoints. 2. mapUtils.js: Contains functions related to map operations such as redrawPolyline and saveLineData. 3. markerUtils.js: Contains functions related to marker operations like insertNewMarker and handleEditPoi. - Updated import statements in the relevant files to reflect the new structure. - Ensured that each utility module is self-contained and has clear responsibilities. This refactor improves the separation of concerns, making the codebase more organized and easier to navigate. Future maintenance and enhancements can now be more easily localized to the appropriate utility module.
This commit is contained in:
21
utils/geometryUtils.js
Normal file
21
utils/geometryUtils.js
Normal file
@@ -0,0 +1,21 @@
|
||||
export const parsePoint = (position) => {
|
||||
const [longitude, latitude] = position.slice(6, -1).split(" ");
|
||||
return { latitude: parseFloat(latitude), longitude: parseFloat(longitude) };
|
||||
};
|
||||
|
||||
export const findClosestPoints = (coordinates, newPoint, map) => {
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user