<!-- 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 mapRef = ref() const publicPath = window.location.origin // console.log(publicPath, 'publicPath') // 展示图例数据 const legendShowData = ref(['1', '2', '3', '0']) // 地图图例数据 const legendData = ref([ { name: '正常', url: `${publicPath}/image/well/闸井-正常.png`, value: '1', }, { name: '异常', url: `${publicPath}/image/well/闸井-异常.png`, value: '2', }, { name: '离线', url: `${publicPath}/image/well/闸井-离线.png`, value: '3', }, { name: '未监控', url: `${publicPath}/image/well/闸井-未部署.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) } resetDraw() } // 初次加载标识 const pageIsFirst = ref(true) // 绘制标记点 const drawMarker = () => { const style = [ { url: `${publicPath}/image/well/闸井-正常.png`, // 图标地址 anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, { url: `${publicPath}/image/well/闸井-异常.png`, // 图标地址 anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, { url: `${publicPath}/image/well/闸井-离线.png`, // 图标地址 anchor: new mapRef.value.AMap.Pixel(4, 4), // 图标显示位置偏移量,基准点为图标左上角 size: new mapRef.value.AMap.Size(20, 20), // 图标的尺寸 zIndex: 99, // 每种样式图标的叠加顺序,数字越大越靠前 }, { url: `${publicPath}/image/well/闸井-未部署.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 = $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] : 0, id: item.id, row: item, })) mapRef.value.addMassMarks({ path: data, zIndex: 111, zooms: [3, 20], style, }) // if (pageIsFirst.value) { mapRef.value.map.setCenter([116.397428, 39.90923]) } else { mapRef.value.map.setFitView() } mapRef.value.map.setZoom(10.5) } // 点标记弹窗 const detail = ref() const detailRef = ref() const massMarksClick = (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), // 偏移 }) // 打开信息窗口 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(() => { // mapRef.value.map.setZoom(14) mapRef.value.map.setFitView() mapRef.value.map.setCenter(data.event.data.lnglat) }) } // 打开信息窗体 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) }) } function resetDraw() { mapRef.value.removeMassMarks() if (detailRef.value) { detailRef.value.close() } if ($props.data) { // 清空标记点重新绘制 drawMarker() } } // 地图绘制完毕 const completeMap = () => { console.log('地图绘制完毕') // 绘制海量点 resetDraw() setTimeout(() => { pageIsFirst.value = false }, 2000) } defineExpose({ openInfoDetail, drawMarker, resetDraw }) </script> <template> <div :style="`height: ${$props.height}px`" class="map-container"> <!-- 设备信息窗体 --> <detail-info ref="detailRef" /> <!-- 地图 --> <a-map ref="mapRef" @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" /> <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; .legend-item { margin: 10px 0; // font-weight: 700; display: flex; align-items: center; .img { width: 20px; height: 20px; } &:hover { cursor: pointer; } } } </style>