|
| 1 | +<script lang="ts"> |
| 2 | +import { |
| 3 | + PropType, Ref, defineComponent, onMounted, ref, |
| 4 | +} from 'vue'; |
| 5 | +import maplibre, { Map } from 'maplibre-gl'; |
| 6 | +
|
| 7 | +export default defineComponent({ |
| 8 | + name: 'MapSelection', |
| 9 | + props: { |
| 10 | + defaultMapSettings: { |
| 11 | + type: Object as PropType<{ location: { |
| 12 | + center: [number, number]; |
| 13 | + zoom: number; |
| 14 | + } }>, |
| 15 | + required: true, |
| 16 | + }, |
| 17 | + }, |
| 18 | + emits: ['update:settings'], |
| 19 | + setup(props, { emit }) { |
| 20 | + const mapContainer = ref<HTMLDivElement | null>(null); |
| 21 | + const mapInstance: Ref<Map | null> = ref(null); |
| 22 | +
|
| 23 | + const mapMove = () => { |
| 24 | + if (mapInstance.value) { |
| 25 | + const center = mapInstance.value.getCenter(); |
| 26 | + const zoom = mapInstance.value.getZoom(); |
| 27 | + emit('update:settings', { |
| 28 | + location: { |
| 29 | + center: [center.lng, center.lat], |
| 30 | + zoom, |
| 31 | + }, |
| 32 | + }); |
| 33 | + } |
| 34 | + }; |
| 35 | + // Initialize Map |
| 36 | + onMounted(() => { |
| 37 | + if (!mapContainer.value) return; |
| 38 | + mapInstance.value = new maplibre.Map({ |
| 39 | + container: mapContainer.value, |
| 40 | + style: { |
| 41 | + version: 8, |
| 42 | + sources: { |
| 43 | + osm: { |
| 44 | + type: 'raster', |
| 45 | + tiles: [ |
| 46 | + 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 47 | + 'https://b.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 48 | + 'https://c.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 49 | + ], |
| 50 | + tileSize: 256, |
| 51 | + attribution: '© OpenStreetMap contributors', |
| 52 | + }, |
| 53 | + }, |
| 54 | + layers: [ |
| 55 | + { |
| 56 | + id: 'osm-tiles', |
| 57 | + type: 'raster', |
| 58 | + source: 'osm', |
| 59 | + maxzoom: 19, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + center: props.defaultMapSettings.location.center, |
| 64 | + zoom: props.defaultMapSettings.location.zoom, |
| 65 | + }); |
| 66 | + mapInstance.value.on('load', () => { |
| 67 | + if (mapInstance.value) { |
| 68 | + mapInstance.value.on('move', mapMove); |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | +
|
| 73 | + return { |
| 74 | + mapContainer, |
| 75 | + }; |
| 76 | + }, |
| 77 | +}); |
| 78 | +</script> |
| 79 | + |
| 80 | +<template> |
| 81 | + <div ref="mapContainer" class="map-container" /> |
| 82 | +</template> |
| 83 | + |
| 84 | +<style scoped> |
| 85 | + .map-container { |
| 86 | + width: 100%; |
| 87 | + height: 400px; |
| 88 | + } |
| 89 | +</style> |
0 commit comments