Map.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <div class="map-container">
  3. <div id="baidu-map" ref="mapContainer"></div>
  4. <div class="map-controls">
  5. <button @click="zoomIn">放大</button>
  6. <button @click="zoomOut">缩小</button>
  7. <button @click="toggleMapType">切换地图类型({{ mapType === 'BMAP_NORMAL_MAP' ? '地图' : '卫星' }})</button>
  8. </div>
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, onMounted, onBeforeUnmount, ref } from 'vue';
  13. import {DICT_TYPE, getDictLabel} from "@/utils/dict";
  14. import { IotDeviceApi, IotDeviceVO } from '@/api/pms/device'
  15. import {aY} from "@fullcalendar/core/internal-common";
  16. interface Cluster {
  17. lng: number;
  18. lat: number;
  19. count: number;
  20. devices?: Device[];
  21. }
  22. export default defineComponent({
  23. name: 'ChinaDeviceMap',
  24. setup() {
  25. const mapContainer = ref<HTMLElement | null>(null);
  26. const map = ref<any>(null);
  27. const mapType = ref<'BMAP_NORMAL_MAP' | 'BMAP_SATELLITE_MAP'>('BMAP_NORMAL_MAP');
  28. const selectedDevice = ref<IotDeviceVO | null>(null);
  29. const hoverDevice = ref<IotDeviceVO | null>(null);
  30. const clusters = ref<Cluster[]>([]);
  31. // 设备数据示例
  32. const devices = ref<IotDeviceVO[]>();
  33. // const devices = ref<Device[]>([
  34. // { id: 'D001', deviceName: '设备1', location: '北京', deviceStatus: 1, lng: 84.053192, lat: 41.319412 },
  35. // { id: 'D002', deviceName: '设备2', location: '上海', deviceStatus: 2, lng: 121.474, lat: 31.230 },
  36. // { id: 'D003', deviceName: '设备3', location: '广州', deviceStatus: 1, lng: 113.264, lat: 23.129 },
  37. // { id: 'D004', deviceName: '设备4', location: '深圳', deviceStatus: 3, lng: 114.058, lat: 22.543 },
  38. // { id: 'D005', deviceName: '设备5', location: '成都', deviceStatus: 1, lng: 104.065, lat: 30.659 },
  39. // { id: 'D006', deviceName: '设备6', location: '武汉', deviceStatus: 2, lng: 114.305, lat: 30.593 },
  40. // { id: 'D007', deviceName: '设备7', location: '西安', deviceStatus: 1, lng: 108.948, lat: 34.263 },
  41. // { id: 'D008', deviceName: '设备8', location: '杭州', deviceStatus: 3, lng: 120.155, lat: 30.274 },
  42. // { id: 'D009', deviceName: '设备9', location: '南京', deviceStatus: 1, lng: 118.797, lat: 32.060 },
  43. // { id: 'D010', deviceName: '设备10', location: '重庆', deviceStatus: 2, lng: 106.505, lat: 29.533 },
  44. // ]);
  45. // const deviceStatusMap = {
  46. // 1: '在线',
  47. // 2: '离线',
  48. // 3: '异常'
  49. // };
  50. // 初始化地图
  51. const initMap = () => {
  52. if (!mapContainer.value) return;
  53. const script = document.createElement('script');
  54. script.src = `https://api.map.baidu.com/api?v=3.0&ak=c0crhdxQ5H7WcqbcazGr7mnHrLa4GmO0&type=webgl`;
  55. script.async = true;
  56. script.onload = () => {
  57. if ((window as any).BMap) {
  58. map.value = new (window as any).BMap.Map(mapContainer.value);
  59. const point = new (window as any).BMap.Point(104.114129, 37.550339);
  60. map.value.centerAndZoom(point, 6);
  61. map.value.enableScrollWheelZoom(true);
  62. map.value.setMapType((window as any)[mapType.value]);
  63. map.value.addControl(new (window as any).BMap.NavigationControl());
  64. map.value.addControl(new (window as any).BMap.ScaleControl());
  65. initDeviceMarkers();
  66. map.value.addEventListener('zoomend', () => {
  67. initDeviceMarkers();
  68. });
  69. } else {
  70. console.error('百度地图API加载失败');
  71. }
  72. };
  73. script.onerror = () => {
  74. console.error('百度地图API加载失败');
  75. };
  76. document.head.appendChild(script);
  77. };
  78. const initDeviceMarkers = () => {
  79. if (!map.value) return;
  80. map.value.clearOverlays();
  81. const zoomLevel = map.value.getZoom();
  82. if (zoomLevel > 8) {
  83. // 高缩放级别下显示单个设备标记
  84. devices.value.forEach(device => {
  85. const point = new (window as any).BMap.Point(device.lng, device.lat);
  86. const marker = createDeviceMarker(device, point);
  87. map.value.addOverlay(marker);
  88. });
  89. } else {
  90. // 低缩放级别下进行聚合
  91. debugger;
  92. clusters.value = clusterDevices(devices.value, map.value);
  93. clusters.value.forEach(cluster => {
  94. if (cluster.count === 1) {
  95. // 只有一个设备时显示设备图标
  96. const device = cluster.devices?.[0];
  97. if (device) {
  98. const point = new (window as any).BMap.Point(device.lng, device.lat);
  99. const marker = createDeviceMarker(device, point);
  100. map.value.addOverlay(marker);
  101. }
  102. } else if (cluster.count > 1) {
  103. // 多个设备时显示聚合标签
  104. const point = new (window as any).BMap.Point(cluster.lng, cluster.lat);
  105. const label = createClusterLabel(cluster, point);
  106. map.value.addOverlay(label);
  107. }
  108. });
  109. }
  110. };
  111. const createDeviceMarker = (device: IotDeviceVO, point: any) => {
  112. const marker = new (window as any).BMap.Marker(point, {
  113. icon: new (window as any).BMap.Icon('https://iot.deepoil.cc/images/ding300.svg', new (window as any).BMap.Size(28, 30),
  114. {
  115. anchor: new (window as any).BMap.Size(14, 25),
  116. // imageOffset: new (window as any).BMap.Size(0, -5)
  117. }
  118. )
  119. });
  120. // 添加点击事件
  121. marker.addEventListener('click', () => {
  122. showDeviceInfoWindow(device, point);
  123. });
  124. return marker;
  125. };
  126. const createClusterLabel = (cluster: Cluster, point: any) => {
  127. const label = new (window as any).BMap.Label(cluster.count.toString(), {
  128. position: point,
  129. offset: new (window as any).BMap.Size(-10, -10)
  130. });
  131. // 创建一个 style 标签并添加到 head 中,定义呼吸动画
  132. const style = document.createElement('style');
  133. style.textContent = `
  134. @keyframes breathing {
  135. 0% {
  136. border-color: rgba(255, 255, 255, 0.3);
  137. }
  138. 50% {
  139. border-color: rgba(255, 255, 255, 0.8);
  140. }
  141. 100% {
  142. border-color: rgba(255, 255, 255, 0.3);
  143. }
  144. }
  145. `;
  146. document.head.appendChild(style);
  147. // 初始样式
  148. label.setStyle({
  149. color: '#fff',
  150. backgroundColor: '#c38f65',
  151. borderRadius: '50%',
  152. width: '50px',
  153. height: '50px',
  154. textAlign: 'center',
  155. lineHeight: '40px',
  156. cursor: 'pointer',
  157. fontSize: '18px',
  158. fontWeight: 'bold',
  159. boxShadow: '0 2px 4px rgba(0, 0, 0, 0.2)',
  160. transition: 'all 0.3s ease',
  161. border: '4px solid rgba(255, 255, 255, 0.5)', // 添加带有不透明度的边框
  162. animation: 'breathing 2s infinite' // 添加呼吸动画
  163. });
  164. // 鼠标悬停样式
  165. const hoverStyle = {
  166. backgroundColor: '#2196df',
  167. transform: 'scale(1.1)'
  168. };
  169. // 鼠标移出样式
  170. const normalStyle = {
  171. backgroundColor: '#c38f65',
  172. transform: 'scale(1)'
  173. };
  174. // // 添加鼠标悬停事件
  175. // label.addEventListener('mouseover', () => {
  176. // Object.assign(label.getStyle(), hoverStyle);
  177. // label.setStyle(label.getStyle());
  178. // });
  179. //
  180. // // 添加鼠标移出事件
  181. // label.addEventListener('mouseout', () => {
  182. // Object.assign(label.getStyle(), normalStyle);
  183. // label.setStyle(label.getStyle());
  184. // });
  185. // 添加点击事件
  186. label.addEventListener('click', () => {
  187. map.value?.setZoom(9);
  188. map.value?.panTo(point);
  189. });
  190. return label;
  191. };
  192. const clusterDevices = (devices: IotDeviceVO[], map: any): Cluster[] => {
  193. const clusters: Cluster[] = [];
  194. debugger
  195. const gridSize = getGridSize(map.getZoom());
  196. const gridMap = new Map<string, Cluster>();
  197. devices.forEach(device => {
  198. const gridKey = `${Math.floor(device.lng / gridSize)}_${Math.floor(device.lat / gridSize)}`;
  199. if (!gridMap.has(gridKey)) {
  200. gridMap.set(gridKey, {
  201. lng: Math.floor(device.lng / gridSize) * gridSize + gridSize / 2,
  202. lat: Math.floor(device.lat / gridSize) * gridSize + gridSize / 2,
  203. count: 0,
  204. devices: []
  205. });
  206. }
  207. const cluster = gridMap.get(gridKey)!;
  208. cluster.count++;
  209. if (!cluster.devices) cluster.devices = [];
  210. cluster.devices.push(device);
  211. });
  212. return Array.from(gridMap.values());
  213. };
  214. const getGridSize = (zoom: number): number => {
  215. debugger
  216. if (zoom <= 5) return 2;
  217. if (zoom <= 8) return 1;
  218. if (zoom < 10) return 5;
  219. return 0.1;
  220. };
  221. const deviceDetail = (device: IotDeviceVO, point: any) => {
  222. alert("abc")
  223. }
  224. const showDeviceInfoWindow = (device: IotDeviceVO, point: any) => {
  225. const content = `
  226. <div style="margin-top: 5px">
  227. <p><strong>设备编码:</strong> ${device.deviceCode}</p>
  228. <p><strong>设备名称:</strong> ${device.deviceName}</p>
  229. <p><strong>位置:</strong> ${device.location}</p>
  230. <p><strong>状态:</strong> ${getDictLabel(DICT_TYPE.PMS_DEVICE_STATUS, device.deviceStatus)}</p>
  231. </div>
  232. <div>
  233. <button :@click="deviceDetail" style=" background-color: #2196f3;
  234. color: white;
  235. border: none;
  236. padding: 8px 16px;
  237. border-radius: 4px;
  238. margin-top: 15px;
  239. cursor: pointer;">查看</button>
  240. </div>
  241. `;
  242. const infoWindow = new (window as any).BMap.InfoWindow(content, {
  243. width: 300,
  244. height: 180
  245. });
  246. map.value.openInfoWindow(infoWindow, point);
  247. };
  248. const zoomIn = () => {
  249. if (map.value) {
  250. map.value.zoomIn();
  251. }
  252. };
  253. const zoomOut = () => {
  254. if (map.value) {
  255. map.value.zoomOut();
  256. }
  257. };
  258. const toggleMapType = () => {
  259. if (!map.value) return;
  260. mapType.value = mapType.value === 'BMAP_NORMAL_MAP'
  261. ? 'BMAP_SATELLITE_MAP'
  262. : 'BMAP_NORMAL_MAP';
  263. map.value.setMapType((window as any)[mapType.value]);
  264. };
  265. const getData = async () =>{
  266. await IotDeviceApi.getMapDevice().then(res=>{
  267. devices.value = res
  268. initMap()
  269. })
  270. }
  271. onMounted(async () => {
  272. await getData();
  273. });
  274. onBeforeUnmount(() => {
  275. if (map.value) {
  276. map.value.destroy();
  277. }
  278. });
  279. return {
  280. mapContainer,
  281. selectedDevice,
  282. hoverDevice,
  283. // deviceStatusMap,
  284. zoomIn,
  285. zoomOut,
  286. toggleMapType
  287. };
  288. }
  289. });
  290. </script>
  291. <style scoped>
  292. .map-container {
  293. position: relative;
  294. width: 100%;
  295. height: 100vh;
  296. }
  297. #baidu-map {
  298. width: 100%;
  299. height: 100%;
  300. }
  301. .map-controls {
  302. position: absolute;
  303. top: 20px;
  304. right: 20px; /* 修改为right定位 */
  305. z-index: 1000;
  306. display: flex;
  307. flex-direction: column; /* 垂直排列按钮 */
  308. gap: 10px;
  309. }
  310. .map-controls button {
  311. padding: 5px 10px;
  312. background: #fff;
  313. border: 1px solid #ccc;
  314. border-radius: 3px;
  315. cursor: pointer;
  316. }
  317. </style>