2023-03-11 02:37:07 +01:00
|
|
|
<script lang="ts">
|
2023-07-16 04:24:32 +02:00
|
|
|
import {onDestroy, onMount} from "svelte"
|
|
|
|
import * as maplibre from "maplibre-gl"
|
2023-07-17 01:43:53 +02:00
|
|
|
import type {Map} from "maplibre-gl"
|
2023-07-16 04:24:32 +02:00
|
|
|
import type {Readable, Writable} from "svelte/store"
|
|
|
|
import {get, writable} from "svelte/store"
|
|
|
|
import {AvailableRasterLayers} from "../../Models/RasterLayers"
|
|
|
|
import {Utils} from "../../Utils";
|
|
|
|
|
2023-07-17 01:43:53 +02:00
|
|
|
/**
|
|
|
|
* The 'MaplibreMap' maps various event sources onto MapLibre.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-07-16 04:24:32 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2023-07-17 00:19:01 +02:00
|
|
|
export let center: {lng: number, lat: number} | Readable<{ lng: number; lat: number }> = writable({lng: 0, lat: 0})
|
2023-07-16 04:24:32 +02:00
|
|
|
export let zoom: Readable<number> = writable(1)
|
|
|
|
|
|
|
|
const styleUrl = AvailableRasterLayers.maplibre.properties.url
|
|
|
|
|
|
|
|
let _map: Map
|
|
|
|
onMount(() => {
|
|
|
|
|
2023-07-17 00:19:01 +02:00
|
|
|
let _center: {lng: number, lat: number}
|
|
|
|
if(typeof center["lng"] === "number" && typeof center["lat"] === "number"){
|
|
|
|
_center = <any> center
|
|
|
|
}else{
|
|
|
|
_center = get(<any> center)
|
|
|
|
}
|
|
|
|
|
2023-07-16 04:24:32 +02:00
|
|
|
|
|
|
|
_map = new maplibre.Map({
|
|
|
|
container,
|
|
|
|
style: styleUrl,
|
|
|
|
zoom: get(zoom),
|
2023-07-17 00:19:01 +02:00
|
|
|
center: _center,
|
2023-07-16 04:24:32 +02:00
|
|
|
maxZoom: 24,
|
|
|
|
interactive: true,
|
|
|
|
attributionControl: false,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
_map.on("load", function () {
|
|
|
|
_map.resize()
|
|
|
|
})
|
|
|
|
map.set(_map)
|
|
|
|
|
2023-06-14 20:39:36 +02:00
|
|
|
})
|
2023-07-16 04:24:32 +02:00
|
|
|
onDestroy(async () => {
|
|
|
|
await Utils.waitFor(250);
|
|
|
|
if (_map) _map.remove();
|
|
|
|
map = null;
|
|
|
|
});
|
|
|
|
|
2023-03-11 02:37:07 +01:00
|
|
|
</script>
|
2023-06-14 20:39:36 +02:00
|
|
|
|
2023-07-16 04:24:32 +02:00
|
|
|
<svelte:head>
|
|
|
|
<link
|
|
|
|
href="./maplibre-gl.css"
|
|
|
|
rel="stylesheet"
|
|
|
|
/>
|
|
|
|
</svelte:head>
|
|
|
|
|
2023-07-17 00:19:01 +02:00
|
|
|
<div bind:this={container} class="map" id="map" style=" position: relative;
|
2023-07-16 04:24:32 +02:00
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;"></div>
|
|
|
|
|
2023-03-11 02:37:07 +01:00
|
|
|
|