Pārlūkot izejas kodu

Don't auto-fit map after user manually pans or zooms

Adds a userMovedMap flag in MapView. Leaflet fires movestart/zoomstart
with e.originalEvent for user gestures (drag, scroll, touch) and
without it for programmatic calls (fitBounds, setView). The flag is
set only on user-initiated events.

fitTrack() and fitAll() bail out immediately when the flag is set, so
opening a track or selecting a folder no longer jumps the map after the
user has taken control of the viewport.

Double-clicking a polyline resets the flag before calling fitTrack()
since that is an explicit "zoom to this track" request.

No backend restart required.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
k4be 5 stundas atpakaļ
vecāks
revīzija
b7fe59ba99
1 mainītis faili ar 10 papildinājumiem un 0 dzēšanām
  1. 10 0
      gpx-vis-frontend/js/map.js

+ 10 - 0
gpx-vis-frontend/js/map.js

@@ -8,6 +8,7 @@ const MapView = (() => {
   const COLORS = ['#e74c3c', '#3498db', '#2ecc71', '#f39c12', '#9b59b6', '#1abc9c', '#e67e22', '#34495e'];
   const HOVER_TOLERANCE_PX = 20;
   let colorIndex = 0;
+  let userMovedMap = false;  // true after manual pan/zoom; suppresses auto-fit
 
   // ===== Haversine / point helpers =====
 
@@ -78,6 +79,12 @@ const MapView = (() => {
     map.on('mouseout',  onMapMouseOut);
     map.on('click',     onMapClick);
 
+    // Detect manual pan/zoom: Leaflet sets originalEvent only for user gestures,
+    // not for programmatic fitBounds/setView calls.
+    map.on('movestart zoomstart', (e) => {
+      if (e.originalEvent) userMovedMap = true;
+    });
+
     restoreFromHash();
     map.on('moveend zoomend', saveToHash);
 
@@ -134,6 +141,7 @@ const MapView = (() => {
     layer.addTo(map);
     layer.on('dblclick', (e) => {
       L.DomEvent.stopPropagation(e);
+      userMovedMap = false;  // explicit user request — override the guard
       fitTrack(trackId);
     });
     trackLayers[trackId] = {
@@ -161,6 +169,7 @@ const MapView = (() => {
   }
 
   function fitTrack(trackId) {
+    if (userMovedMap) return;
     if (!trackLayers[trackId]) return;
     try {
       const bounds = trackLayers[trackId].layer.getBounds();
@@ -171,6 +180,7 @@ const MapView = (() => {
   }
 
   function fitAll() {
+    if (userMovedMap) return;
     const layers = Object.values(trackLayers).map(t => t.layer);
     if (layers.length === 0) return;
     try {