Driving declutter by a ["pitch"] in the layers' filters (MapLibre 6.21): route numbers and minor roads' / paths' names go already while tilting past 30°, no pitchend listener, no re-apply on style.load
This commit is contained in:
1 parent
f85785c61d
commit
cba14300b5
2 files changed
+11
-18
No files matched your search
+10
-16
@@ -11,28 +11,22 @@
|
||||
// The driving view (the map tilted to 3D, as following does): route
|
||||
// numbers and minor roads' / paths' names hidden, the main roads' names
|
||||
// left — what's needed at a glance. Back as soon as the map is flat.
|
||||
// A ["pitch"] in the layers' filters (MapLibre ≥ 6.21), so they go already
|
||||
// while tilting, and come back with every style without a listener.
|
||||
const DRIVE_HIDDEN_LABELS = /^(highway-shield|road_shield|highway-name-(minor|path))/;
|
||||
const DRIVE_PITCH = 30;
|
||||
|
||||
export function declutterWhenDriving(map) {
|
||||
let hidden = null;
|
||||
const apply = () => {
|
||||
const hide = map.getPitch() >= DRIVE_PITCH;
|
||||
if (hide === hidden) return;
|
||||
hidden = hide;
|
||||
for (const layer of map.getStyle().layers) {
|
||||
if (DRIVE_HIDDEN_LABELS.test(layer.id)) map.setLayoutProperty(layer.id, "visibility", hide ? "none" : "visible");
|
||||
}
|
||||
};
|
||||
map.on("pitchend", apply);
|
||||
map.on("style.load", () => {
|
||||
hidden = null;
|
||||
apply();
|
||||
});
|
||||
function declutterWhenDriving(style) {
|
||||
for (const layer of style.layers) {
|
||||
if (!DRIVE_HIDDEN_LABELS.test(layer.id)) continue;
|
||||
const flat = ["<", ["pitch"], DRIVE_PITCH];
|
||||
layer.filter = layer.filter ? ["all", flat, layer.filter] : flat;
|
||||
}
|
||||
return style;
|
||||
}
|
||||
|
||||
export function patchOnlineStyle(style) {
|
||||
return insertTrackOnlyLayer(protectedAreasAndTextures(roadLabelSpacing(parkTilesStyle(style))));
|
||||
return insertTrackOnlyLayer(protectedAreasAndTextures(roadLabelSpacing(declutterWhenDriving(parkTilesStyle(style)))));
|
||||
}
|
||||
|
||||
// Road names and route numbers repeat every symbol-spacing *pixels*, so
|
||||
|
||||
Reference in new issue
Block a user