From 95d6993cfea1e9689b00cc02c171b5bf8098d355 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Tue, 7 May 2024 15:16:32 +0200 Subject: [PATCH] better map bounds logic --- website/src/lib/stores.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/website/src/lib/stores.ts b/website/src/lib/stores.ts index 8fedf07a..c647b0cd 100644 --- a/website/src/lib/stores.ts +++ b/website/src/lib/stores.ts @@ -28,7 +28,10 @@ fileObservers.subscribe((files) => { // Update selectedFiles automatically when } }); -const targetMapBounds = writable(new mapboxgl.LngLatBounds([180, 90, -180, -90])); +const targetMapBounds = writable({ + bounds: new mapboxgl.LngLatBounds([180, 90, -180, -90]), + initial: true +}); const fileStatistics: Map> = new Map(); const fileUnsubscribe: Map = new Map(); export const gpxStatistics: Writable = writable(new GPXStatistics()); @@ -52,7 +55,10 @@ fileObservers.subscribe((files) => { // Maintain up-to-date statistics mapBounds = get(map)?.getBounds() ?? mapBounds; bounds.extend(mapBounds); } - targetMapBounds.set(bounds); + targetMapBounds.set({ + bounds: bounds, + initial: true + }); } fileStatistics.forEach((stats, fileId) => { @@ -79,16 +85,19 @@ fileObservers.subscribe((files) => { // Maintain up-to-date statistics let boundsUnsubscribe = statisticsStore.subscribe((stats) => { let fileBounds = stats.global.bounds; - if (fileBounds.southWest.lat == 90 && fileBounds.southWest.lon == 180 && fileBounds.northEast.lat == -90 && fileBounds.northEast.lon == -180) { + if (fileBounds.southWest.lat == 90 && fileBounds.southWest.lon == 180 && fileBounds.northEast.lat == -90 && fileBounds.northEast.lon == -180) { // Stats are not yet calculated return; } - targetMapBounds.update((bounds) => { - bounds.extend(fileBounds.southWest); - bounds.extend(fileBounds.northEast); - bounds.extend([fileBounds.southWest.lon, fileBounds.northEast.lat]); - bounds.extend([fileBounds.northEast.lon, fileBounds.southWest.lat]); - return bounds; - }); + if (fileBounds.southWest.lat != fileBounds.northEast.lat || fileBounds.southWest.lon != fileBounds.northEast.lon) { // Avoid update for new files + targetMapBounds.update((target) => { + target.bounds.extend(fileBounds.southWest); + target.bounds.extend(fileBounds.northEast); + target.bounds.extend([fileBounds.southWest.lon, fileBounds.northEast.lat]); + target.bounds.extend([fileBounds.northEast.lon, fileBounds.southWest.lat]); + target.initial = false; + return target; + }); + } boundsUnsubscribe(); }) } @@ -100,11 +109,11 @@ selectedFiles.subscribe((selectedFiles) => { // Maintain up-to-date statistics f }); targetMapBounds.subscribe((bounds) => { - if (bounds.getSouth() == 90 && bounds.getWest() == 180 && bounds.getNorth() == -90 && bounds.getEast() == -180) { + if (bounds.initial) { return; } - get(map)?.fitBounds(bounds, { + get(map)?.fitBounds(bounds.bounds, { padding: 80, linear: true, easing: () => 1