MapComplete/UI/Map/MaplibreMap.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
1.8 KiB
Svelte
Raw Normal View History

<script lang="ts">
/**
* The 'MaplibreMap' maps various event sources onto MapLibre.
*
* As it replaces the old 'MinimapObj' onto MapLibre and the existing codebase, this is sometimes a bit awkward
*/
import {onDestroy, onMount} from "svelte"
import type {Map} from "maplibre-gl"
import * as maplibre from "maplibre-gl"
import type {Readable, Writable} from "svelte/store"
import {get, writable} from "svelte/store"
import {AvailableRasterLayers} from "../../Models/RasterLayers"
import {Utils} from "../../Utils";
/**
* Beware: this map will _only_ be set by this component
* It should thus be treated as a 'store' by external parties
*/
export let map: Writable<Map>
let container: HTMLElement
export let attribution = false
export let center: Readable<{ lng: number; lat: number }> = writable({lng: 0, lat: 0})
export let zoom: Readable<number> = writable(1)
const styleUrl = AvailableRasterLayers.maplibre.properties.url
let _map: Map
onMount(() => {
_map = new maplibre.Map({
container,
style: styleUrl,
zoom: get(zoom),
center: get(center),
maxZoom: 24,
interactive: true,
attributionControl: false,
});
_map.on("load", function () {
_map.resize()
})
map.set(_map)
})
onDestroy(async () => {
await Utils.waitFor(250);
if (_map) _map.remove();
map = null;
});
</script>
<svelte:head>
<link
href="./maplibre-gl.css"
rel="stylesheet"
/>
</svelte:head>
<main>
2023-07-16 19:37:11 +02:00
<div bind:this={container} class="map" id="map" style=" position: relative;
top: 0;
bottom: 0;
width: 100%;
height: 100%;"></div>
</main>
<style>
</style>