Map.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, 5);
  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 > 10) {
  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. clusters.value = clusterDevices(devices.value, map.value);
  92. clusters.value.forEach(cluster => {
  93. if (cluster.count === 1) {
  94. // 只有一个设备时显示设备图标
  95. const device = cluster.devices?.[0];
  96. if (device) {
  97. const point = new (window as any).BMap.Point(device.lng, device.lat);
  98. const marker = createDeviceMarker(device, point);
  99. map.value.addOverlay(marker);
  100. }
  101. } else if (cluster.count > 1) {
  102. // 多个设备时显示聚合标签
  103. const point = new (window as any).BMap.Point(cluster.lng, cluster.lat);
  104. const label = createClusterLabel(cluster, point);
  105. map.value.addOverlay(label);
  106. }
  107. });
  108. }
  109. };
  110. const createDeviceMarker = (device: IotDeviceVO, point: any) => {
  111. const marker = new (window as any).BMap.Marker(point, {
  112. icon: new (window as any).BMap.Icon('https://iot.deepoil.cc/images/ding300.svg', new (window as any).BMap.Size(28, 30),
  113. {
  114. anchor: new (window as any).BMap.Size(10, 25),
  115. // imageOffset: new (window as any).BMap.Size(0, -5)
  116. }
  117. )
  118. });
  119. // 添加点击事件
  120. marker.addEventListener('click', () => {
  121. showDeviceInfoWindow(device, point);
  122. });
  123. return marker;
  124. };
  125. const createClusterLabel = (cluster: Cluster, point: any) => {
  126. const label = new (window as any).BMap.Label(cluster.count.toString(), {
  127. position: point,
  128. offset: new (window as any).BMap.Size(-10, -10)
  129. });
  130. // 创建一个 style 标签并添加到 head 中,定义呼吸动画
  131. const style = document.createElement('style');
  132. style.textContent = `
  133. @keyframes breathing {
  134. 0% {
  135. border-color: rgba(255, 255, 255, 0.3);
  136. }
  137. 50% {
  138. border-color: rgba(255, 255, 255, 0.8);
  139. }
  140. 100% {
  141. border-color: rgba(255, 255, 255, 0.3);
  142. }
  143. }
  144. `;
  145. document.head.appendChild(style);
  146. // 初始样式
  147. label.setStyle({
  148. color: '#fff',
  149. backgroundColor: '#c38f65',
  150. borderRadius: '50%',
  151. width: '50px',
  152. height: '50px',
  153. textAlign: 'center',
  154. lineHeight: '40px',
  155. cursor: 'pointer',
  156. fontSize: '18px',
  157. fontWeight: 'bold',
  158. boxShadow: '0 2px 4px rgba(0, 0, 0, 0.2)',
  159. transition: 'all 0.3s ease',
  160. border: '4px solid rgba(255, 255, 255, 0.5)', // 添加带有不透明度的边框
  161. animation: 'breathing 2s infinite' // 添加呼吸动画
  162. });
  163. // 鼠标悬停样式
  164. const hoverStyle = {
  165. backgroundColor: '#2196df',
  166. transform: 'scale(1.1)'
  167. };
  168. // 鼠标移出样式
  169. const normalStyle = {
  170. backgroundColor: '#c38f65',
  171. transform: 'scale(1)'
  172. };
  173. // // 添加鼠标悬停事件
  174. // label.addEventListener('mouseover', () => {
  175. // Object.assign(label.getStyle(), hoverStyle);
  176. // label.setStyle(label.getStyle());
  177. // });
  178. //
  179. // // 添加鼠标移出事件
  180. // label.addEventListener('mouseout', () => {
  181. // Object.assign(label.getStyle(), normalStyle);
  182. // label.setStyle(label.getStyle());
  183. // });
  184. // 添加点击事件
  185. label.addEventListener('click', () => {
  186. map.value?.setZoom(10);
  187. map.value?.panTo(point);
  188. });
  189. return label;
  190. };
  191. const clusterDevices = (devices: IotDeviceVO[], map: any): Cluster[] => {
  192. const clusters: Cluster[] = [];
  193. debugger
  194. const gridSize = getGridSize(map.getZoom());
  195. const gridMap = new Map<string, Cluster>();
  196. devices.forEach(device => {
  197. const gridKey = `${Math.floor(device.lng / gridSize)}_${Math.floor(device.lat / gridSize)}`;
  198. if (!gridMap.has(gridKey)) {
  199. gridMap.set(gridKey, {
  200. lng: Math.floor(device.lng / gridSize) * gridSize + gridSize / 2,
  201. lat: Math.floor(device.lat / gridSize) * gridSize + gridSize / 2,
  202. count: 0,
  203. devices: []
  204. });
  205. }
  206. const cluster = gridMap.get(gridKey)!;
  207. cluster.count++;
  208. if (!cluster.devices) cluster.devices = [];
  209. cluster.devices.push(device);
  210. });
  211. return Array.from(gridMap.values());
  212. };
  213. const getGridSize = (zoom: number): number => {
  214. debugger
  215. if (zoom <= 5) return 2;
  216. if (zoom <= 8) return 1;
  217. if (zoom < 10) return 0.5;
  218. return 0.1;
  219. };
  220. const showDeviceInfoWindow = (device: IotDeviceVO, point: any) => {
  221. const content = `
  222. <div>
  223. <p><strong>设备编码:</strong> ${device.deviceCode}</p>
  224. <p><strong>设备名称:</strong> ${device.deviceName}</p>
  225. <p><strong>位置:</strong> ${device.location}</p>
  226. <p><strong>状态:</strong> ${getDictLabel(DICT_TYPE.PMS_DEVICE_STATUS, device.deviceStatus)}</p>
  227. </div>
  228. `;
  229. const infoWindow = new (window as any).BMap.InfoWindow(content, {
  230. width: 300,
  231. height: 150
  232. });
  233. map.value.openInfoWindow(infoWindow, point);
  234. };
  235. const zoomIn = () => {
  236. if (map.value) {
  237. map.value.zoomIn();
  238. }
  239. };
  240. const zoomOut = () => {
  241. if (map.value) {
  242. map.value.zoomOut();
  243. }
  244. };
  245. const toggleMapType = () => {
  246. if (!map.value) return;
  247. mapType.value = mapType.value === 'BMAP_NORMAL_MAP'
  248. ? 'BMAP_SATELLITE_MAP'
  249. : 'BMAP_NORMAL_MAP';
  250. map.value.setMapType((window as any)[mapType.value]);
  251. };
  252. const getData = async () =>{
  253. await IotDeviceApi.getMapDevice().then(res=>{
  254. devices.value = res
  255. initMap()
  256. })
  257. }
  258. onMounted(async () => {
  259. await getData();
  260. });
  261. onBeforeUnmount(() => {
  262. if (map.value) {
  263. map.value.destroy();
  264. }
  265. });
  266. return {
  267. mapContainer,
  268. selectedDevice,
  269. hoverDevice,
  270. // deviceStatusMap,
  271. zoomIn,
  272. zoomOut,
  273. toggleMapType
  274. };
  275. }
  276. });
  277. </script>
  278. <style scoped>
  279. .map-container {
  280. position: relative;
  281. width: 100%;
  282. height: 100vh;
  283. }
  284. #baidu-map {
  285. width: 100%;
  286. height: 100%;
  287. }
  288. .map-controls {
  289. position: absolute;
  290. top: 20px;
  291. right: 20px; /* 修改为right定位 */
  292. z-index: 1000;
  293. display: flex;
  294. flex-direction: column; /* 垂直排列按钮 */
  295. gap: 10px;
  296. }
  297. .map-controls button {
  298. padding: 5px 10px;
  299. background: #fff;
  300. border: 1px solid #ccc;
  301. border-radius: 3px;
  302. cursor: pointer;
  303. }
  304. </style>