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