|  | 
|  | 1 | +<script lang="ts"> | 
|  | 2 | +import { Map } from 'maplibre-gl' | 
|  | 3 | +
 | 
|  | 4 | +export type MapLibreProvider = () => Map | 
|  | 5 | +</script> | 
|  | 6 | +<script lang="ts" setup> | 
|  | 7 | +import { type SingleCoordinate, WEBMERCATOR, WGS84 } from '@geoadmin/coordinates' | 
|  | 8 | +import { round } from '@geoadmin/numbers' | 
|  | 9 | +import { Map } from 'maplibre-gl' | 
|  | 10 | +import proj4 from 'proj4' | 
|  | 11 | +import { computed, onMounted, provide, ref, watch } from 'vue' | 
|  | 12 | +import { useStore } from 'vuex' | 
|  | 13 | +
 | 
|  | 14 | +import type AbstractLayer from '@/api/layers/AbstractLayer.class' | 
|  | 15 | +
 | 
|  | 16 | +import { getVectorTilesBaseUrl } from '@/config/baseUrl.config' | 
|  | 17 | +import { VECTOR_LIGHT_BASE_MAP_STYLE_ID } from '@/config/vectortiles.config' | 
|  | 18 | +import MaplibreInternalLayer from '@/modules/map/components/maplibre/MaplibreInternalLayer.vue' | 
|  | 19 | +
 | 
|  | 20 | +import 'maplibre-gl/dist/maplibre-gl.css' | 
|  | 21 | +
 | 
|  | 22 | +const dispatcher = { | 
|  | 23 | +    dispatcher: 'MapLibreMap.vue', | 
|  | 24 | +} | 
|  | 25 | +
 | 
|  | 26 | +const centerChangeTriggeredByMe = ref<boolean>() | 
|  | 27 | +
 | 
|  | 28 | +const store = useStore() | 
|  | 29 | +
 | 
|  | 30 | +const zoom = computed<number>(() => store.state.position.zoom - 1) | 
|  | 31 | +const centerEpsg4326 = computed<SingleCoordinate>(() => store.getters.centerEpsg4326) | 
|  | 32 | +const visibleLayers = computed<AbstractLayer[]>(() => store.getters.visibleLayers) | 
|  | 33 | +const currentBackgroundLayer = computed<AbstractLayer>(() => store.getters.currentBackgroundLayer) | 
|  | 34 | +
 | 
|  | 35 | +let mapLibreMap: Map | undefined | 
|  | 36 | +
 | 
