From 9e197a2b13636f5c7e1c9b096508d4920abc8409 Mon Sep 17 00:00:00 2001 From: Alessio Davoli Date: Wed, 13 Dec 2023 18:41:20 +0100 Subject: [PATCH] Clear position watching on map destroy --- frontend/src/lib/components/tabs/MapTab.svelte | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/src/lib/components/tabs/MapTab.svelte b/frontend/src/lib/components/tabs/MapTab.svelte index 9027484..b1a5ba2 100644 --- a/frontend/src/lib/components/tabs/MapTab.svelte +++ b/frontend/src/lib/components/tabs/MapTab.svelte @@ -11,6 +11,7 @@ let latitude; let longitude; let accuracy; + let watchPositionId: number; const errorMessage = "Geolocation not available"; const attribution = '© OpenStreetMap contributors'; const openStreetMapTile = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png'; @@ -48,7 +49,7 @@ console.warn(`ERROR(${err.code}): ${err.message}`); } - navigator.geolocation.watchPosition(success, error, options); + watchPositionId = navigator.geolocation.watchPosition(success, error, options); }; const renderMap = () => { @@ -73,12 +74,13 @@ await watchPosition(); leaflet = await import('leaflet'); - setTimeout(() => { - renderMap() - }, 500); + renderMap() }); onDestroy(async () => { + if(watchPositionId) { + navigator.geolocation.clearWatch(watchPositionId); + } if (map) { console.log('Unloading Leaflet map.'); map.remove();