From e9c9e96b92d203509eeb86a54ef4201872344348 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 23 Nov 2026 16:11:48 +0100 Subject: [PATCH] Following the GNSS receiver or the browser's own position turns the map heading-up in the 3D view, position in the lower part of the screen, like a car navigation --- public/js/gnss.js | 16 +++++++++++++++- public/js/locate.js | 7 ++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/public/js/gnss.js b/public/js/gnss.js index 5dd8201..7cff79e 100644 --- a/public/js/gnss.js +++ b/public/js/gnss.js @@ -10,6 +10,20 @@ const POLL_INTERVAL_MS = 1500; // marker, with fix status in a flyout. Polling rather than a push channel // is deliberate — see the comment in app.rb next to GNSS_LATEST — and fits // gpsd's own ~1Hz report rate fine. +// One follow step for a moving position (this receiver, the browser's own +// geolocation). In the 3D (pitched) view it turns heading-up like a car +// navigation, with the position in the lower part of the screen; the last +// heading stays while standing still (speed in m/s, heading in degrees). +export function followPosition(map, lngLat, heading, speed) { + const pitched = map.getPitch() > 0; + const moving = Number.isFinite(heading) && (speed ?? 0) >= 1; + map.easeTo({ + center: lngLat, + padding: { top: pitched ? map.getContainer().clientHeight * 0.4 : 0 }, + ...(pitched && moving && { bearing: heading }), + }); +} + export class GnssControl { constructor(map) { this._map = map; @@ -109,7 +123,7 @@ export class GnssControl { this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) }); } else { this._marker.setLngLat(lngLat); - this._map.easeTo({ center: lngLat }); + followPosition(this._map, lngLat, data.track, data.speed); } } diff --git a/public/js/locate.js b/public/js/locate.js index 06107ac..e4920d4 100644 --- a/public/js/locate.js +++ b/public/js/locate.js @@ -1,4 +1,5 @@ import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs"; +import { followPosition } from "/js/gnss.js"; // Replaces MapLibre's own GeolocateControl (browser Geolocation API only) // with one that prefers this app's own GNSS receiver when it's turned on @@ -105,7 +106,7 @@ export class LocateControl { this._active = true; this._button.classList.add("maplibregl-ctrl-geolocate-active"); this._watchId = navigator.geolocation.watchPosition( - (position) => this._setPosition(position.coords.longitude, position.coords.latitude, position.coords.accuracy), + ({ coords }) => this._setPosition(coords.longitude, coords.latitude, coords.accuracy, coords.heading, coords.speed), () => { window.alert("Could not determine your location."); this._stop(); @@ -128,7 +129,7 @@ export class LocateControl { // .gnss-marker — reusing that green for a browser-estimated position too // looked like this control was somehow still using GNSS data even when // it very much wasn't; distinct colors for distinct sources. - _setPosition(lon, lat, accuracy) { + _setPosition(lon, lat, accuracy, heading, speed) { this._accuracy = accuracy; if (!this._marker) { const el = document.createElement("div"); @@ -141,7 +142,7 @@ export class LocateControl { } else { this._marker.setLngLat([lon, lat]); this._circleMarker.setLngLat([lon, lat]); - this._map.easeTo({ center: [lon, lat] }); + followPosition(this._map, [lon, lat], heading, speed); } this._updateCircleRadius(); }