Make "find my location" actually follow GNSS, gated on the sidebar toggle
CI / check (push) Successful in 44s
CI / deploy (push) Successful in 18s

Two fixes based on feedback after the last commit:

1. The map wasn't following the position at all -- only the first fix
   recentered, later updates just moved the marker. Now every fix
   recenters (easeTo, not flyTo, after the first one, so it doesn't
   repeat that zoom swoop on every update) -- matches how
   GeolocateControl's own trackUserLocation always behaved.

2. GNSS should only be used when explicitly turned on via the sidebar
   "GNSS" icon, not whenever a receiver happens to have a fix --
   simpler to test against the simulator that way, and puts the
   visitor in control of whether GNSS is engaged at all. GnssControl
   now owns following the map on its own (same flyTo-once/easeTo-after
   pattern) whenever it's enabled; LocateControl checks
   GnssControl#isEnabled() and, when true, just jumps to
   #getPosition()'s current fix and reuses its existing .gnss-marker
   instead of polling/marking on its own. Only falls back to the
   browser's own geolocation when GNSS is off.

Verified end to end with the GNSS simulator: enabling sidebar GNSS
recenters immediately and keeps following over several polls;
clicking Locate while GNSS is on jumps to the same position without
creating a second marker or touching its own active state; turning
GNSS back off and clicking Locate correctly falls through to the
browser geolocation path instead.
This commit is contained in:
randy committed 2026-09-08 00:56:25 +02:00
1 parent aec665179f
commit 6e5676c890
4 files changed
+93 -76

No files matched your search

+23
View File
@@ -74,6 +74,12 @@ export class GnssControl {
this._status.textContent = fixLabel(data);
const lngLat = [data.lon, data.lat];
// Recenters on every fix while enabled — this is the app's own
// receiver, so "GNSS on" means "follow it", the same way LocateControl
// (public/js/locate.js) follows it when GNSS is what it's using too.
// flyTo (with its zoom swoop) only for the first fix, since that's the
// one jumping from wherever the map already was; easeTo for later
// updates, a plain follow rather than repeating that swoop every poll.
if (!this._marker) {
const el = document.createElement("div");
el.className = "gnss-marker";
@@ -82,8 +88,10 @@ export class GnssControl {
// survives map.setStyle() (switching Online/Offline) with no extra
// re-add handling needed.
this._marker = new Marker({ element: el }).setLngLat(lngLat).addTo(this._map);
this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) });
} else {
this._marker.setLngLat(lngLat);
this._map.easeTo({ center: lngLat });
}
}
@@ -93,6 +101,21 @@ export class GnssControl {
this._marker = null;
}
}
// Read by LocateControl: GNSS is only ever used for "find my location"
// when explicitly turned on here — clicking Locate while this is off
// falls back to the browser's own geolocation instead, rather than
// silently switching sources on whatever GET /api/gnss/position happens
// to report at that moment.
isEnabled() {
return this._enabled;
}
// The current fix, if any — null both when disabled and when enabled
// but not fixed yet (no marker placed either way).
getPosition() {
return this._marker ? this._marker.getLngLat() : null;
}
}
function fixLabel({ mode, eph }) {