refactor(maps): build every GL map from one set of options

The trip map, the journey map and the settings preview each assembled
their own constructor options and their own 3D setup on load, and the
copies had drifted: the preview laid terrain under every style outside
the Standard family, while the trip map only under the satellite and
outdoor styles, so the preview showed a map the trip never drew.

glMapOptions builds the options for all three and applyGl3d sets up
terrain, buildings and the flattened Standard DEM. For Mapbox the options
are exactly the ones each map built before, key for key and in the same
order, which a test pins; the preview now follows the trip map's terrain
rule. The next steps tune MapLibre in this one place.

MapViewGL.tsx shrinks, and its size baseline follows.
This commit is contained in:
Maurice
2026-11-30 14:38:03 +01:00
parent 0a72ed5e76
commit 6dafbc6d05
7 changed files with 333 additions and 76 deletions
+86
View File
@@ -0,0 +1,86 @@
import type mapboxgl from 'mapbox-gl';
import { mapboxTileCacheRequest, tileCacheActive } from '../../native/tileCache';
import { MAPBOX_DEFAULT_STYLE, type GlMapProvider } from './glProviders';
import {
addCustom3dBuildings,
addTerrainAndSky,
isStandardFamily,
supportsCustom3d,
wantsTerrain,
} from './mapboxSetup';
/*
* How every GL map in TREK is constructed: the trip map, the journey map and the
* settings preview. The three used to carry their own copy of these options and
* of the 3D setup, and the copies had drifted (the preview laid terrain under
* every non-Standard style, the trip map only under the satellite and outdoor
* ones). Like the renderers it serves, this module stays free of both SDKs at
* runtime (`check:gl-split`): the engine arrives as the caller's `gl`.
*
* The Mapbox options are exactly what the three built before; a test pins them.
*/
interface GlMapOptionsInput {
provider: GlMapProvider;
container: HTMLElement;
/** A style URL, or a style document for a stored or inline style. */
style: unknown;
/** `[lng, lat]`, or the engine's own LngLat. */
center: unknown;
zoom: number;
pitch: number;
attributionControl: boolean | { compact: boolean };
/** The quality mode: antialiasing, and on Mapbox the globe instead of the flat projection. */
quality: boolean;
/**
* In the iOS app, Mapbox's own resources go through the app's tile store, so
* what was viewed stays on the device for up to 30 days (native/tileCache.ts).
* The maps a user travels with ask for it; the settings preview does not.
*/
appTileStore?: boolean;
}
/** The constructor options for `new gl.Map(...)`. */
export function glMapOptions(input: GlMapOptionsInput): Record<string, unknown> {
const isMapLibre = input.provider === 'maplibre-gl';
const options: Record<string, unknown> = {
container: input.container,
style: input.style,
center: input.center,
zoom: input.zoom,
pitch: input.pitch,
attributionControl: input.attributionControl,
antialias: input.quality,
};
if (!isMapLibre) options.projection = input.quality ? 'globe' : 'mercator';
if (!isMapLibre && input.appTileStore && tileCacheActive()) options.transformRequest = mapboxTileCacheRequest;
// MapLibre's around-center rotate ping-pongs near mid-screen (#1545); false is mapbox-gl's dx rotate.
if (isMapLibre) options.aroundCenter = false;
return options;
}
/**
* The 3D look of a Mapbox style, on the map's `load`.
*
* Terrain only under the styles that benefit from elevation (satellite and
* outdoors): on a flat vector style it lifts route lines onto the DEM while the
* HTML markers stay at sea level, and they drift apart once the map is pitched.
* Buildings go on every style outside the Standard family, which brings its own.
* Mapbox Standard's built-in DEM is flattened in any case, so the markers stay
* on their coordinates at every zoom; the satellite variant keeps it, the DEM is
* what gives it its character. A MapLibre style matches none of these.
*/
export function applyGl3d(map: mapboxgl.Map, style: string, enable3d: boolean, dark: boolean): void {
if (enable3d) {
if (!isStandardFamily(style) && wantsTerrain(style)) addTerrainAndSky(map);
if (supportsCustom3d(style)) addCustom3dBuildings(map, dark);
}
if (style === MAPBOX_DEFAULT_STYLE) {
try {
map.setTerrain(null);
} catch {
/* the style is not ready for terrain yet; it has none to flatten then */
}
}
}