<!-- Description: 闸井监测-地图 Author: 李亚光 Date: 2023-07-08 --> <script lang="ts" setup name="WellMap"> import detailInfo from './detailInfoDialog.vue' import AMap from '@/components/map/index.vue' const $props = defineProps({ height: { type: Number, default: 0, }, data: { type: Array, default: () => ([]), }, }) const $emits = defineEmits(['clickMarker']) // 地图实例 const mapRef = ref() const publicPath = window.location.href.split('#')[0] // console.log(publicPath, 'publicPath') // 展示图例数据 const legendShowData = ref(['1', '2', '3']) const resetLegend = () => { legendShowData.value = ['1', '2', '3'] } // 地图图例数据 const legendData = ref([ { name: '正常', url: `${publicPath}/image/station/station-normal.png`, value: '1', }, { name: '异常', url: `${publicPath}/image/station/station-error.png`, value: '2', }, { name: '离线', url: `${publicPath}/image/station/station-offline.png`, value: '3', }, // { // name: '未监控', // url: `${publicPath}/image/station/station-will.png`, // value: '0', // }, ]) // 控制图例 const clickLegend = (type: string) => { if (legendShowData.value.includes(type)) { legendShowData.value = legendShowData.value.filter((item: string) => item !== type) } else { legendShowData.value.push(type) } // console.log(legendShowData.value, 'legendShowData.value') resetDraw() } // 初次加载标识 const pageIsFirst = ref(true) // 绘制标记点 const drawMarker = () => { const style = [ { url: `${publicPath}/image/station/station-normal.png`, // 图标地址 // anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 // size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, { url: `${publicPath}/image/station/station-error.png`, // 图标地址 // anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 // size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, { url: `${publicPath}/image/station/station-offline.png`, // 图标地址 // anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 // size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, { url: `${publicPath}/image/station/station-will.png`, // 图标地址 // anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 // size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, ] // (0:未监控,1:正常,2:异常,3:离线) const styleDict = { 0: 3, 1: 0, 2: 1, 3: 2, } as { [key: string]: number } const data = JSON.parse(JSON.stringify($props.data)).map((item: any) => ({ ...item, monitorState: item.monitorState || '0' })).filter((item: any) => legendShowData.value.includes(item.monitorState)).filter((item: any) => item.lngGaode && item.latGaode).map((item: any) => ({ lnglat: [item.lngGaode, item.latGaode], name: item.ledgerName, style: item.monitorState ? styleDict[item.monitorState] : 3, id: item.id, row: item, })) // console.log(data, '场站点位') mapRef.value.addCluster(data, style) // mapRef.value.addMassMarks({ // path: data, // zIndex: 111, // zooms: [3, 20], // style, // }) if (pageIsFirst.value) { mapRef.value.map.setCenter([116.397428, 39.90923]) mapRef.value.map.setZoom(10.5) } // else { // mapRef.value.map.setFitView() // } } // 点标记弹窗 const detail = ref() const detailRef = ref() const massMarksClick = (data: any) => { // console.log(data, '11111') $emits('clickMarker', data.data.row) mapRef.value.map.setZoom(10.5) // 定义弹窗 // detail.value = new mapRef.value.AMap.InfoWindow({ // closeWhenClickMap: true, // 是否在鼠标点击地图后关闭信息窗体 // autoMove: true, // 是否自动调整窗体到视野内 // isCustom: true, // 自定义信息窗体 // content: detailRef.value.$el, // 窗体内容(vue组件) // offset: new mapRef.value.AMap.Pixel(9, -5), // 偏移 // }) // // 打开信息窗口 // detail.value.open(data.map, data.event.data.lnglat) // // 初始化信息窗口 detailRef.value.initDialog({ overlay: data.event.target, infoWindow: detail.value, info: data.event.data, map: mapRef.value.map, }) setTimeout(() => { const center = JSON.parse(JSON.stringify(data.event.data.lnglat)) center[1] = Number(center[1]) mapRef.value.map.setCenter(center) }) } // 打开信息窗体 const openInfoDetail = (data: any) => { mapRef.value.map.setZoom(10.5) // 定义弹窗 detail.value = new mapRef.value.AMap.InfoWindow({ closeWhenClickMap: true, // 是否在鼠标点击地图后关闭信息窗体 autoMove: true, // 是否自动调整窗体到视野内 isCustom: true, // 自定义信息窗体 content: detailRef.value.$el, // 窗体内容(vue组件) offset: new mapRef.value.AMap.Pixel(9, -5), // 偏移 }) // 打开信息窗口 setTimeout(() => { detail.value.open(mapRef.value.map, data.lnglat) // 初始化信息窗口 detailRef.value.initDialog({ infoWindow: detail.value, info: data, map: mapRef.value.map, }) // mapRef.value.map.setCenter(data.lnglat) // mapRef.value.map.setFitView() const center = JSON.parse(JSON.stringify(data.lnglat)) center[1] = Number(center[1]) mapRef.value.map.setCenter(center) }) } function resetDraw() { mapRef.value.removeCluster() if (detailRef.value) { detailRef.value.close() } if ($props.data) { // 清空标记点重新绘制 drawMarker() } } // 地图绘制完毕 const completeMap = () => { // console.log('地图绘制完毕') // 绘制海量点 resetDraw() setTimeout(() => { pageIsFirst.value = false }, 2000) } defineExpose({ openInfoDetail, drawMarker, resetDraw, mapRef,resetLegend }) </script> <template> <div :style="`height: ${$props.height}px`" class="map-container"> <!-- 设备信息窗体 --> <detail-info ref="detailRef" /> <!-- 地图 --> <a-map ref="mapRef" :show-pieple-layer="true" @complete="completeMap" @massMarksClick="massMarksClick" /> <!-- 图例 --> <div class="legend"> <div v-for="item in legendData" :key="item.value" class="legend-item" @click="clickLegend(item.value)"> <img v-show="legendShowData.includes(item.value)" class="img" :src="item.url"> <!-- <span v-show="!legendShowData.includes(item.value)" class="img" /> --> <img v-show="!legendShowData.includes(item.value)" class="img transparent" :src="item.url"> <span style="margin-left: 5px;">{{ item.name }}</span> </div> </div> </div> </template> <style lang="scss" scoped> .map-container { width: 100%; position: relative; } .legend { background: rgb(255 255 255 / 80%); position: absolute; z-index: 1; left: 15px; bottom: 10px; padding: 0 10px; border-radius: 3px; color: #000; .legend-item { margin: 10px 0; // font-weight: 700; display: flex; align-items: center; .img { width: 20px; height: 20px; } .transparent { opacity: 0.25; } &:hover { cursor: pointer; } } } </style>