Night mode (auto after sunset / on / off, one invert+dim filter for now), tracker drawn as a top-down vehicle rotated by its GNSS course with a heading-up tilted Navigate mode, side panels float over the map on phone width instead of squeezing it
This commit is contained in:
1 parent
0e5f79f8e4
commit
9148c9a411
5 files changed
+189
-17
No files matched your search
+88
-16
@@ -104,7 +104,25 @@ const poller = {
|
||||
|
||||
const TRACKER_SOURCE = "live-tracker";
|
||||
const TRAIL_SOURCE = "live-tracker-trail";
|
||||
const TRACKER_LAYERS = ["live-tracker-trail", "live-tracker-badge", "live-tracker-icon", "live-tracker-label"];
|
||||
const TRACKER_LAYERS = ["live-tracker-trail", "live-tracker-badge", "live-tracker-icon", "live-tracker-vehicle", "live-tracker-label"];
|
||||
// Below this speed the GNSS course is noise: the vehicle keeps its last one.
|
||||
const MOVING_KMH = 3;
|
||||
const NAV_PITCH = 60;
|
||||
const NAV_ZOOM = 16;
|
||||
|
||||
// Top-down shapes pointing north, rotated on the map by the course (like
|
||||
// OpenTrafficMap's vehicles): picked from the tracker's icon.
|
||||
const VEHICLE_SHAPES = {
|
||||
car: `<svg viewBox="0 0 24 24"><rect x="6" y="2" width="12" height="20" rx="4" fill="currentColor" stroke="#fff" stroke-width="1.5"/><path d="M8 8.5h8l-1-2.5H9zM8.5 17h7l.5 2h-8z" fill="#fff" opacity=".85"/></svg>`,
|
||||
moto: `<svg viewBox="0 0 24 24"><rect x="10" y="1.5" width="4" height="5" rx="2" fill="currentColor" stroke="#fff"/><rect x="10" y="17.5" width="4" height="5" rx="2" fill="currentColor" stroke="#fff"/><path d="M6 8h12M9 8l1.5 9h3L15 8" fill="currentColor" stroke="#fff" stroke-width="1.5" stroke-linecap="round"/></svg>`,
|
||||
bike: `<svg viewBox="0 0 24 24"><path d="M12 2v20M7 7h10" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><path d="M12 2v20M7 7h10" stroke="#fff" stroke-width=".8" stroke-linecap="round"/></svg>`,
|
||||
};
|
||||
|
||||
function vehicleShape(icon) {
|
||||
if (/moto/.test(icon)) return "moto";
|
||||
if (/bicycle|cycl|vtt|bike/.test(icon)) return "bike";
|
||||
return "car";
|
||||
}
|
||||
|
||||
export class TrackerControl {
|
||||
constructor(map) {
|
||||
@@ -150,6 +168,7 @@ export class TrackerControl {
|
||||
|
||||
this._status.addEventListener("click", (event) => {
|
||||
if (event.target.closest(".live-panel__show")) this._showTracker();
|
||||
if (event.target.closest(".live-panel__follow")) this._setFollow(true);
|
||||
});
|
||||
if (!isLoggedIn()) {
|
||||
this._toggle.disabled = true;
|
||||
@@ -202,6 +221,7 @@ export class TrackerControl {
|
||||
}
|
||||
|
||||
_disable() {
|
||||
this._setFollow(false);
|
||||
this._enabled = false;
|
||||
poller.unsubscribe(this._onData);
|
||||
this._status.hidden = true;
|
||||
@@ -210,6 +230,28 @@ export class TrackerControl {
|
||||
removeLayers(this._map, TRACKER_LAYERS, [TRACKER_SOURCE, TRAIL_SOURCE]);
|
||||
}
|
||||
|
||||
// Navigation mode: the map follows the tracker heading-up, tilted, with
|
||||
// the vehicle in the lower third so more road ahead shows.
|
||||
_setFollow(on) {
|
||||
if (!this._follow === !on) return;
|
||||
this._follow = on;
|
||||
this._renderCard();
|
||||
const t = this._data?.tracker;
|
||||
if (on && t) this._navigate(t);
|
||||
if (!on) this._map.easeTo({ padding: { top: 0, bottom: 0, left: 0, right: 0 }, duration: 300 });
|
||||
}
|
||||
|
||||
_navigate(t) {
|
||||
this._map.easeTo({
|
||||
center: [t.longitude, t.latitude],
|
||||
bearing: this._course ?? this._map.getBearing(),
|
||||
pitch: NAV_PITCH,
|
||||
zoom: Math.max(this._map.getZoom(), NAV_ZOOM),
|
||||
padding: { top: this._map.getContainer().clientHeight * 0.4, bottom: 0, left: 0, right: 0 },
|
||||
duration: Math.min(POLL_INTERVAL_MS, 1000),
|
||||
});
|
||||
}
|
||||
|
||||
_showTracker() {
|
||||
const t = this._data?.tracker;
|
||||
if (t) this._map.flyTo({ center: [t.longitude, t.latitude], zoom: Math.max(this._map.getZoom(), 14) });
|
||||
@@ -227,18 +269,36 @@ export class TrackerControl {
|
||||
layout: { "line-cap": "round", "line-join": "round" },
|
||||
paint: { "line-color": ["get", "color"], "line-width": 3, "line-opacity": 0.75 },
|
||||
});
|
||||
// Badge + glyph until a course is known, then the rotated vehicle.
|
||||
const still = ["!", ["has", "course"]];
|
||||
this._map.addLayer({
|
||||
id: "live-tracker-badge",
|
||||
type: "symbol",
|
||||
source: TRACKER_SOURCE,
|
||||
filter: still,
|
||||
layout: { "icon-image": ["get", "badge"], "icon-size": 0.7, "icon-allow-overlap": true },
|
||||
});
|
||||
this._map.addLayer({
|
||||
id: "live-tracker-icon",
|
||||
type: "symbol",
|
||||
source: TRACKER_SOURCE,
|
||||
filter: still,
|
||||
layout: { "icon-image": ["get", "glyph"], "icon-size": 0.4, "icon-allow-overlap": true },
|
||||
});
|
||||
this._map.addLayer({
|
||||
id: "live-tracker-vehicle",
|
||||
type: "symbol",
|
||||
source: TRACKER_SOURCE,
|
||||
filter: ["has", "course"],
|
||||
layout: {
|
||||
"icon-image": ["get", "vehicle"],
|
||||
"icon-size": 0.9,
|
||||
"icon-rotate": ["get", "course"],
|
||||
"icon-rotation-alignment": "map",
|
||||
"icon-pitch-alignment": "map",
|
||||
"icon-allow-overlap": true,
|
||||
},
|
||||
});
|
||||
this._map.addLayer({
|
||||
id: "live-tracker-label",
|
||||
type: "symbol",
|
||||
@@ -248,9 +308,13 @@ export class TrackerControl {
|
||||
});
|
||||
if (!this._interactive) {
|
||||
this._interactive = true;
|
||||
this._map.on("click", "live-tracker-badge", (event) => this._popup(event.lngLat));
|
||||
this._map.on("mouseenter", "live-tracker-badge", () => (this._map.getCanvas().style.cursor = "pointer"));
|
||||
this._map.on("mouseleave", "live-tracker-badge", () => (this._map.getCanvas().style.cursor = ""));
|
||||
for (const layer of ["live-tracker-badge", "live-tracker-vehicle"]) {
|
||||
this._map.on("click", layer, (event) => this._popup(event.lngLat));
|
||||
this._map.on("mouseenter", layer, () => (this._map.getCanvas().style.cursor = "pointer"));
|
||||
this._map.on("mouseleave", layer, () => (this._map.getCanvas().style.cursor = ""));
|
||||
}
|
||||
// Panning by hand ends navigation mode (zoom/pinch keeps it).
|
||||
this._map.on("dragstart", () => this._setFollow(false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,7 +332,13 @@ export class TrackerControl {
|
||||
const image = await svgToImageData(svg.replace(/currentColor/g, "#fff"), ICON_IMAGE_SIZE);
|
||||
if (!this._map.hasImage(glyph)) this._map.addImage(glyph, image);
|
||||
}
|
||||
return { badge, glyph };
|
||||
const shape = vehicleShape(icon);
|
||||
const vehicle = `live-vehicle-${shape}-${color}`;
|
||||
if (!this._map.hasImage(vehicle)) {
|
||||
const image = await svgToImageData(VEHICLE_SHAPES[shape].replace(/currentColor/g, color), ICON_IMAGE_SIZE);
|
||||
if (!this._map.hasImage(vehicle)) this._map.addImage(vehicle, image);
|
||||
}
|
||||
return { badge, glyph, vehicle };
|
||||
}
|
||||
|
||||
async _render(data) {
|
||||
@@ -286,22 +356,20 @@ export class TrackerControl {
|
||||
}
|
||||
const t = data.tracker;
|
||||
this._status.innerHTML = t
|
||||
? `${escapeHtml(settings.name)} ${ago(t.age)} · <button type="button" class="live-panel__show">show</button>`
|
||||
? `${escapeHtml(settings.name)} ${ago(t.age)} · <button type="button" class="live-panel__show">show</button> · <button type="button" class="live-panel__follow">navigate</button>`
|
||||
: `No ${escapeHtml(settings.name)} position yet`;
|
||||
if (t?.course != null && (t.speed ?? MOVING_KMH) >= MOVING_KMH) this._course = t.course;
|
||||
this._renderCard();
|
||||
if (!t) return;
|
||||
|
||||
const { badge, glyph } = await this._ensureImages(settings.color, settings.icon);
|
||||
const { badge, glyph, vehicle } = await this._ensureImages(settings.color, settings.icon);
|
||||
const properties = { badge, glyph, vehicle, label: `${settings.name} · ${ago(t.age)}` };
|
||||
if (this._course != null) properties.course = this._course;
|
||||
this._map.getSource(TRACKER_SOURCE)?.setData({
|
||||
type: "FeatureCollection",
|
||||
features: [
|
||||
{
|
||||
type: "Feature",
|
||||
geometry: { type: "Point", coordinates: [t.longitude, t.latitude] },
|
||||
properties: { badge, glyph, label: `${settings.name} · ${ago(t.age)}` },
|
||||
},
|
||||
],
|
||||
features: [{ type: "Feature", geometry: { type: "Point", coordinates: [t.longitude, t.latitude] }, properties }],
|
||||
});
|
||||
if (this._follow) this._navigate(t);
|
||||
const trail = [...(t.trail || []), [t.longitude, t.latitude]];
|
||||
this._map.getSource(TRAIL_SOURCE)?.setData({
|
||||
type: "FeatureCollection",
|
||||
@@ -337,7 +405,10 @@ export class TrackerControl {
|
||||
if (!this._card) {
|
||||
this._card = document.createElement("div");
|
||||
this._card.className = "live-tracker-card";
|
||||
this._card.addEventListener("click", () => this._showTracker());
|
||||
this._card.addEventListener("click", (event) => {
|
||||
if (event.target.closest(".live-tracker-card__nav")) this._setFollow(!this._follow);
|
||||
else this._showTracker();
|
||||
});
|
||||
this._map.getContainer().appendChild(this._card);
|
||||
}
|
||||
this._card.style.opacity = this._cardOpacity.value;
|
||||
@@ -352,7 +423,8 @@ export class TrackerControl {
|
||||
["Updated", ago(t.age)],
|
||||
].filter(Boolean);
|
||||
this._card.innerHTML = `<div class="live-tracker-card__title"><span class="live-tracker-card__icon" style="background:${escapeHtml(s.color)}">${POI_ICONS[s.icon] || NPS_SYMBOLS.automobiles}</span>${escapeHtml(s.name)}</div>
|
||||
<table>${rows.map(([k, v]) => `<tr><th>${k}</th><td>${escapeHtml(v)}</td></tr>`).join("")}</table>`;
|
||||
<table>${rows.map(([k, v]) => `<tr><th>${k}</th><td>${escapeHtml(v)}</td></tr>`).join("")}</table>
|
||||
<button type="button" class="live-tracker-card__nav${this._follow ? " active" : ""}">${this._follow ? "Stop navigating" : "Navigate"}</button>`;
|
||||
}
|
||||
|
||||
_popup(lngLat) {
|
||||
|
||||
Reference in new issue
Block a user