Browse Source

Fix GPS accuracy circle source/layer errors breaking WASD

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 <noreply@anthropic.com>
master
HikeMap User 4 weeks ago
parent
commit
800c45305d
  1. 30
      index.html

30
index.html

@ -6421,13 +6421,17 @@
gpsMarker.remove(); // MapLibre marker removal gpsMarker.remove(); // MapLibre marker removal
gpsMarker = null; 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'); 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'); 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'); if (map.getSource('gps-accuracy')) map.removeSource('gps-accuracy');
} catch (e) { console.log('Could not remove gps-accuracy source:', e.message); }
gpsAccuracyCircle = null; gpsAccuracyCircle = null;
}
// Sync test position to last known GPS location for seamless manual mode // Sync test position to last known GPS location for seamless manual mode
if (userLocation) { if (userLocation) {
@ -6563,9 +6567,16 @@
} }
// Update or create accuracy circle (MapLibre GeoJSON version) // Update or create accuracy circle (MapLibre GeoJSON version)
if (!gpsAccuracyCircle) {
// Create circle as GeoJSON using turf.js
const circleGeoJSON = createCircleGeoJSON([lng, lat], accuracy); const circleGeoJSON = createCircleGeoJSON([lng, lat], accuracy);
const existingSource = map.getSource('gps-accuracy');
if (existingSource) {
// Update existing source
existingSource.setData(circleGeoJSON);
gpsAccuracyCircle = true;
} else {
// Create new source and layers
try {
map.addSource('gps-accuracy', { map.addSource('gps-accuracy', {
type: 'geojson', type: 'geojson',
data: circleGeoJSON data: circleGeoJSON
@ -6588,12 +6599,9 @@
'line-width': 1 'line-width': 1
} }
}); });
gpsAccuracyCircle = true; // Flag that source exists
} else {
// Update existing source
const source = map.getSource('gps-accuracy');
if (source) {
source.setData(createCircleGeoJSON([lng, lat], accuracy));
gpsAccuracyCircle = true;
} catch (e) {
console.log('Error creating GPS accuracy circle:', e.message);
} }
} }

Loading…
Cancel
Save