Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 11x 7x 7x 7x 7x 7x 7x 7x 7x 1x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 2x 2x 1x 1x 4x 6x 6x 6x 6x 3x 3x 2x 2x 1x 1x 2x 1x 7x 7x 7x 7x 1x 1x 1x 6x 6x 6x 1x 1x 1x 5x 4x 5x 2x 2x 1x 5x 5x 5x 5x 7x 8x 1x 1x 7x 8x 1x 7x 7x 7x | <template>
<div class="map-view" :class="{ 'map-view-pickable': pickable }" data-testid="map-view">
<div ref="container" class="map-view-canvas" :style="{ height }"></div>
<p v-if="failed" class="map-view-unavailable" role="status" data-testid="map-unavailable">
{{ t('map.unavailable') }}
</p>
</div>
</template>
<script setup lang="ts">
/**
* An interactive map on the self-hosted basemap (docs/architecture/MAPS.md):
* the only component using MapLibre, loaded on demand where a map shows.
* Markers take the space's main color when inside a space (`--space-primary`),
* else the app's.
*
* @implements UC-SPACE-022.1, UC-ENTITY-028.2
*/
import { onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
import * as maplibregl from 'maplibre-gl'
import 'maplibre-gl/dist/maplibre-gl.css'
// MapLibre finds its worker next to its own file, which Vite moves: without it no tile is drawn
import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?url'
import { Protocol } from 'pmtiles'
import { useUILanguage } from '@/composables/useUILanguage'
import { basemapStyle, basemapUrl } from '@/utils/basemap'
export interface MapMarker {
id: string
lat: number
lng: number
/** Read by screen readers and shown on hover */
label?: string
}
const props = withDefaults(
defineProps<{
markers?: MapMarker[]
/** Clicking the map emits `pick` with the place clicked */
pickable?: boolean
/** Panning and zooming; off for a small preview */
interactive?: boolean
lang?: string
height?: string
}>(),
{
markers: () => [],
pickable: false,
interactive: true,
lang: 'en',
height: '320px'
}
)
const emit = defineEmits<{
(e: 'marker-click', id: string): void
(e: 'pick', place: { lat: number; lng: number }): void
}>()
const { t } = useUILanguage()
const container = ref<HTMLElement | null>(null)
const map = shallowRef<maplibregl.Map | null>(null)
const failed = ref(false)
let shownMarkers: maplibregl.Marker[] = []
let fileUrl = ''
/** A street's zoom for a single place; the whole world when there is none */
const SINGLE_PLACE_ZOOM = 15
const WORLD = { center: [10, 30] as [number, number], zoom: 1 }
let protocolAdded = false
function addProtocol() {
Iif (protocolAdded) return
maplibregl.setWorkerUrl(workerUrl)
maplibregl.addProtocol('pmtiles', new Protocol().tile)
protocolAdded = true
}
function drawMarkers() {
const current = map.value
Iif (!current) return
for (const marker of shownMarkers) marker.remove()
shownMarkers = props.markers.map((place) => {
const element = document.createElement(props.pickable ? 'div' : 'button')
element.className = 'map-view-marker'
// MapLibre positions the element by its transform: the pin's shape turns inside it
const pin = document.createElement('span')
pin.className = 'map-view-pin'
element.appendChild(pin)
element.dataset.testid = `map-marker-${place.id}`
if (place.label) {
element.title = place.label
element.setAttribute('aria-label', place.label)
}
if (!props.pickable) {
element.setAttribute('type', 'button')
element.addEventListener('click', (event) => {
event.stopPropagation()
emit('marker-click', place.id)
})
}
return new maplibregl.Marker({ element, anchor: 'bottom' })
.setLngLat([place.lng, place.lat])
.addTo(current)
})
}
/** Shows every marker: a single one at a street's zoom, none the whole world */
function fitMarkers(animate = false) {
const current = map.value
Iif (!current) return
const places = props.markers
if (places.length === 0) {
current.jumpTo(WORLD)
} else if (places.length === 1) {
const center: [number, number] = [places[0].lng, places[0].lat]
if (animate) current.easeTo({ center, zoom: Math.max(current.getZoom(), SINGLE_PLACE_ZOOM) })
else current.jumpTo({ center, zoom: SINGLE_PLACE_ZOOM })
} else {
const bounds = new maplibregl.LngLatBounds()
for (const place of places) bounds.extend([place.lng, place.lat])
current.fitBounds(bounds, { padding: 48, maxZoom: SINGLE_PLACE_ZOOM, animate })
}
}
onMounted(async () => {
try {
addProtocol()
fileUrl = await basemapUrl()
} catch (error) {
console.error('[MapView] the basemap cannot be read:', error)
failed.value = true
return
}
Iif (!container.value) return
let current: maplibregl.Map
try {
current = new maplibregl.Map({
container: container.value,
style: basemapStyle(fileUrl, props.lang),
interactive: props.interactive,
attributionControl: {},
cooperativeGestures: props.interactive && !props.pickable,
...WORLD
})
} catch (error) {
// No WebGL (an old browser, or turned off)
console.error('[MapView] the map cannot be drawn:', error)
failed.value = true
return
}
if (props.interactive) {
current.addControl(new maplibregl.NavigationControl({ showCompass: false }), 'top-right')
}
current.on('click', (event: maplibregl.MapMouseEvent) => {
if (!props.pickable) return
// 6 decimals: about 10 cm, more than a phone's GPS gives
const round = (value: number) => Math.round(value * 1e6) / 1e6
emit('pick', { lat: round(event.lngLat.lat), lng: round(event.lngLat.lng) })
})
current.on('error', (event: maplibregl.ErrorEvent) => {
console.error('[MapView] map error:', event.error)
})
map.value = current
drawMarkers()
fitMarkers()
})
watch(
() => props.markers,
() => {
drawMarkers()
fitMarkers(true)
},
{ deep: true }
)
watch(
() => props.lang,
(lang) => {
Eif (map.value && fileUrl) map.value.setStyle(basemapStyle(fileUrl, lang))
}
)
onBeforeUnmount(() => {
map.value?.remove()
map.value = null
})
</script>
<style scoped>
.map-view {
position: relative;
width: 100%;
}
.map-view-canvas {
width: 100%;
border-radius: 0.5rem;
overflow: hidden;
background: var(--bs-tertiary-bg);
}
.map-view-pickable .map-view-canvas :deep(.maplibregl-canvas) {
cursor: crosshair;
}
.map-view-unavailable {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
padding: 1rem;
text-align: center;
font-size: 0.9rem;
opacity: 0.8;
}
/* A pin in the space's main color (else the app's); its tip on the place */
.map-view-canvas :deep(.map-view-marker) {
width: 26px;
height: 34px;
padding: 0;
border: 0;
background: none;
cursor: pointer;
}
.map-view-canvas :deep(.map-view-pin) {
position: absolute;
top: 2px;
left: 2px;
width: 22px;
height: 22px;
border: 2px solid #fff;
border-radius: 50% 50% 50% 0;
transform: rotate(-45deg);
background: var(--space-primary, var(--bs-primary));
box-shadow: 0 1px 4px rgb(0 0 0 / 40%);
}
.map-view-pickable .map-view-canvas :deep(.map-view-marker) {
cursor: default;
}
</style>
|