mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-10-09 03:02:03 +00:00
fix: show 3d buildings only when the map is tilted
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import {Directive, HostListener} from '@angular/core';
|
||||
import {Map, MapMouseEvent, MapStyleDataEvent} from 'maplibre-gl';
|
||||
|
||||
/** Layer ids in the map style */
|
||||
const BUILDING_LAYER = 'building';
|
||||
const BUILDING_3D_LAYER = 'building-3d';
|
||||
|
||||
/**
|
||||
* Switches between flat and 3D buildings depending on whether the map is tilted
|
||||
*/
|
||||
@Directive({
|
||||
selector: 'mgl-map[auto-3d]',
|
||||
standalone: true,
|
||||
@@ -16,21 +23,37 @@ export class MapAuto3dDirective {
|
||||
this.updatePitch(event.target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the 3D buildings only while the map is tilted; flat buildings otherwise
|
||||
*/
|
||||
updatePitch(map: Map) {
|
||||
if (map.getPitch() === 0) {
|
||||
const layer = map.getLayer('building-3d');
|
||||
if (layer && layer?.visibility !== 'none') {
|
||||
layer.visibility = 'none';
|
||||
map.setPaintProperty('building', 'fill-opacity', 1);
|
||||
map.setLayerZoomRange('building', 13, 24);
|
||||
}
|
||||
if (!map.getLayer(BUILDING_3D_LAYER)) return;
|
||||
|
||||
const flat = map.getPitch() === 0;
|
||||
|
||||
const visibility = flat ? 'none' : 'visible';
|
||||
// Nothing to do if the 3D layer is already in the target state. This check matters
|
||||
// because `pitch` fires on every frame while tilting. `getLayoutProperty` returns
|
||||
// undefined when the style doesn't set `visibility`; MapLibre then treats the layer as visible.
|
||||
if ((map.getLayoutProperty(BUILDING_3D_LAYER, 'visibility') ?? 'visible') === visibility) return;
|
||||
|
||||
map.setLayoutProperty(BUILDING_3D_LAYER, 'visibility', visibility);
|
||||
if (!map.getLayer(BUILDING_LAYER)) return;
|
||||
if (flat) {
|
||||
map.setPaintProperty(BUILDING_LAYER, 'fill-opacity', 1);
|
||||
map.setLayerZoomRange(BUILDING_LAYER, 13, 24);
|
||||
} else {
|
||||
const layer = map.getLayer('building-3d');
|
||||
if (layer && layer?.visibility !== 'visible') {
|
||||
layer.visibility = 'visible';
|
||||
map.setPaintProperty('building', 'fill-opacity', ['interpolate', ['linear'], ['zoom'], 15, 1, 16, 0]);
|
||||
map.setLayerZoomRange('building', 13, 16);
|
||||
}
|
||||
// when tilted, fade the flat buildings out where the 3D ones take over
|
||||
map.setPaintProperty(BUILDING_LAYER, 'fill-opacity', [
|
||||
'interpolate',
|
||||
['linear'],
|
||||
['zoom'],
|
||||
15,
|
||||
1,
|
||||
16,
|
||||
0,
|
||||
]);
|
||||
map.setLayerZoomRange(BUILDING_LAYER, 13, 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user