import type mapboxgl from 'mapbox-gl'; import { MAP_MAX_ZOOM } from '../../constants/mapDefaults'; import { mapboxTileCacheRequest, tileCacheActive } from '../../native/tileCache'; import { isGlRasterStyle, type GlStyle } from './glBasemap'; 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 { const isMapLibre = input.provider === 'maplibre-gl'; const options: Record = { 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; // A raster template ends where Leaflet's map ends: past it the provider has no // tiles and the map would only enlarge its last ones. if (isMapLibre && isGlRasterStyle(input.style)) options.maxZoom = MAP_MAX_ZOOM; 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: GlStyle, enable3d: boolean, dark: boolean): void { if (typeof style !== 'string') return; 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 */ } } }