Map.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 v-if="selectedDevice" class="device-info-popup">
  10. <div class="popup-header">
  11. <h3>设备详情</h3>
  12. <button @click="closeDeviceInfo">×</button>
  13. </div>
  14. <div class="popup-content">
  15. <p><strong>设备ID:</strong> {{ selectedDevice.id }}</p>
  16. <p><strong>设备名称:</strong> {{ selectedDevice.name }}</p>
  17. <p><strong>位置:</strong> {{ selectedDevice.location }}</p>
  18. <p><strong>状态:</strong> {{ deviceStatusMap[selectedDevice.status] }}</p>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script lang="ts">
  24. import { defineComponent, onMounted, onBeforeUnmount, ref } from 'vue';
  25. interface Device {
  26. id: string;
  27. name: string;
  28. location: string;
  29. status: number;
  30. lng: number;
  31. lat: number;
  32. }
  33. interface Cluster {
  34. lng: number;
  35. lat: number;
  36. count: number;
  37. devices?: Device[];
  38. }
  39. export default defineComponent({
  40. name: 'ChinaDeviceMap',
  41. setup() {
  42. const mapContainer = ref<HTMLElement | null>(null);
  43. const map = ref<any>(null);
  44. const mapType = ref<'BMAP_NORMAL_MAP' | 'BMAP_SATELLITE_MAP'>('BMAP_NORMAL_MAP');
  45. const selectedDevice = ref<Device | null>(null);
  46. const hoverDevice = ref<Device | null>(null);
  47. const clusters = ref<Cluster[]>([]);
  48. // 设备数据示例
  49. const devices = ref<Device[]>([
  50. { id: 'D001', name: '设备1', location: '北京', status: 1, lng: 116.404, lat: 39.915 },
  51. { id: 'D002', name: '设备2', location: '上海', status: 2, lng: 121.474, lat: 31.230 },
  52. { id: 'D003', name: '设备3', location: '广州', status: 1, lng: 113.264, lat: 23.129 },
  53. { id: 'D004', name: '设备4', location: '深圳', status: 3, lng: 114.058, lat: 22.543 },
  54. { id: 'D005', name: '设备5', location: '成都', status: 1, lng: 104.065, lat: 30.659 },
  55. { id: 'D006', name: '设备6', location: '武汉', status: 2, lng: 114.305, lat: 30.593 },
  56. { id: 'D007', name: '设备7', location: '西安', status: 1, lng: 108.948, lat: 34.263 },
  57. { id: 'D008', name: '设备8', location: '杭州', status: 3, lng: 120.155, lat: 30.274 },
  58. { id: 'D009', name: '设备9', location: '南京', status: 1, lng: 118.797, lat: 32.060 },
  59. { id: 'D010', name: '设备10', location: '重庆', status: 2, lng: 106.505, lat: 29.533 },
  60. ]);
  61. const deviceStatusMap = {
  62. 1: '在线',
  63. 2: '离线',
  64. 3: '异常'
  65. };
  66. // 初始化地图
  67. const initMap = () => {
  68. if (!mapContainer.value) return;
  69. const script = document.createElement('script');
  70. script.src = `https://api.map.baidu.com/api?v=3.0&ak=c0crhdxQ5H7WcqbcazGr7mnHrLa4GmO0&type=webgl`;
  71. script.async = true;
  72. script.onload = () => {
  73. if ((window as any).BMap) {
  74. map.value = new (window as any).BMap.Map(mapContainer.value);
  75. const point = new (window as any).BMap.Point(104.114129, 37.550339);
  76. map.value.centerAndZoom(point, 5);
  77. map.value.enableScrollWheelZoom(true);
  78. map.value.setMapType((window as any)[mapType.value]);
  79. map.value.addControl(new (window as any).BMap.NavigationControl());
  80. map.value.addControl(new (window as any).BMap.ScaleControl());
  81. initDeviceMarkers();
  82. map.value.addEventListener('zoomend', initDeviceMarkers);
  83. } else {
  84. console.error('百度地图API加载失败');
  85. }
  86. };
  87. script.onerror = () => {
  88. console.error('百度地图API加载失败');
  89. };
  90. document.head.appendChild(script);
  91. };
  92. const initDeviceMarkers = () => {
  93. if (!map.value) return;
  94. map.value.clearOverlays();
  95. const zoomLevel = map.value.getZoom();
  96. if (zoomLevel > 10) {
  97. // 高缩放级别下显示单个设备标记
  98. devices.value.forEach(device => {
  99. const point = new (window as any).BMap.Point(device.lng, device.lat);
  100. const marker = createDeviceMarker(device, point);
  101. map.value.addOverlay(marker);
  102. });
  103. } else {
  104. // 低缩放级别下进行聚合
  105. clusters.value = clusterDevices(devices.value, map.value);
  106. clusters.value.forEach(cluster => {
  107. if (cluster.count === 1) {
  108. // 只有一个设备时显示设备图标
  109. const device = cluster.devices?.[0];
  110. if (device) {
  111. const point = new (window as any).BMap.Point(device.lng, device.lat);
  112. const marker = createDeviceMarker(device, point);
  113. map.value.addOverlay(marker);
  114. }
  115. } else if (cluster.count > 1) {
  116. // 多个设备时显示聚合标签
  117. const point = new (window as any).BMap.Point(cluster.lng, cluster.lat);
  118. const label = createClusterLabel(cluster, point);
  119. map.value.addOverlay(label);
  120. }
  121. });
  122. }
  123. };
  124. const createDeviceMarker = (device: Device, point: any) => {
  125. debugger
  126. const marker = new (window as any).BMap.Marker(point, {
  127. icon: new (window as any).BMap.Icon('/images/dingweida.png', new (window as any).BMap.Size(103, 105),
  128. {
  129. anchor: new (window as any).BMap.Size(10, 25),
  130. imageOffset: new (window as any).BMap.Size(0, 0 - device.status * 25)
  131. }
  132. )
  133. });
  134. // 添加点击事件
  135. marker.addEventListener('click', () => {
  136. showDeviceInfo(device);
  137. });
  138. // 添加鼠标悬停事件
  139. marker.addEventListener('mouseover', () => {
  140. hoverDevice.value = device;
  141. });
  142. marker.addEventListener('mouseout', () => {
  143. hoverDevice.value = null;
  144. });
  145. return marker;
  146. };
  147. const createClusterLabel = (cluster: Cluster, point: any) => {
  148. const label = new (window as any).BMap.Label(cluster.count.toString(), {
  149. position: point,
  150. offset: new (window as any).BMap.Size(-10, -10)
  151. });
  152. label.setStyle({
  153. color: '#fff',
  154. backgroundColor: '#1890ff',
  155. borderRadius: '50%',
  156. width: '20px',
  157. height: '20px',
  158. textAlign: 'center',
  159. lineHeight: '20px',
  160. cursor: 'pointer',
  161. fontSize: '12px',
  162. fontWeight: 'bold'
  163. });
  164. // 添加点击事件
  165. label.addEventListener('click', () => {
  166. map.value?.setZoom(11);
  167. map.value?.panTo(point);
  168. });
  169. return label;
  170. };
  171. const clusterDevices = (devices: Device[], map: any): Cluster[] => {
  172. const clusters: Cluster[] = [];
  173. const gridSize = getGridSize(map.getZoom());
  174. const gridMap = new Map<string, Cluster>();
  175. devices.forEach(device => {
  176. const gridKey = `${Math.floor(device.lng / gridSize)}_${Math.floor(device.lat / gridSize)}`;
  177. if (!gridMap.has(gridKey)) {
  178. gridMap.set(gridKey, {
  179. lng: Math.floor(device.lng / gridSize) * gridSize + gridSize / 2,
  180. lat: Math.floor(device.lat / gridSize) * gridSize + gridSize / 2,
  181. count: 0,
  182. devices: []
  183. });
  184. }
  185. const cluster = gridMap.get(gridKey)!;
  186. cluster.count++;
  187. if (!cluster.devices) cluster.devices = [];
  188. cluster.devices.push(device);
  189. });
  190. return Array.from(gridMap.values());
  191. };
  192. const getGridSize = (zoom: number): number => {
  193. if (zoom <= 5) return 2;
  194. if (zoom <= 8) return 1;
  195. if (zoom <= 10) return 0.5;
  196. return 0.1;
  197. };
  198. const showDeviceInfo = (device: Device) => {
  199. selectedDevice.value = device;
  200. };
  201. const closeDeviceInfo = () => {
  202. selectedDevice.value = null;
  203. };
  204. const zoomIn = () => {
  205. if (map.value) {
  206. map.value.zoomIn();
  207. }
  208. };
  209. const zoomOut = () => {
  210. if (map.value) {
  211. map.value.zoomOut();
  212. }
  213. };
  214. const toggleMapType = () => {
  215. if (!map.value) return;
  216. mapType.value = mapType.value === 'BMAP_NORMAL_MAP'
  217. ? 'BMAP_SATELLITE_MAP'
  218. : 'BMAP_NORMAL_MAP';
  219. map.value.setMapType((window as any)[mapType.value]);
  220. };
  221. onMounted(() => {
  222. initMap();
  223. });
  224. onBeforeUnmount(() => {
  225. if (map.value) {
  226. map.value.destroy();
  227. }
  228. });
  229. return {
  230. mapContainer,
  231. selectedDevice,
  232. hoverDevice,
  233. deviceStatusMap,
  234. zoomIn,
  235. zoomOut,
  236. toggleMapType,
  237. showDeviceInfo,
  238. closeDeviceInfo
  239. };
  240. }
  241. });
  242. </script>
  243. <style scoped>
  244. .map-container {
  245. position: relative;
  246. width: 100%;
  247. height: 100vh;
  248. }
  249. #baidu-map {
  250. width: 100%;
  251. height: 100%;
  252. }
  253. .map-controls {
  254. position: absolute;
  255. top: 20px;
  256. left: 20px;
  257. z-index: 1000;
  258. display: flex;
  259. gap: 10px;
  260. }
  261. .map-controls button {
  262. padding: 5px 10px;
  263. background: #fff;
  264. border: 1px solid #ccc;
  265. border-radius: 3px;
  266. cursor: pointer;
  267. }
  268. .device-info-popup {
  269. position: absolute;
  270. top: 20px;
  271. right: 20px;
  272. z-index: 1000;
  273. width: 300px;
  274. background: #fff;
  275. border-radius: 5px;
  276. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  277. padding: 15px;
  278. }
  279. .popup-header {
  280. display: flex;
  281. justify-content: space-between;
  282. align-items: center;
  283. margin-bottom: 10px;
  284. }
  285. .popup-header button {
  286. background: none;
  287. border: none;
  288. font-size: 18px;
  289. cursor: pointer;
  290. }
  291. .popup-content p {
  292. margin: 5px 0;
  293. }
  294. </style>