Open alen-s opened 8 months ago
By the way, you can use this code to determine the nearest point on the route by given points coords.
const findNearestPointOnRoute = (
routePoints: string | any[],
givenPoints: any[],
) => {
return givenPoints.map((givenPoint: any) => {
let minDistance = Number.MAX_VALUE;
let nearestPoint = null;
// Calculate distance for each point on the route
for (let i = 0; i < routePoints.length; i++) {
const routePoint = routePoints[i];
const distance = calculateHaversineDistance(givenPoint, routePoint);
// Update nearest point if the current point is closer
if (distance < minDistance) {
minDistance = distance;
nearestPoint = routePoint;
}
}
return nearestPoint;
});
};
// Function to calculate Haversine distance between two points
const calculateHaversineDistance = (
point1: {lat: number; lon: number},
point2: {lat: number; lon: number},
) => {
const R = 6371; // Radius of the Earth in kilometers
const dLat = toRadians(point2.lat - point1.lat);
const dLon = toRadians(point2.lon - point1.lon);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRadians(point1.lat)) *
Math.cos(toRadians(point2.lat)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
// Distance in kilometers
return R * c;
};
// Helper function to convert degrees to radians
const toRadians = (degrees: number) => {
return degrees * (Math.PI / 180);
};
При просмотре https://yandex.com/dev/maps/jsbox/2.1/multiroute_view_options вы можете заметить, что на маршруте розовым маркером отмечена «Кремлёвская набережная». Есть ли у нас такой функционал? И если нет, когда вы планируете над этим работать?
CC: @ownikss