|  | 37 | +onMounted(() => { | 
|  | 38 | +    mapLibreMap = new Map({ | 
|  | 39 | +        container: 'maplibre-map', | 
|  | 40 | +        style: `${getVectorTilesBaseUrl()}styles/${VECTOR_LIGHT_BASE_MAP_STYLE_ID}/testing/poc-terrain/style.json`, | 
|  | 41 | +        center: centerEpsg4326.value, | 
|  | 42 | +        zoom: zoom.value, | 
|  | 43 | +        maxPitch: 75, | 
|  | 44 | +    }) | 
|  | 45 | +
 | 
|  | 46 | +    // Click management | 
|  | 47 | +    let clickStartTimestamp: number = 0 | 
|  | 48 | +    let lastClickDuration: number = 0 | 
|  | 49 | +    mapLibreMap.on('mousedown', () => { | 
|  | 50 | +        clickStartTimestamp = performance.now() | 
|  | 51 | +    }) | 
|  | 52 | +    mapLibreMap.on('mouseup', () => { | 
|  | 53 | +        lastClickDuration = performance.now() - clickStartTimestamp | 
|  | 54 | +        clickStartTimestamp = 0 | 
|  | 55 | +    }) | 
|  | 56 | +    mapLibreMap.on('click', (e) => { | 
|  | 57 | +        const clickLocation: SingleCoordinate = proj4(WGS84.epsg, WEBMERCATOR.epsg, [ | 
|  | 58 | +            round(e.lngLat.lng, 6), | 
|  | 59 | +            round(e.lngLat.lat, 6), | 
|  | 60 | +        ]) | 
|  | 61 | +        store.dispatch('click', { | 
|  | 62 | +            coordinate: clickLocation, | 
|  | 63 | +            millisecondsSpentMouseDown: lastClickDuration, | 
|  | 64 | +            ...dispatcher, | 
|  | 65 | +        }) | 
|  | 66 | +    }) | 
|  | 67 | +
 | 
|  | 68 | +    // position management | 
|  | 69 | +    mapLibreMap.on('moveend', () => { | 
|  | 70 | +        const mapboxCenter = mapLibreMap.getCenter() | 
|  | 71 | +        centerChangeTriggeredByMe.value = true | 
|  | 72 | +        store.dispatch('setCenter', { | 
|  | 73 | +            center: proj4(WGS84.epsg, WEBMERCATOR.epsg, [ | 
|  | 74 | +                round(mapboxCenter.lng, 6), | 
|  | 75 | +                round(mapboxCenter.lat, 6), | 
|  | 76 | +            ]), | 
|  | 77 | +            ...dispatcher, | 
|  | 78 | +        }) | 
|  | 79 | +        const newZoom = round(mapLibreMap.getZoom(), 3) | 
|  | 80 | +        if (newZoom && newZoom !== zoom.value) { | 
|  | 81 | +            store.dispatch('setZoom', { | 
|  | 82 | +                zoom: newZoom + 1, | 
|  | 83 | +                ...dispatcher, | 
|  | 84 | +            }) | 
|  | 85 | +        } | 
|  | 86 | +    }) | 
|  | 87 | +}) | 
|  | 88 | +
 | 
|  | 89 | +provide<MapLibreProvider>('getMapLibreMap', () => mapLibreMap) | 
|  | 90 | +provide('getStyle', () => style.value) | 
|  | 91 | +
 | 
|  | 92 | +watch(centerEpsg4326, (newCenter) => { | 
|  | 93 | +    if (mapLibreMap) { | 
|  | 94 | +        if (centerChangeTriggeredByMe.value) { | 
|  | 95 | +            centerChangeTriggeredByMe.value = false | 
|  | 96 | +        } else { | 
|  | 97 | +            mapLibreMap.flyTo({ | 
|  | 98 | +                center: newCenter, | 
|  | 99 | +                zoom: zoom.value, | 
|  | 100 | +            }) | 
|  | 101 | +        } | 
|  | 102 | +    } | 
|  | 103 | +}) | 
|  | 104 | +watch(zoom, (newZoom) => { | 
|  | 105 | +    mapLibreMap?.flyTo({ | 
|  | 106 | +        center: centerEpsg4326.value, | 
|  | 107 | +        zoom: newZoom, | 
|  | 108 | +    }) | 
|  | 109 | +}) | 
|  | 110 | +</script> | 
|  | 111 | + | 
|  | 112 | +<template> | 
|  | 113 | +    <div id="maplibre-map"> | 
|  | 114 | +        <MaplibreInternalLayer | 
|  | 115 | +            v-for="(layer, index) in visibleLayers" | 
|  | 116 | +            :key="layer.id" | 
|  | 117 | +            :layer-config="layer" | 
|  | 118 | +            :z-index="index + (currentBackgroundLayer ? 1 : 0)" | 
|  | 119 | +        /> | 
|  | 120 | +        <slot /> | 
|  | 121 | +    </div> | 
|  | 122 | +</template> | 
|  | 123 | + | 
|  | 124 | +<style> | 
|  | 125 | +#maplibre-map { | 
|  | 126 | +    width: 100%; | 
|  | 127 | +    height: 100%; | 
|  | 128 | +} | 
|  | 129 | +</style> | 
0 commit comments