mapcomplete/UI/Map/OverlayMap.svelte

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

70 lines
2.2 KiB
Svelte
Raw Normal View History

2023-05-18 15:44:54 +02:00
<script lang="ts">
/**
* 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
export let placedOverMapProperties: MapProperties
export let placedOverMap: UIEventSource<MlMap>
2023-05-18 15:44:54 +02:00
export let rasterLayer: UIEventSource<RasterLayerPolygon>
2023-05-18 15:44:54 +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),
})
altproperties.allowMoving.setData(false)
altproperties.allowZooming.setData(false)
2023-05-18 15:44:54 +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
}
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
function updateLocation() {
if (!placedOverMap.data || !altmap.data) {
return
2023-05-18 15:44:54 +02:00
}
altmap.data.resize()
const { lon, lat } = placedOverMapProperties.location.data
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
onDestroy(placedOverMapProperties.location.addCallbackAndRunD(updateLocation))
updateLocation()
window.setTimeout(updateLocation, 150)
window.setTimeout(updateLocation, 500)
2023-05-18 15:44:54 +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>
<MaplibreMap map={altmap} />