Compass and a north-up rotation lock (on by default, remembered): a two-finger gesture on a tablet rotated the map far too easily

This commit is contained in:
randy committed 2026-11-18 22:59:12 +01:00
1 parent 4265421457
commit 6faabca93d
4 files changed
+57 -6

No files matched your search

+4
View File
@@ -14,6 +14,8 @@ import {
mdiFolderPlusOutline,
mdiLayersOutline,
mdiLayersPlus,
mdiLock,
mdiLockOpenVariantOutline,
mdiMagnify,
mdiMapMarkerOutline,
mdiMapMarkerPath,
@@ -63,6 +65,8 @@ export const SAVED_POINT_ICON = mdi(mdiMapMarkerStarOutline);
export const REVERSE_ICON = mdi(mdiSwapVertical);
export const CLEAR_ICON = mdi(mdiDeleteSweepOutline);
export const SAVE_ICON = mdi(mdiContentSaveOutline);
export const LOCK_ICON = mdi(mdiLock);
export const UNLOCK_ICON = mdi(mdiLockOpenVariantOutline);
export const TRACK_ICON = GPX_ICON;
// A rounded-square badge — used as the map marker for individual waypoints,
+46 -6
View File
@@ -17,7 +17,7 @@ import { LocateControl } from "/js/locate.js";
import { SearchControl } from "/js/search.js";
import { OfflineDataControl } from "/js/offline_data.js";
import { RoutePanelControl } from "/js/route.js";
import { LAYERS_ICON, WAYPOINT_ICON, SUNRISE_ICON, SUNSET_ICON, PENDING_ICON } from "/js/icons.js";
import { LAYERS_ICON, WAYPOINT_ICON, SUNRISE_ICON, SUNSET_ICON, PENDING_ICON, LOCK_ICON, UNLOCK_ICON } from "/js/icons.js";
import { formatCoords, formatSunInfo, sunChartHtml } from "/js/sun.js";
import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "/js/panels.js";
import { isLoggedIn } from "/js/auth.js";
@@ -359,8 +359,7 @@ class ZoomLevelControl {
}
// Tilts the camera between a flat top-down view and a fixed 3D-perspective
// pitch — bearing stays untouched (this map never rotates, see the
// NavigationControl comment below), so this is purely "look at it from an
// pitch — bearing stays untouched, so this is purely "look at it from an
// angle", not a rotate/compass control. Independent of the "Terrain
// shading" overlay toggle (overlays.js) — that adds a raster-dem hillshade/
// color-relief layer, this just moves the camera; combining both gives a
@@ -417,6 +416,47 @@ class PitchToggleControl {
}
}
// Locks the map north-up (on by default): a two-finger gesture on a
// tablet rotates it far too easily. Unlocked, it rotates like any map and
// NavigationControl's compass turns it back north.
class RotationLockControl {
onAdd(map) {
this._map = map;
this._container = document.createElement("div");
this._container.className = "maplibregl-ctrl maplibregl-ctrl-group rotation-lock-ctrl";
this._button = document.createElement("button");
this._button.type = "button";
this._button.addEventListener("click", () => this._set(!this._locked));
this._container.appendChild(this._button);
let stored = null;
try {
stored = localStorage.getItem("rotationLocked");
} catch {}
this._set(stored !== "false");
return this._container;
}
onRemove() {
this._container.remove();
}
_set(locked) {
this._locked = locked;
const action = locked ? "disableRotation" : "enableRotation";
this._map.touchZoomRotate[action]();
this._map.keyboard[action]();
this._map.dragRotate[locked ? "disable" : "enable"]();
if (locked && this._map.getBearing() !== 0) this._map.easeTo({ bearing: 0, duration: 300 });
this._button.innerHTML = locked ? LOCK_ICON : UNLOCK_ICON;
this._button.title = locked ? "Rotation locked (north up), click to allow rotating" : "Rotation allowed, click to lock north up";
this._button.setAttribute("aria-label", this._button.title);
this._button.classList.toggle("active", locked);
try {
localStorage.setItem("rotationLocked", String(locked));
} catch {}
}
}
const map = new MaplibreMap({
container: "map",
style: ONLINE_STYLE,
@@ -450,9 +490,9 @@ const SLOVAKIA_BOUNDS = [
const searchControl = new SearchControl();
map.addControl(searchControl, "top-left");
map.addControl(new HomeButton({ bounds: SLOVAKIA_BOUNDS }), "top-left");
// showCompass: false — this map never rotates, so a bearing-reset button
// (NavigationControl's default third button) would just be dead weight.
map.addControl(new NavigationControl({ showCompass: false }), "top-left");
// The compass shows the bearing; a click turns the map back north.
map.addControl(new NavigationControl({ visualizePitch: false }), "top-left");
map.addControl(new RotationLockControl(), "top-left");
map.addControl(new ZoomLevelControl(), "top-left");
const pitchToggle = new PitchToggleControl();
map.addControl(pitchToggle, "top-left");