| 
									
										
										
										
											2023-03-11 02:37:07 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |   import { onDestroy, onMount } from "svelte" | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |   import type { Map, MapOptions } from "maplibre-gl" | 
					
						
							| 
									
										
										
										
											2023-12-12 13:53:49 +01:00
										 |  |  |   import * as maplibre from "maplibre-gl" | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |   import type { Writable } from "svelte/store" | 
					
						
							|  |  |  |   import { get } from "svelte/store" | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |   import { AvailableRasterLayers } from "../../Models/RasterLayers" | 
					
						
							|  |  |  |   import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2023-12-15 01:46:01 +01:00
										 |  |  |   import { ariaLabel } from "../../Utils/ariaLabel" | 
					
						
							|  |  |  |   import Translations from "../i18n/Translations" | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |   import type { MapProperties } from "../../Models/MapProperties" | 
					
						
							|  |  |  |   import type { RasterLayerProperties } from "../../Models/RasterLayerProperties" | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * The 'MaplibreMap' maps various event sources onto MapLibre. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Beware: this map will _only_ be set by this component | 
					
						
							|  |  |  |    * It should thus be treated as a 'store' by external parties | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |   export let map: Writable<Map> = undefined | 
					
						
							|  |  |  |   export let mapProperties: MapProperties = undefined | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-25 22:48:29 +01:00
										 |  |  |   export let interactive: boolean = true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |   let container: HTMLElement | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let _map: Map | 
					
						
							|  |  |  |   onMount(() => { | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |     const { lon, lat } = mapProperties?.location?.data ?? { lon: 0, lat: 0 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const rasterLayer: RasterLayerProperties = mapProperties?.rasterLayer?.data?.properties | 
					
						
							|  |  |  |     let styleUrl: string | 
					
						
							|  |  |  |     if (rasterLayer?.type === "vector") { | 
					
						
							|  |  |  |       styleUrl = rasterLayer?.style ?? rasterLayer?.url ?? AvailableRasterLayers.defaultBackgroundLayer.properties.url | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |       const defaultLayer = AvailableRasterLayers.defaultBackgroundLayer.properties | 
					
						
							|  |  |  |       styleUrl = defaultLayer.style ?? defaultLayer.url | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |     console.log("INiting mapLIbremap with style", styleUrl) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const options: MapOptions = { | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |       container, | 
					
						
							|  |  |  |       style: styleUrl, | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |       zoom: mapProperties?.zoom?.data ?? 1, | 
					
						
							|  |  |  |       center: { lng: lon, lat }, | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |       maxZoom: 24, | 
					
						
							|  |  |  |       interactive: true, | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |       attributionControl: false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _map = new maplibre.Map(options) | 
					
						
							| 
									
										
										
										
											2023-12-12 13:53:49 +01:00
										 |  |  |     window.requestAnimationFrame(() => { | 
					
						
							|  |  |  |       _map.resize() | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2024-03-25 04:17:13 +01:00
										 |  |  |     _map.on("load", function() { | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |       _map.resize() | 
					
						
							| 
									
										
										
										
											2023-12-30 15:24:30 +01:00
										 |  |  |       const canvas = _map.getCanvas() | 
					
						
							| 
									
										
										
										
											2023-12-25 22:48:29 +01:00
										 |  |  |       if (interactive) { | 
					
						
							|  |  |  |         ariaLabel(canvas, Translations.t.general.visualFeedback.navigation) | 
					
						
							|  |  |  |         canvas.role = "application" | 
					
						
							|  |  |  |         canvas.tabIndex = 0 | 
					
						
							| 
									
										
										
										
											2023-12-30 15:24:30 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2023-12-25 22:48:29 +01:00
										 |  |  |         canvas.tabIndex = -1 | 
					
						
							|  |  |  |         _map.getContainer().tabIndex = -1 | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |     }) | 
					
						
							|  |  |  |     map.set(_map) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   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> | 
					
						
							| 
									
										
										
										
											2023-07-28 01:02:31 +02:00
										 |  |  |   <link href="./maplibre-gl.css" rel="stylesheet" /> | 
					
						
							| 
									
										
										
										
											2023-07-16 04:24:32 +02:00
										 |  |  | </svelte:head> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-21 01:46:18 +01:00
										 |  |  | <div bind:this={container} class="map relative top-0 left-0 h-full w-full" id="map" /> |