diff options
Diffstat (limited to 'src/components/LocationsMap.svelte')
| -rw-r--r-- | src/components/LocationsMap.svelte | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/src/components/LocationsMap.svelte b/src/components/LocationsMap.svelte index 5de5ac56..eec704a8 100644 --- a/src/components/LocationsMap.svelte +++ b/src/components/LocationsMap.svelte @@ -4,8 +4,6 @@ import Popup from "$comp/Popup.svelte"; import { back } from "$lib/utils"; - import "maplibre-gl/dist/maplibre-gl.css"; - let { locations } = $props(); let map; let mapContainer = $state(), @@ -172,10 +170,46 @@ let locationMarkers = {}; let popups = {}; let counter = 0; + let maplibrePromise; + + const loadMaplibre = async () => { + if (window.maplibregl) return window.maplibregl; + if (maplibrePromise) return maplibrePromise; + + maplibrePromise = new Promise((resolve, reject) => { + const existing = document.querySelector("script[data-maplibre]"); + if (existing) { + if (window.maplibregl) return resolve(window.maplibregl); + existing.addEventListener("load", () => resolve(window.maplibregl)); + existing.addEventListener("error", reject); + return; + } + + const css = document.createElement("link"); + css.rel = "stylesheet"; + css.href = + "https://unpkg.com/[email protected]/dist/maplibre-gl.css"; + document.head.appendChild(css); + + const script = document.createElement("script"); + script.src = + "https://unpkg.com/[email protected]/dist/maplibre-gl.js"; + script.async = true; + script.defer = true; + script.dataset.maplibre = "true"; + script.onload = () => resolve(window.maplibregl); + script.onerror = reject; + document.head.appendChild(script); + }); + + return maplibrePromise; + }; - onMount(() => { + onMount(async () => { if (browser) { - import("maplibre-gl").then(({ default: maplibre }) => { + await tick(); + if (!mapContainer) return; + loadMaplibre().then((maplibre) => { map = new maplibre.Map({ container: mapContainer, style: "https://tiles.stadiamaps.com/styles/stamen_toner.json", |
