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
This commit is contained in:
1 parent
fdd77986c1
commit
e9c9e96b92
2 files changed
+19
-4
No files matched your search
+15
-1
@@ -10,6 +10,20 @@ const POLL_INTERVAL_MS = 1500;
|
|||||||
// marker, with fix status in a flyout. Polling rather than a push channel
|
// 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
|
// is deliberate — see the comment in app.rb next to GNSS_LATEST — and fits
|
||||||
// gpsd's own ~1Hz report rate fine.
|
// 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 {
|
export class GnssControl {
|
||||||
constructor(map) {
|
constructor(map) {
|
||||||
this._map = map;
|
this._map = map;
|
||||||
@@ -109,7 +123,7 @@ export class GnssControl {
|
|||||||
this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) });
|
this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) });
|
||||||
} else {
|
} else {
|
||||||
this._marker.setLngLat(lngLat);
|
this._marker.setLngLat(lngLat);
|
||||||
this._map.easeTo({ center: lngLat });
|
followPosition(this._map, lngLat, data.track, data.speed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -1,4 +1,5 @@
|
|||||||
import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs";
|
import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs";
|
||||||
|
import { followPosition } from "/js/gnss.js";
|
||||||
|
|
||||||
// Replaces MapLibre's own GeolocateControl (browser Geolocation API only)
|
// Replaces MapLibre's own GeolocateControl (browser Geolocation API only)
|
||||||
// with one that prefers this app's own GNSS receiver when it's turned on
|
// 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._active = true;
|
||||||
this._button.classList.add("maplibregl-ctrl-geolocate-active");
|
this._button.classList.add("maplibregl-ctrl-geolocate-active");
|
||||||
this._watchId = navigator.geolocation.watchPosition(
|
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.");
|
window.alert("Could not determine your location.");
|
||||||
this._stop();
|
this._stop();
|
||||||
@@ -128,7 +129,7 @@ export class LocateControl {
|
|||||||
// .gnss-marker — reusing that green for a browser-estimated position too
|
// .gnss-marker — reusing that green for a browser-estimated position too
|
||||||
// looked like this control was somehow still using GNSS data even when
|
// looked like this control was somehow still using GNSS data even when
|
||||||
// it very much wasn't; distinct colors for distinct sources.
|
// it very much wasn't; distinct colors for distinct sources.
|
||||||
_setPosition(lon, lat, accuracy) {
|
_setPosition(lon, lat, accuracy, heading, speed) {
|
||||||
this._accuracy = accuracy;
|
this._accuracy = accuracy;
|
||||||
if (!this._marker) {
|
if (!this._marker) {
|
||||||
const el = document.createElement("div");
|
const el = document.createElement("div");
|
||||||
@@ -141,7 +142,7 @@ export class LocateControl {
|
|||||||
} else {
|
} else {
|
||||||
this._marker.setLngLat([lon, lat]);
|
this._marker.setLngLat([lon, lat]);
|
||||||
this._circleMarker.setLngLat([lon, lat]);
|
this._circleMarker.setLngLat([lon, lat]);
|
||||||
this._map.easeTo({ center: [lon, lat] });
|
followPosition(this._map, [lon, lat], heading, speed);
|
||||||
}
|
}
|
||||||
this._updateCircleRadius();
|
this._updateCircleRadius();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user