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); } }