| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * The overlay map is a bit a weird map: | 
					
						
							|  |  |  |    * it is a HTML-component which is intended to be placed _over_ another map. | 
					
						
							|  |  |  |    * It will align itself in order to seamlessly show the same location; but possibly in a different style | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   import MaplibreMap from "./MaplibreMap.svelte" | 
					
						
							|  |  |  |   import { Store, UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  |   import { Map as MlMap } from "maplibre-gl" | 
					
						
							|  |  |  |   import { MapLibreAdaptor } from "./MapLibreAdaptor" | 
					
						
							|  |  |  |   import type { MapProperties } from "../../Models/MapProperties" | 
					
						
							|  |  |  |   import { onDestroy } from "svelte" | 
					
						
							|  |  |  |   import type { RasterLayerPolygon } from "../../Models/RasterLayers" | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   export let placedOverMapProperties: MapProperties | 
					
						
							| 
									
										
										
										
											2024-01-10 02:25:24 +01:00
										 |  |  |   export let placedOverMap: Store<MlMap> | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   export let interactive: boolean = undefined | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   export let rasterLayer: UIEventSource<RasterLayerPolygon> | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   export let visible: Store<boolean> = undefined | 
					
						
							|  |  |  |   let altmap: UIEventSource<MlMap> = new UIEventSource(undefined) | 
					
						
							|  |  |  |   let altproperties = new MapLibreAdaptor(altmap, { | 
					
						
							|  |  |  |     rasterLayer, | 
					
						
							|  |  |  |     zoom: UIEventSource.feedFrom(placedOverMapProperties.zoom), | 
					
						
							| 
									
										
										
										
											2024-02-03 14:33:10 +01:00
										 |  |  |     rotation: UIEventSource.feedFrom(placedOverMapProperties.rotation), | 
					
						
							|  |  |  |     pitch: UIEventSource.feedFrom(placedOverMapProperties.pitch) | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   }) | 
					
						
							|  |  |  |   altproperties.allowMoving.setData(false) | 
					
						
							|  |  |  |   altproperties.allowZooming.setData(false) | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   function pixelCenterOf(map: UIEventSource<MlMap>): [number, number] { | 
					
						
							|  |  |  |     const rect = map?.data?.getCanvas()?.getBoundingClientRect() | 
					
						
							|  |  |  |     if (!rect) { | 
					
						
							|  |  |  |       return undefined | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |     const x = (rect.left + rect.right) / 2 | 
					
						
							|  |  |  |     const y = (rect.top + rect.bottom) / 2 | 
					
						
							|  |  |  |     return [x, y] | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   function updateLocation() { | 
					
						
							|  |  |  |     if (!placedOverMap.data || !altmap.data) { | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |     altmap.data.resize() | 
					
						
							|  |  |  |     const altMapCenter = pixelCenterOf(altmap) | 
					
						
							|  |  |  |     const c = placedOverMap.data.unproject(altMapCenter) | 
					
						
							|  |  |  |     altproperties.location.setData({ lon: c.lng, lat: c.lat }) | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   onDestroy(placedOverMapProperties.location.addCallbackAndRunD(updateLocation)) | 
					
						
							|  |  |  |   updateLocation() | 
					
						
							|  |  |  |   window.setTimeout(updateLocation, 150) | 
					
						
							|  |  |  |   window.setTimeout(updateLocation, 500) | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   if (visible) { | 
					
						
							|  |  |  |     onDestroy( | 
					
						
							|  |  |  |       visible?.addCallbackAndRunD((v) => { | 
					
						
							|  |  |  |         if (!v) { | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         updateLocation() | 
					
						
							|  |  |  |         window.setTimeout(updateLocation, 150) | 
					
						
							|  |  |  |         window.setTimeout(updateLocation, 500) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-10 02:25:24 +01:00
										 |  |  | <MaplibreMap {interactive} map={altmap} /> |