Map.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <template>
  2. <div class="map-container">
  3. <div class="status-info">
  4. <div class="status-item">
  5. <span class="online-icon"></span>
  6. <span style="font-weight: bold;font-size: 17px">在线数量 {{ inlineCount }}</span>
  7. </div>
  8. <div class="status-item">
  9. <span class="offline-icon"></span>
  10. <span style="font-weight: bold;font-size: 17px">离线数量 {{ outlineCount }}</span>
  11. </div>
  12. </div>
  13. <div id="baidu-map" ref="mapContainer"></div>
  14. <div class="map-controls">
  15. <button @click="zoomIn">{{t('map.amplify')}}</button>
  16. <button @click="zoomOut">{{t('map.reduce')}}</button>
  17. <button @click="toggleMapType"
  18. >{{t('map.SwitchMapType')}}({{ mapType === 'BMAP_NORMAL_MAP' ? '地图' : '卫星' }})</button>
  19. <el-tree-select
  20. class="my-el-select"
  21. v-model="queryParams.deptId"
  22. :data="treeList"
  23. :props="defaultProps"
  24. check-strictly
  25. node-key="id"
  26. :placeholder="t('deviceForm.deptHolder')"
  27. filterable
  28. @change="getData"
  29. />
  30. </div>
  31. </div>
  32. <DeviceMonitorDrawer :model-value="drawerVisible" @update:model-value="(val) => (drawerVisible = val)" :id="deviceId" :deviceName="deviceName" :lastLineTime="lastLineTime"
  33. :ifLine="ifLine" :dept="dept" :deviceCode="deviceCode"
  34. ref="showDrawer" />
  35. </template>
  36. <script setup lang="ts">
  37. import { onBeforeUnmount, onMounted, ref } from 'vue'
  38. import { DICT_TYPE, getDictLabel } from '@/utils/dict'
  39. import { IotDeviceApi, IotDeviceVO } from '@/api/pms/device'
  40. import DeviceMonitorDrawer from '@/views/pms/map/DeviceMonitorDrawer.vue'
  41. import * as DeptApi from '@/api/system/dept'
  42. import {defaultProps, handleTree} from "@/utils/tree";
  43. interface Cluster {
  44. lng: number
  45. lat: number
  46. count: number
  47. devices?: IotDeviceVO[]
  48. }
  49. defineOptions({ name: 'DeviceMap' })
  50. const showDrawer = ref()
  51. const drawerVisible = ref<boolean>(false)
  52. const mapContainer = ref<HTMLElement | null>(null)
  53. const map = ref<any>(null)
  54. const mapType = ref<'BMAP_NORMAL_MAP' | 'BMAP_SATELLITE_MAP'>('BMAP_NORMAL_MAP')
  55. const clusters = ref<Cluster[]>([])
  56. const deviceId = ref()
  57. const deviceName = ref('')
  58. const lastLineTime = ref('')
  59. const ifLine = ref()
  60. const dept = ref('')
  61. const deviceCode = ref('')
  62. const { t } = useI18n() // 国际化
  63. const inlineCount = ref(0)
  64. const outlineCount = ref(0)
  65. // 设备数据示例
  66. const devices = ref<IotDeviceVO[]>()
  67. const treeList = ref<Tree[]>([]) // 树形结构
  68. // 初始化地图
  69. const initMap = () => {
  70. if (!mapContainer.value) return
  71. const script = document.createElement('script')
  72. script.src = `https://api.map.baidu.com/api?v=3.0&ak=c0crhdxQ5H7WcqbcazGr7mnHrLa4GmO0&type=webgl`
  73. script.async = true
  74. script.onload = () => {
  75. if ((window as any).BMap) {
  76. map.value = new (window as any).BMap.Map(mapContainer.value)
  77. const point = new (window as any).BMap.Point(104.114129, 37.550339)
  78. map.value.centerAndZoom(point, 5)
  79. map.value.enableScrollWheelZoom(true)
  80. map.value.setMapType((window as any)[mapType.value])
  81. map.value.addControl(new (window as any).BMap.NavigationControl())
  82. map.value.addControl(new (window as any).BMap.ScaleControl())
  83. initDeviceMarkers()
  84. map.value.addEventListener('zoomend', () => {
  85. initDeviceMarkers()
  86. })
  87. } else {
  88. console.error('百度地图API加载失败')
  89. }
  90. }
  91. script.onerror = () => {
  92. console.error('百度地图API加载失败')
  93. }
  94. document.head.appendChild(script)
  95. }
  96. const initDeviceMarkers = () => {
  97. if (!map.value) return
  98. map.value.clearOverlays()
  99. const zoomLevel = map.value.getZoom()
  100. if (zoomLevel > 8) {
  101. debugger
  102. // 高缩放级别下显示单个设备标记
  103. devices.value.forEach((device) => {
  104. const point = new (window as any).BMap.Point(device.lng, device.lat)
  105. const marker = createDeviceMarker(device, point)
  106. map.value.addOverlay(marker)
  107. })
  108. } else {
  109. debugger
  110. // 低缩放级别下进行聚合
  111. clusters.value = clusterDevices(devices.value, map.value)
  112. clusters.value.forEach((cluster) => {
  113. if (cluster.count === 1) {
  114. // 只有一个设备时显示设备图标
  115. const device = cluster.devices?.[0]
  116. if (device) {
  117. const point = new (window as any).BMap.Point(device.lng, device.lat)
  118. const marker = createDeviceMarker(device, point)
  119. map.value.addOverlay(marker)
  120. }
  121. } else if (cluster.count > 1) {
  122. // 多个设备时显示聚合标签
  123. const point = new (window as any).BMap.Point(cluster.lng, cluster.lat)
  124. const label = createClusterLabel(cluster, point)
  125. map.value.addOverlay(label)
  126. }
  127. })
  128. }
  129. }
  130. const createDeviceMarker = (device: IotDeviceVO, point: any) => {
  131. // 根据设备是否在线选择不同的图标
  132. const iconUrl = device.ifInline === 3
  133. ? import.meta.env.VITE_BASE_URL + '/images/newding.svg'
  134. : import.meta.env.VITE_BASE_URL + '/images/newfail.svg';
  135. debugger
  136. const marker = new (window as any).BMap.Marker(point, {
  137. icon: new (window as any).BMap.Icon(
  138. iconUrl,
  139. new (window as any).BMap.Size(40, 47),
  140. {
  141. anchor: new (window as any).BMap.Size(25, 40)
  142. // imageOffset: new (window as any).BMap.Size(0, -5)
  143. }
  144. )
  145. });
  146. // 添加点击事件
  147. marker.addEventListener('click', () => {
  148. showDeviceInfoWindow(device, point);
  149. });
  150. return marker;
  151. };
  152. const createClusterLabel = (cluster: Cluster, point: any) => {
  153. const label = new (window as any).BMap.Label(cluster.count.toString(), {
  154. position: point,
  155. offset: new (window as any).BMap.Size(-10, -10)
  156. })
  157. // 创建一个 style 标签并添加到 head 中,定义呼吸动画
  158. const style = document.createElement('style')
  159. style.textContent = `
  160. @keyframes breathing {
  161. 0% {
  162. border-color: rgba(255, 255, 255, 0.3);
  163. }
  164. 50% {
  165. border-color: rgba(255, 255, 255, 0.8);
  166. }
  167. 100% {
  168. border-color: rgba(255, 255, 255, 0.3);
  169. }
  170. }
  171. `
  172. document.head.appendChild(style)
  173. // 根据 cluster.count 的值设置不同的背景颜色
  174. const backgroundColor = cluster.count > 10 ? '#1d4eed' : '#f67d1a';
  175. // 初始样式
  176. label.setStyle({
  177. color: '#fff',
  178. backgroundColor: backgroundColor,
  179. borderRadius: '50%',
  180. width: '50px',
  181. height: '50px',
  182. textAlign: 'center',
  183. lineHeight: '40px',
  184. cursor: 'pointer',
  185. fontSize: '18px',
  186. fontWeight: 'bold',
  187. boxShadow: '0 2px 4px rgba(0, 0, 0, 0.2)',
  188. transition: 'all 0.3s ease',
  189. border: '6px solid rgba(255, 255, 255, 0.5)', // 添加带有不透明度的边框
  190. animation: 'breathing 1.5s infinite' // 添加呼吸动画
  191. })
  192. // 鼠标悬停样式
  193. const hoverStyle = {
  194. backgroundColor: '#2196df',
  195. transform: 'scale(1.1)'
  196. }
  197. // 鼠标移出样式
  198. const normalStyle = {
  199. backgroundColor: backgroundColor,
  200. transform: 'scale(1)'
  201. }
  202. // 添加点击事件
  203. label.addEventListener('click', () => {
  204. if (map.value) {
  205. const currentZoom = map.value.getZoom();
  206. const MAX_ZOOM = 12; // 手动设定最大缩放级别
  207. const newZoom = Math.min(currentZoom + 2, MAX_ZOOM);
  208. map.value.setZoom(newZoom);
  209. map.value.panTo(point);
  210. initDeviceMarkers(); // 重新计算并显示标记和聚合标签
  211. }
  212. })
  213. return label
  214. }
  215. const clusterDevices = (devices: IotDeviceVO[], map: any): Cluster[] => {
  216. const clusters: Cluster[] = []
  217. const gridSize = getGridSize(map.getZoom())
  218. const gridMap = new Map<string, Cluster>()
  219. devices.forEach((device) => {
  220. const gridKey = `${Math.floor(device.lng / gridSize)}_${Math.floor(device.lat / gridSize)}`
  221. if (!gridMap.has(gridKey)) {
  222. gridMap.set(gridKey, {
  223. lng: Math.floor(device.lng / gridSize) * gridSize + gridSize / 2,
  224. lat: Math.floor(device.lat / gridSize) * gridSize + gridSize / 2,
  225. count: 0,
  226. devices: []
  227. })
  228. }
  229. const cluster = gridMap.get(gridKey)!
  230. cluster.count++
  231. if (!cluster.devices) cluster.devices = []
  232. cluster.devices.push(device)
  233. })
  234. return Array.from(gridMap.values())
  235. }
  236. const getGridSize = (zoom: number): number => {
  237. debugger
  238. if (zoom <= 5) return 5
  239. if (zoom <= 8) return 3
  240. if (zoom < 10) return 0.1
  241. return 0.1
  242. }
  243. const showDeviceInfoWindow = (device: IotDeviceVO, point: any) => {
  244. DeptApi.getDept(device.deptId).then(res=>{
  245. dept.value = res.name
  246. const content = `
  247. <div style="display: flex;flex-direction: column;justify-content: center;border: 1px solid #ccc;">
  248. <div style="margin-top: 1px;padding: 8px">
  249. <p><strong>${t('iotDevice.code')}:</strong> ${device.deviceCode}</p>
  250. <p><strong>${t('iotDevice.name')}:</strong> ${device.deviceName}</p>
  251. <p><strong>${t('iotDevice.dept')}:</strong> ${res.name}</p>
  252. <p><strong>${t('form.position')}:</strong> ${device.location?device.location.replaceAll('"',''):''}</p>
  253. <p><strong>${t('dict.status')}:</strong> ${getDictLabel(DICT_TYPE.PMS_DEVICE_STATUS, device.deviceStatus)}</p>
  254. <p><strong>${t('monitor.online')}:</strong> ${getDictLabel(DICT_TYPE.IOT_DEVICE_STATUS, device.ifInline)}</p>
  255. <p><strong>${t('monitor.latestDataTime')}:</strong> ${device.lastInlineTime}</p>
  256. </div>
  257. <div style="margin-bottom: 5px;padding: 8px">
  258. <button id="device-detail-btn" style=" background-color: #2196f3;
  259. color: white;
  260. border: none;
  261. padding: 8px 16px;
  262. border-radius: 4px;
  263. cursor: pointer;">${t('fault.view')}</button>
  264. </div>
  265. </div>
  266. `
  267. const infoWindow = new (window as any).BMap.InfoWindow(content, {
  268. width: 350,
  269. height: 270
  270. })
  271. map.value.openInfoWindow(infoWindow, point)
  272. // 事件绑定(需延迟确保DOM加载)
  273. setTimeout(function () {
  274. document.getElementById('device-detail-btn').addEventListener('click', function () {
  275. drawerVisible.value = true
  276. deviceId.value = device.id;
  277. deviceCode.value = device.deviceCode
  278. deviceName.value = device.deviceName
  279. ifLine.value = device.ifInline
  280. lastLineTime.value = device.lastInlineTime
  281. showDrawer.value.openDrawer()
  282. })
  283. }, 200)
  284. })
  285. }
  286. const zoomIn = () => {
  287. if (map.value) {
  288. map.value.zoomIn()
  289. }
  290. }
  291. const zoomOut = () => {
  292. if (map.value) {
  293. map.value.zoomOut()
  294. }
  295. }
  296. const toggleMapType = () => {
  297. if (!map.value) return
  298. mapType.value = mapType.value === 'BMAP_NORMAL_MAP' ? 'BMAP_SATELLITE_MAP' : 'BMAP_NORMAL_MAP'
  299. map.value.setMapType((window as any)[mapType.value])
  300. }
  301. const queryParams = reactive({
  302. deptId: undefined
  303. })
  304. const getData = async () => {
  305. debugger
  306. await IotDeviceApi.getMapDevice(queryParams).then((res) => {
  307. devices.value = res.filter((item)=> item.lat!=0&&item.lng!=0)
  308. outlineCount.value = devices.value.filter((item)=> item.ifInline===4).length
  309. inlineCount.value = devices.value.filter((item)=> item.ifInline===3).length
  310. initMap()
  311. })
  312. }
  313. onMounted(async () => {
  314. await getData()
  315. const res = await DeptApi.getSimpleDeptList()
  316. treeList.value = []
  317. treeList.value.push(...handleTree(res))
  318. debugger
  319. })
  320. onBeforeUnmount(() => {
  321. if (map.value) {
  322. // 清除地图上的覆盖物
  323. map.value.clearOverlays();
  324. // 移除地图容器中的内容
  325. if (mapContainer.value) {
  326. mapContainer.value.innerHTML = '';
  327. }
  328. // 解除地图的事件绑定
  329. map.value.removeEventListener('zoomend', initDeviceMarkers);
  330. }
  331. })
  332. // return {
  333. // mapContainer,
  334. // selectedDevice,
  335. // hoverDevice,
  336. // // deviceStatusMap,
  337. // zoomIn,
  338. // zoomOut,
  339. // toggleMapType
  340. // }
  341. // })
  342. </script>
  343. <style scoped>
  344. .map-container {
  345. position: relative;
  346. width: 100%;
  347. height: 90vh;
  348. }
  349. #baidu-map {
  350. width: 100%;
  351. height: 100%;
  352. }
  353. .map-controls {
  354. position: absolute;
  355. top: 20px;
  356. right: 20px; /* 修改为right定位 */
  357. z-index: 1000;
  358. display: flex;
  359. flex-direction: column; /* 垂直排列按钮 */
  360. gap: 10px;
  361. }
  362. .map-controls button {
  363. padding: 5px 10px;
  364. background: #fff;
  365. border: 1px solid #ccc;
  366. border-radius: 3px;
  367. cursor: pointer;
  368. }
  369. .status-info {
  370. position: absolute;
  371. top: 20px;
  372. left: 40%;
  373. z-index: 1000;
  374. background: white;
  375. padding: 10px 15px;
  376. border-radius: 6px;
  377. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  378. display: flex;
  379. gap: 20px;
  380. }
  381. .status-item {
  382. display: flex;
  383. align-items: center;
  384. gap: 8px;
  385. font-size: 14px;
  386. height: 50px;
  387. }
  388. .online-icon {
  389. display: inline-block;
  390. width: 25px;
  391. height: 25px;
  392. background-image: url('https://aims.deepoil.cc/images/newding.svg');
  393. background-size: contain;
  394. background-repeat: no-repeat;
  395. background-position: center;
  396. }
  397. .offline-icon {
  398. display: inline-block;
  399. width: 25px;
  400. height: 25px;
  401. background-image: url('https://aims.deepoil.cc/images/newfail.svg');
  402. background-size: contain;
  403. background-repeat: no-repeat;
  404. background-position: center;
  405. }
  406. :deep(.el-select__selection) {
  407. height: 35px; /* 自定义高度 */
  408. }
  409. :deep(.el-select__placeholder.is-transparent){
  410. color: orangered;
  411. }
  412. </style>