From ecb1875077e69c4c7e722e15bc9d1376001fbc15 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Sun, 3 Oct 2021 23:30:47 +0300 Subject: [PATCH] Fix button behaviours --- src/App.svelte | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index c036b9b..b8702fc 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -86,20 +86,35 @@ else { locationCircleLayerGroup.clearLayers(); } - $: if (coords && followLocation) { - map.fitBounds( - L.latLng(coords.latitude, coords.longitude).toBounds(coords.accuracy * 2), - { - animate: true, - duration: 1, - }, - ); - } else if (map) { - map.setZoom(1, { - animate: true, - duration: 5, - }); - } + + const locationFollowLogic = { + follow: false, + coords: void 0, + setFollow: function(shouldFollow: boolean) { + if (this.follow && !shouldFollow) { + map.setZoom(1, { + animate: true, + duration: 5, + }); + } + this.follow = shouldFollow; + }, + update: function(coords: GeolocationCoordinates) { + this.coords = coords; + if (this.coords && this.follow) { + map.fitBounds( + L.latLng(this.coords.latitude, this.coords.longitude).toBounds(this.coords.accuracy * 2), + { + animate: true, + duration: 1, + }, + ); + } + }, + }; + + $: locationFollowLogic.setFollow(followLocation); + $: locationFollowLogic.update(coords); function onPosition(e: GeolocationPosition) { error = void 0; @@ -166,7 +181,7 @@ {/if}
{#key geoWatchHandle} - + {/key} {#key followLocation}