多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.0 KiB

<script lang="ts" setup>
import 'leaflet/dist/leaflet.css'
import * as L from 'leaflet'
import { IsGalleryInj, IsGridInj, IsMapInj, onMounted, provide, ref } from '#imports'
provide(IsGalleryInj, ref(false))
provide(IsGridInj, ref(false))
provide(IsMapInj, ref(true))
const reloadViewDataHook = inject(ReloadViewDataHookInj)
const { formattedData, loadMapData, loadMapMeta, mapMetaData, geoDataFieldColumn } = useMapViewStoreOrThrow()
2 years ago
const markerRef = ref()
const myMapRef = ref()
const markersRef = ref<L.MarkerClusterGroup | undefined>()
onBeforeMount(async () => {
await loadMapMeta()
await loadMapData()
})
2 years ago
reloadViewDataHook?.on(async () => {
loadMapData()
2 years ago
loadMapMeta()
2 years ago
})
function addMarker(lat: number, long: number, popupContent: string) {
2 years ago
L.marker([50.5, 0]).addTo(myMapRef.value)
console.log('myMapRef.value', myMapRef.value)
const markerNew = markerRef?.value?.([lat, long])
markersRef?.value?.addLayer(markerNew)
2 years ago
myMapRef?.value?.addLayer(markerRef.value)
if (markerNew) {
markerNew.bindPopup(popupContent)
}
return markerNew
2 years ago
}
watch([formattedData, mapMetaData], () => {
markersRef.value?.clearLayers()
formattedData.value?.forEach((row) => {
const primaryGeoDataColumnTitle = geoDataFieldColumn.value?.title
if (primaryGeoDataColumnTitle == null) {
throw new Error('Cannot find primary geo data column title')
}
const primaryGeoDataValue = row[primaryGeoDataColumnTitle]
const listItems = Object.entries(row)
.map(([key, val]) => {
2 years ago
const prettyVal = val !== null && (typeof val === 'object' || Array.isArray(val)) ? JSON.stringify(val) : val
return `<li><b>${key}</b>: <br/>${prettyVal}</li>`
})
.join('')
const popupContent = `<ul>${listItems}</ul>`
const [lat, long] = primaryGeoDataValue.split(';').map(parseFloat)
addMarker(lat, long, popupContent)
})
})
onMounted(async () => {
const { map, tileLayer, marker } = await import('leaflet')
await import('leaflet.markercluster')
const myMap = map('map').setView([51.505, -0.09], 13)
markerRef.value = marker
myMapRef.value = myMap
markersRef.value = L.markerClusterGroup()
2 years ago
// addMarker(52, 0, 'jdksauwdk')
// {
// iconCreateFunction(cluster) {
// return L.divIcon({ html: `<b>${cluster.getChildCount()}</b>` })
// // return L.divIcon({ html: `<b>${cluster.getChildCount()}</b>`, className: 'FOOBAR' })
// },
// }
// )
tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
2 years ago
maxZoom: 10,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}).addTo(myMap)
})
</script>
<template>
2 years ago
<div class="flex flex-col h-full w-full nounderline">
<client-only placeholder="Loading...">
2 years ago
<div id="map"></div>
</client-only>
</div>
</template>
<style scoped>
#map {
height: 100vh;
}
2 years ago
.nounderline {
text-decoration: none;
}
2 years ago
/* .marker-cluster b {
background-color: rgba(226, 36, 36, 0.6);
} */
/* .FOOBAR {
background-color: pink;
} */
</style>