From c37f794eeaaae6ec625a4fd02c84f4fbceb13f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 10 Feb 2026 17:18:59 +0400 Subject: [PATCH] Auto-locate user on first app launch closes #17 --- app/components/map.gjs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/components/map.gjs b/app/components/map.gjs index 8545e77..ed5309e 100644 --- a/app/components/map.gjs +++ b/app/components/map.gjs @@ -68,6 +68,7 @@ export default class MapComponent extends Component { // Default view settings let center = [14.21683569, 27.060114248]; let zoom = 2.661; + let restoredFromStorage = false; // Try to restore from localStorage try { @@ -82,6 +83,7 @@ export default class MapComponent extends Component { ) { center = parsed.center; zoom = parsed.zoom; + restoredFromStorage = true; } } } catch (e) { @@ -243,6 +245,7 @@ export default class MapComponent extends Component { const coordinates = geolocation.getPosition(); const accuracyGeometry = geolocation.getAccuracyGeometry(); const accuracy = geolocation.getAccuracy(); + console.debug('Geolocation change:', { coordinates, accuracy }); if (!coordinates) return; @@ -307,7 +310,8 @@ export default class MapComponent extends Component { this.mapInstance.getView().animate(viewOptions); }; - locateBtn.addEventListener('click', () => { + const startLocating = () => { + console.debug('Getting current geolocation...') // 1. Clear any previous session stopLocating(); @@ -331,7 +335,9 @@ export default class MapComponent extends Component { locateTimeout = setTimeout(() => { stopLocating(); }, 10000); - }); + }; + + locateBtn.addEventListener('click', startLocating); const locateControl = new Control({ element: locateElement, @@ -340,6 +346,11 @@ export default class MapComponent extends Component { this.mapInstance.addLayer(geolocationLayer); this.mapInstance.addControl(locateControl); + // Auto-locate on first visit (if not restored from storage and on home page) + if (!restoredFromStorage && this.router.currentRouteName === 'index') { + startLocating(); + } + this.mapInstance.on('singleclick', this.handleMapClick); // Load places when map moves