diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-06-24 13:29:50 +0200 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-06-24 13:29:50 +0200 |
| commit | 894e67863dbb89a4819e825fcdf7117021082b2a (patch) | |
| tree | fb544ef7fa99ff86489717e793595f503783bb72 /src/frontend/app/maps | |
| parent | 7dd9ea97a2f34a35e80c28d59d046f839eb6c60b (diff) | |
Replace leaflet for maplibre, use react-router in framework mode
Diffstat (limited to 'src/frontend/app/maps')
| -rw-r--r-- | src/frontend/app/maps/styleloader.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/frontend/app/maps/styleloader.ts b/src/frontend/app/maps/styleloader.ts new file mode 100644 index 0000000..f00aacc --- /dev/null +++ b/src/frontend/app/maps/styleloader.ts @@ -0,0 +1,49 @@ +import type { StyleSpecification } from "react-map-gl/maplibre"; + +export async function loadStyle(styleName: string, colorScheme: string): Promise<StyleSpecification> { + const stylePath = `/maps/styles/${styleName}-${colorScheme}.json`; + const resp = await fetch(stylePath); + + if (!resp.ok) { + throw new Error(`Failed to load style: ${stylePath}`); + } + + const style = await resp.json(); + + const baseUrl = window.location.origin; + const spritePath = style.sprite; + + // Handle both string and array cases for spritePath + if (Array.isArray(spritePath)) { + // For array format, update each sprite object's URL to be absolute + style.sprite = spritePath.map(spriteObj => { + const isAbsoluteUrl = spriteObj.url.startsWith("http://") || spriteObj.url.startsWith("https://"); + if (isAbsoluteUrl) { + return spriteObj; + } + + return { + ...spriteObj, + url: `${baseUrl}${spriteObj.url}` + }; + }); + } else if (typeof spritePath === "string") { + if (!spritePath.startsWith("http://") && !spritePath.startsWith("https://")) { + style.sprite = `${baseUrl}${spritePath}`; + } + } + + // Detect on each source if it the 'tiles' URLs are relative and convert them to absolute URLs + for (const sourceKey in style.sources) { + const source = style.sources[sourceKey]; + for (const tileKey in source.tiles) { + const tileUrl = source.tiles[tileKey]; + const isAbsoluteUrl = tileUrl.startsWith("http://") || tileUrl.startsWith("https://"); + if (!isAbsoluteUrl) { + source.tiles[tileKey] = `${baseUrl}${tileUrl}`; + } + } + } + + return style as StyleSpecification; +} |
