From 800c45305d25836bacca90f4772132fcf3132afc Mon Sep 17 00:00:00 2001 From: HikeMap User Date: Sat, 10 Jan 2026 21:03:07 -0600 Subject: [PATCH] Fix GPS accuracy circle source/layer errors breaking WASD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause: Uncaught "Source already exists" error in onGPSSuccess was breaking execution flow before the WASD interval could start. Fixes: - Check if source exists before adding (use getSource instead of flag) - Wrap layer/source removal in try-catch to handle edge cases - Wrap source/layer creation in try-catch to prevent breaking execution - Update existing source data instead of recreating when possible 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- index.html | 76 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/index.html b/index.html index ef931d3..aff01fe 100644 --- a/index.html +++ b/index.html @@ -6421,13 +6421,17 @@ gpsMarker.remove(); // MapLibre marker removal gpsMarker = null; } - if (gpsAccuracyCircle) { - // Remove MapLibre layers and source + // Remove MapLibre GPS accuracy layers and source (always try, ignore errors) + try { if (map.getLayer('gps-accuracy-fill')) map.removeLayer('gps-accuracy-fill'); + } catch (e) { console.log('Could not remove gps-accuracy-fill:', e.message); } + try { if (map.getLayer('gps-accuracy-stroke')) map.removeLayer('gps-accuracy-stroke'); + } catch (e) { console.log('Could not remove gps-accuracy-stroke:', e.message); } + try { if (map.getSource('gps-accuracy')) map.removeSource('gps-accuracy'); - gpsAccuracyCircle = null; - } + } catch (e) { console.log('Could not remove gps-accuracy source:', e.message); } + gpsAccuracyCircle = null; // Sync test position to last known GPS location for seamless manual mode if (userLocation) { @@ -6563,37 +6567,41 @@ } // Update or create accuracy circle (MapLibre GeoJSON version) - if (!gpsAccuracyCircle) { - // Create circle as GeoJSON using turf.js - const circleGeoJSON = createCircleGeoJSON([lng, lat], accuracy); - map.addSource('gps-accuracy', { - type: 'geojson', - data: circleGeoJSON - }); - map.addLayer({ - id: 'gps-accuracy-fill', - type: 'fill', - source: 'gps-accuracy', - paint: { - 'fill-color': '#4285f4', - 'fill-opacity': 0.15 - } - }); - map.addLayer({ - id: 'gps-accuracy-stroke', - type: 'line', - source: 'gps-accuracy', - paint: { - 'line-color': '#4285f4', - 'line-width': 1 - } - }); - gpsAccuracyCircle = true; // Flag that source exists - } else { + const circleGeoJSON = createCircleGeoJSON([lng, lat], accuracy); + const existingSource = map.getSource('gps-accuracy'); + + if (existingSource) { // Update existing source - const source = map.getSource('gps-accuracy'); - if (source) { - source.setData(createCircleGeoJSON([lng, lat], accuracy)); + existingSource.setData(circleGeoJSON); + gpsAccuracyCircle = true; + } else { + // Create new source and layers + try { + map.addSource('gps-accuracy', { + type: 'geojson', + data: circleGeoJSON + }); + map.addLayer({ + id: 'gps-accuracy-fill', + type: 'fill', + source: 'gps-accuracy', + paint: { + 'fill-color': '#4285f4', + 'fill-opacity': 0.15 + } + }); + map.addLayer({ + id: 'gps-accuracy-stroke', + type: 'line', + source: 'gps-accuracy', + paint: { + 'line-color': '#4285f4', + 'line-width': 1 + } + }); + gpsAccuracyCircle = true; + } catch (e) { + console.log('Error creating GPS accuracy circle:', e.message); } }