mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
32879dd528
A MapLibre user could only draw OpenFreeMap styles; a raster template was Leaflet's alone. With maplibre_style set to trek:template, MapLibre now draws the template the trip map already resolves for Leaflet (CARTO key, keyless fallback and dark default included), so both renderers show the same map. No stored row holds the keyword, so every existing user sees exactly the map they saw before, and Mapbox is untouched. - glBasemap turns the basemap spec into a raster style document (256px tiles as Leaflet requests them, the provider's deepest zoom, the credit, OpenFreeMap glyphs for the map's own labels) and resolves trek:ofm-auto to Liberty or OpenFreeMap Dark with the colour scheme. - glCanFetch decides whether a GL map may fetch a basemap here: https on a host the CSP names, the shipped ones or MAP_EXTRA_CONNECT_HOSTS from the app config. GCJ-02 tiles (Amap) and other hosts are drawn by Leaflet for that render, the setting untouched. - rasterHealth watches the first tiles of a template map: a CSP refusal or twelve failures without a success move every map drawing that template to Leaflet for the session, with a pill that says so. - useEffectiveMapEngine decides this on the engine that actually draws and runs the offline checks after it; a stored raster template keeps drawing offline where Leaflet would keep the same tiles. - The journey map uses the same hook: the same template, the same fallback and now the offline basemap with its pill as the trip map has it. - A MapLibre raster style stops at zoom 19 like Leaflet; the Mapbox options stay as they were. The offline pill moves to BasemapPill for both maps. Dropping the unused imports there lowers the eslint baseline.
93 lines
3.8 KiB
TypeScript
93 lines
3.8 KiB
TypeScript
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<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;
|
|
// 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 */
|
|
}
|
|
}
|
|
}
|