diff --git a/src/pages/workouts/components/WorkoutItem.vue b/src/pages/workouts/components/WorkoutItem.vue index 10cd2fb..44b18ce 100644 --- a/src/pages/workouts/components/WorkoutItem.vue +++ b/src/pages/workouts/components/WorkoutItem.vue @@ -28,16 +28,17 @@
1
@@ -518,17 +519,30 @@ const group = new ChartGroup([]); const mapX = ref>([]); let currentCoordinates = ref | null>([]); let clickCoordinates = ref([]); -let coordWithIndex: Map = new Map(); let showModalTitle = ref(false); const showModalLink = ref(false); -const getKey = (x: number, y: number) => { - x = Math.round(x * 1000) / 1000; - y = Math.round(y * 1000) / 1000; - return `${x}-${y}`; +const CLICK_TRACK_RADIUS_M = 40; +/** + * Finds the nearest track point index within CLICK_TRACK_RADIUS_M (metres) + * of the clicked lng/lat using an equirectangular approximation. Returns -1 + * when the click is farther than the radius from every track point. + */ +const findNearestTrackIndex = (lng: number, lat: number): number => { + let bestIdx = -1; + let bestDist = Infinity; + for (let i = 0; i < lineCoordinates.length; i++) { + const [pLng, pLat] = lineCoordinates[i]; + const midLatRad = ((lat + pLat) / 2) * (Math.PI / 180); + const dx = (pLng - lng) * 111320 * Math.cos(midLatRad); + const dy = (pLat - lat) * 110540; + const dist = Math.hypot(dx, dy); + if (dist < bestDist) { + bestDist = dist; + bestIdx = i; + } + } + return bestDist <= CLICK_TRACK_RADIUS_M ? bestIdx : -1; }; -for (let i = 0; i < lineCoordinates.length; i++) { - coordWithIndex.set(getKey(lineCoordinates[i][0], lineCoordinates[i][1]), i); -} ChartJS.register( Title, Tooltip,