mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
feat(native): offline vector maps in the iOS app
In a browser the service worker answers MapLibre from the gl-map-offline cache that glPrefetcher fills for trips marked for offline. The iOS app has no service worker, so nothing read that cache there and the map went blank without network. A trek-offline:// protocol takes the service worker's place. Inside the iOS app the MapLibre style URL is rewritten to it, the handler rewrites every https URL inside the style and its TileJSON the same way, and then serves documents network first and tiles, glyphs and sprites cache first. Outside the iOS app nothing changes. The default OpenFreeMap basemap is covered; Mapbox GL has no protocol hook and stays online-only in the app, as does a custom raster template, whose opaque responses only a service worker can store.
This commit is contained in:
@@ -5,6 +5,7 @@ import { isStandardFamily, supportsCustom3d, wantsTerrain, addCustom3dBuildings,
|
||||
import { MAPBOX_DEFAULT_STYLE, styleForActiveProvider, basemapLanguage, type GlMapProvider } from '../Map/glProviders'
|
||||
import type { JourneyTrack } from '@trek/shared'
|
||||
import { SHOT_INDEX_ATTR, ensureJourneyPopupStyle, formatMarkerDate, journeyPopupHtml } from './journeyMapPopup'
|
||||
import { offlineStyle } from '../../native/offlineTiles'
|
||||
|
||||
export interface JourneyMapGLHandle {
|
||||
highlightMarker: (id: string | null) => void
|
||||
@@ -290,7 +291,7 @@ function JourneyMapGL(
|
||||
|
||||
const mapOptions: Record<string, unknown> = {
|
||||
container: containerRef.current,
|
||||
style: glStyle,
|
||||
style: isMapLibre ? offlineStyle(glStyle) : glStyle,
|
||||
center: hasPoints ? bounds.getCenter() : [0, 30],
|
||||
zoom: hasPoints ? 2 : 1,
|
||||
pitch: enableMapbox3d && fullScreen ? 45 : 0,
|
||||
|
||||
@@ -43,6 +43,7 @@ import { pluginsApi, type PluginMapMarker, type PluginMapLayer } from '../../api
|
||||
import { DEFAULT_MAP_CENTER, DEFAULT_MAP_ZOOM, SATELLITE_TILE_URL, SATELLITE_TILE_ATTRIBUTION, SATELLITE_TILE_MAXZOOM } from '../../constants/mapDefaults'
|
||||
import { computeMapViewport, TILE_SIZE_GL, type ViewportPadding } from '../../utils/mapViewport'
|
||||
import { selectedPlaceTarget } from './selectedPlaceTarget'
|
||||
import { offlineStyle } from '../../native/offlineTiles'
|
||||
|
||||
function categoryIconSvg(iconName: string | null | undefined, size: number): string {
|
||||
const IconComponent = (iconName && CATEGORY_ICON_MAP[iconName]) || CATEGORY_ICON_MAP['MapPin']
|
||||
@@ -1219,7 +1220,7 @@ export function MapViewGL({
|
||||
|
||||
const mapOptions: Record<string, unknown> = {
|
||||
container: containerRef.current,
|
||||
style: glStyle,
|
||||
style: isMapLibre ? offlineStyle(glStyle) : glStyle,
|
||||
center: [initial.center[1], initial.center[0]],
|
||||
zoom: initial.zoom,
|
||||
pitch: enableMapbox3d ? 45 : 0,
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from '../../constants/mapDefaults'
|
||||
import { hasWebGL } from '../../utils/webgl'
|
||||
import { isChunkLoadError, reloadOnceForChunk } from '../../utils/chunkReload'
|
||||
import { offlineStyle } from '../../native/offlineTiles'
|
||||
|
||||
/** The Leaflet layer maplibre-gl-leaflet hands back. */
|
||||
export type GlLeafletLayer = InstanceType<typeof MaplibreGL>
|
||||
@@ -142,7 +143,7 @@ export function detachBasemapLayer(layer: BasemapLayer | null | undefined): void
|
||||
*/
|
||||
export function restyleBasemap(layer: BasemapLayer | null | undefined, style: string): void {
|
||||
if (!layer || !isGlLayer(layer)) return
|
||||
layer.getMaplibreMap()?.setStyle(style)
|
||||
layer.getMaplibreMap()?.setStyle(offlineStyle(style))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +189,7 @@ export function VectorBasemap({ style }: { style: string }) {
|
||||
// change must retile in place rather than tear the layer down, the same
|
||||
// reason the raster maps keep their template out of the build effect (#2097).
|
||||
const layer = maplibreGL({
|
||||
style: styleRef.current,
|
||||
style: offlineStyle(styleRef.current),
|
||||
interactive: false,
|
||||
attributionControl: false,
|
||||
})
|
||||
@@ -264,7 +265,7 @@ export async function attachVectorBasemap(
|
||||
return
|
||||
}
|
||||
|
||||
const layer = maplibreGL({ style, interactive: false, attributionControl: false })
|
||||
const layer = maplibreGL({ style: offlineStyle(style), interactive: false, attributionControl: false })
|
||||
// The raster layer this replaces carried the credit, and OpenFreeMap asks for
|
||||
// one of its own; attachGlLayer adds it once the layer is actually on the map.
|
||||
if (!attachGlLayer(map, layer, style)) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import maplibregl from 'maplibre-gl'
|
||||
import 'maplibre-gl/dist/maplibre-gl.css'
|
||||
import rtlTextPluginUrl from '@mapbox/mapbox-gl-rtl-text/dist/mapbox-gl-rtl-text.js?url'
|
||||
import { registerRtlTextPlugin } from '../rtlText'
|
||||
import { installOfflineProtocol } from '../../../native/offlineTiles'
|
||||
|
||||
/**
|
||||
* The only module in the client that pulls maplibre-gl in at runtime. Same rule as
|
||||
@@ -15,5 +16,7 @@ import { registerRtlTextPlugin } from '../rtlText'
|
||||
// Arabic/Hebrew/Persian labels come out unjoined and reversed without this.
|
||||
// MapLibre's signature is (url, lazy) and it resolves a promise — see rtlText.ts.
|
||||
registerRtlTextPlugin(() => maplibregl.setRTLTextPlugin(rtlTextPluginUrl, true))
|
||||
// The iOS app has no service worker to serve pre-downloaded tiles; see native/offlineTiles.ts.
|
||||
installOfflineProtocol(maplibregl)
|
||||
|
||||
export default maplibregl
|
||||
|
||||
Reference in New Issue
Block a user