TongzhouTrafficMap.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <template>
  2. <div class="map-wrapper">
  3. <div ref="mapContainer" class="map-container"></div>
  4. <div class="map-legend" :style="privateStyle.legend">
  5. <div class="legend-title">图例</div>
  6. <div class="legend-list">
  7. <div class="legend-item all-select" @click="toggleAll">
  8. <div class="legend-dot"
  9. :style="{ backgroundColor: isAllSelected ? '#fff' : 'transparent', border: '1px solid #fff' }"></div>
  10. <div class="legend-label" style="font-weight: bold;">全选</div>
  11. </div>
  12. <div v-for="item in legendConfig" class="legend-item" @click="toggleRouteVisible(item.name)" :key="item.name"
  13. :class="{ 'is-inactive': !activeLegends.includes(item.name) }">
  14. <div class="legend-dot"
  15. :style="{ backgroundColor: ['离线', '降级', '故障'].includes(item.name) ? 'transparent' : item.color }"
  16. :class="{ 'special-route': ['干线协调', '勤务路线'].includes(item.name), 'is-status-wrapper': ['离线', '降级', '故障'].includes(item.name) }">
  17. <span v-if="!['离线', '降级', '故障'].includes(item.name)">
  18. {{ item.name.charAt(0) }}
  19. </span>
  20. <img v-else
  21. :src="require(`@/assets/images/icon_${item.name === '离线' ? 'lixian' : item.name === '降级' ? 'jiangji' : 'guzhang'}.png`)"
  22. class="status-icon" />
  23. </div>
  24. <div class="legend-label">{{ item.name }}</div>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import AMapLoader from '@amap/amap-jsapi-loader';
  32. export default {
  33. name: "TrafficMap",
  34. props: {
  35. amapKey: { type: String, default: '您的Key' },
  36. securityJsCode: { type: String, default: '您的安全密钥' }
  37. },
  38. data() {
  39. return {
  40. AMap: null,
  41. map: null,
  42. infoWindow: null,
  43. routeGroups: {},
  44. polylines: [],
  45. privateStyle: {
  46. legend: {}
  47. },
  48. activeLegends: ["中心计划", "干线协调", "勤务路线", "定周期控制", "感应控制", "自适应控制", "手动控制", "特殊控制", "离线", "降级", "故障"],
  49. legendConfig: [
  50. // 横向主干道 (由北向南)
  51. { name: "中心计划", start: [116.6350, 39.9105], end: [116.6910, 39.9115], color: "#004CDE" }, // 新华大街全线
  52. { name: "干线协调", start: [116.6355, 39.9025], end: [116.6915, 39.9035], color: "#13C373" }, // 玉带河大街全线
  53. { name: "勤务路线", start: [116.6360, 39.8945], end: [116.6920, 39.8955], color: "#BC301D" }, // 运河西大街全线
  54. { name: "定周期控制", start: [116.6521, 39.9200], end: [116.6531, 39.8800], color: "#3296FA" }, // 车站路
  55. { name: "感应控制", start: [116.6611, 39.9205], end: [116.6615, 39.8805], color: "#FF864C" }, // 新华南路
  56. { name: "自适应控制", start: [116.6711, 39.9210], end: [116.6720, 39.8810], color: "#9F6EFE" }, // 东关大道
  57. { name: "手动控制", start: [116.6300, 39.9150], end: [116.6310, 39.8850], color: "#EB9F36" }, // 北苑南路
  58. { name: "特殊控制", start: [116.6820, 39.9215], end: [116.6825, 39.8815], color: "#A26218" }, // 临河里路
  59. { name: "离线", start: [116.6415, 39.9235], end: [116.6850, 39.9240], color: "#7A7A7A" }, // 北关大街-潞苑
  60. { name: "降级", start: [116.6365, 39.8850], end: [116.6800, 39.8860], color: "#D9C13B" }, // 万盛南街段
  61. { name: "故障", start: [116.6950, 39.9150], end: [116.6955, 39.885], color: "#FF3938" } // 潞通大街
  62. ]
  63. };
  64. },
  65. mounted() {
  66. this.initAMap();
  67. // 自定义首页地图搜索和图例位置样式
  68. if (this.$route.path === '/home') {
  69. this.privateStyle.legend = { right: "25%" };
  70. }
  71. },
  72. beforeDestroy() {
  73. if (this.infoWindow) this.infoWindow.close();
  74. // 1. 清理普通的 polylines 数组
  75. this.polylines.forEach(p => {
  76. if (p && typeof p.setMap === 'function') p.setMap(null);
  77. });
  78. // 2. 核心修改:清理 routeGroups
  79. Object.values(this.routeGroups).forEach(g => {
  80. if (!g) return;
  81. if (Array.isArray(g)) {
  82. // 如果是数组(当前的逻辑),遍历每个成员销毁
  83. g.forEach(overlay => {
  84. if (overlay && typeof overlay.setMap === 'function') {
  85. overlay.setMap(null);
  86. }
  87. });
  88. } else if (typeof g.setMap === 'function') {
  89. // 如果是旧版的 OverlayGroup 或单个覆盖物
  90. g.setMap(null);
  91. }
  92. });
  93. if (this.map) {
  94. this.map.destroy();
  95. }
  96. },
  97. computed: {
  98. // 判断是否所有图例都在激活列表中
  99. isAllSelected() {
  100. return this.activeLegends.length === this.legendConfig.length;
  101. }
  102. },
  103. methods: {
  104. // 修改后的 initAMap
  105. async initAMap() {
  106. window._AMapSecurityConfig = { securityJsCode: this.securityJsCode };
  107. try {
  108. this.AMap = await AMapLoader.load({
  109. key: this.amapKey,
  110. version: "2.0",
  111. plugins: ['AMap.Driving', 'AMap.GeometryUtil']
  112. });
  113. this.map = new this.AMap.Map(this.$refs.mapContainer, {
  114. zoom: 13.5,
  115. mapStyle: "amap://styles/darkblue",
  116. center: [116.663, 39.905],
  117. });
  118. this.map.on('complete', () => this.drawStaticRoutes());
  119. } catch (err) { console.error('地图初始化失败', err); }
  120. },
  121. drawStaticRoutes() {
  122. const AMap = this.AMap;
  123. this.legendConfig.forEach((config, index) => {
  124. setTimeout(() => {
  125. const driving = new AMap.Driving({
  126. map: null,
  127. hideMarkers: true,
  128. autoFitView: false
  129. });
  130. driving.search(config.start, config.end, (status, result) => {
  131. let markers = [];
  132. let path = [];
  133. if (status === 'complete' && result.routes[0]) {
  134. const route = result.routes[0];
  135. route.steps.forEach(step => { path = path.concat(step.path); });
  136. } else {
  137. path = [config.start, config.end];
  138. }
  139. let polyline = null;
  140. const needRouteLine = ["干线协调", "勤务路线"].includes(config.name);
  141. if (needRouteLine) {
  142. polyline = new AMap.Polyline({
  143. path: path,
  144. strokeColor: config.color,
  145. strokeWeight: 8,
  146. strokeOpacity: 0.8,
  147. showDir: false,
  148. lineJoin: 'round',
  149. zIndex: 15,
  150. map: null
  151. });
  152. }
  153. const points = [];
  154. const step = 10; // 步长越大,点越稀疏
  155. for (let i = 0; i < path.length; i += step) {
  156. points.push(path[i]);
  157. }
  158. // 确保终点也被加上
  159. if ((path.length - 1) % step !== 0) {
  160. points.push(path[path.length - 1]);
  161. }
  162. points.forEach((pos, idx) => {
  163. // 临时逻辑,有真实接口后可以删除
  164. const posMap = {
  165. 8: 'pos1',
  166. 9: 'pos2',
  167. 10: 'pos3'
  168. };
  169. if (idx === 0 && posMap[index]) {
  170. localStorage.setItem(posMap[index], pos);
  171. }
  172. // 临时逻辑,有真实接口后可以删除
  173. markers.push(this.createTrafficLightMarker(pos, config));
  174. });
  175. // 3. 【关键修改】:overlays 数组只存放 markers,不再存放 polyline
  176. const overlays = [...markers, polyline].filter(Boolean);
  177. this.routeGroups[config.name] = overlays;
  178. if (this.activeLegends.includes(config.name)) {
  179. this.map.add(overlays);
  180. }
  181. });
  182. }, index * 250);
  183. });
  184. },
  185. createTrafficLightMarker(position, config) {
  186. // 根据业务需求:离线、降级、故障需要闪烁,其他保持静止
  187. const needsFlash = ["离线", "降级", "故障"].includes(config.name);
  188. const displayStatus = needsFlash ? config.name : "正常运行";
  189. const lng = Number(position[0] || position.lng);
  190. const lat = Number(position[1] || position.lat);
  191. const marker = new this.AMap.Marker({
  192. position: [lng, lat],
  193. zIndex: 100,
  194. content: `
  195. <div class="pure-light-node ${needsFlash ? 'breathe' : ''}"
  196. style="background: ${config.color}; box-shadow: 0 0 15px ${config.color}; font-size: 12px; display: flex; justify-content: center; align-items: center; color: #fff; padding: 8px;">
  197. <span>${config.name.charAt(0)}</span>
  198. </div>
  199. `,
  200. offset: new this.AMap.Pixel(-10, -10),
  201. extData: {
  202. ...config,
  203. position: [lng, lat],
  204. statusColor: config.color, // 统一弹窗小圆点颜色
  205. statusLabel: displayStatus, // 统一弹窗状态文字
  206. road: '北京路与南京路',
  207. time: '2026.1.23.12:00'
  208. }
  209. });
  210. marker.on('click', (e) => this.openLightInfo(e.target.getExtData(), e.lnglat));
  211. return marker;
  212. },
  213. openLightInfo(data, position) {
  214. const content = `
  215. <div class="custom-info-card">
  216. <div class="close-btn" onclick="window.closeMapInfoWindow()">✕</div>
  217. <div class="card-header">
  218. <div class="status-dot" style="background: ${data.statusColor}">
  219. <span>${data.name.charAt(0)}</span>
  220. </div>
  221. <span class="status-text">${data.statusLabel}</span>
  222. </div>
  223. <div class="card-body">
  224. <div class="info-line">
  225. <span class="label">路口:</span>
  226. <span class="value">${data.road}</span>
  227. </div>
  228. <div class="info-line">
  229. <span class="label">发生时间:</span>
  230. <span class="value digital">${data.time}</span>
  231. </div>
  232. </div>
  233. </div>
  234. `;
  235. // 定义全局关闭方法(因为 isCustom:true 下 Vue 事件会失效)
  236. window.closeMapInfoWindow = () => {
  237. if (this.infoWindow) this.infoWindow.close();
  238. };
  239. if (!this.infoWindow) {
  240. this.infoWindow = new this.AMap.InfoWindow({
  241. isCustom: true,
  242. offset: new this.AMap.Pixel(0, -20)
  243. });
  244. }
  245. this.infoWindow.setContent(content);
  246. this.infoWindow.open(this.map, position);
  247. },
  248. // 全选/全不选逻辑修正版
  249. toggleAll() {
  250. const targetState = !this.isAllSelected; // 获取点击后的目标状态(true为全选,false为全不选)
  251. if (targetState) {
  252. // --- 情况 1:执行“全选” ---
  253. // 1. 更新激活列表
  254. this.activeLegends = this.legendConfig.map(item => item.name);
  255. // 2. 遍历所有已经生成的路线数组,将其全部添加到地图上
  256. Object.keys(this.routeGroups).forEach(name => {
  257. const overlays = this.routeGroups[name]; // 此时 routeGroups 存储的是数组
  258. if (overlays && overlays.length > 0) {
  259. this.map.add(overlays);
  260. }
  261. });
  262. } else {
  263. // --- 情况 2:执行“全不选” ---
  264. // 1. 清空激活列表
  265. this.activeLegends = [];
  266. // 2. 遍历所有路线数组,从地图上移除
  267. Object.keys(this.routeGroups).forEach(name => {
  268. const overlays = this.routeGroups[name];
  269. if (overlays && overlays.length > 0) {
  270. this.map.remove(overlays);
  271. }
  272. });
  273. // 3. 关闭当前可能打开的弹窗
  274. if (this.infoWindow) this.infoWindow.close();
  275. }
  276. },
  277. toggleRouteVisible(name) {
  278. const overlays = this.routeGroups[name] || []; // 获取的是数组
  279. const index = this.activeLegends.indexOf(name);
  280. if (index > -1) {
  281. this.activeLegends.splice(index, 1);
  282. this.map.remove(overlays); // 改用 remove
  283. if (this.infoWindow) this.infoWindow.close();
  284. } else {
  285. this.activeLegends.push(name);
  286. this.map.add(overlays); // 改用 add
  287. }
  288. },
  289. // 其他组件点击定位到地图指定的点
  290. focusByLocation(targetPos) {
  291. if (!targetPos || targetPos.length !== 2) return;
  292. let foundMarker = null;
  293. // 1. 遍历所有路线组
  294. Object.values(this.routeGroups).forEach(group => {
  295. // 2. 在组内寻找 Marker
  296. const marker = group.find(item => {
  297. if (!(item instanceof this.AMap.Marker)) return false;
  298. const pos = item.getExtData().position;
  299. // 3. 坐标比对(考虑到浮点数精度,建议使用 AMap 自带的几何工具或简单比对)
  300. return pos[0] === targetPos[0] && pos[1] === targetPos[1];
  301. });
  302. if (marker) foundMarker = marker;
  303. });
  304. if (foundMarker) {
  305. // 4. 定位并打开弹窗
  306. const finalPos = foundMarker.getPosition();
  307. this.map.setZoomAndCenter(17, finalPos, false, 500); // 17级视角,平滑移动
  308. setTimeout(() => {
  309. this.openLightInfo(foundMarker.getExtData(), finalPos);
  310. }, 600);
  311. } else {
  312. console.warn("未在地图上找到该坐标对应的点位:", targetPos);
  313. }
  314. }
  315. }
  316. };
  317. </script>
  318. <style scoped>
  319. .map-wrapper {
  320. width: 100%;
  321. height: 100vh;
  322. position: relative;
  323. background: #010813;
  324. }
  325. .map-container {
  326. width: 100%;
  327. height: 100%;
  328. }
  329. ::v-deep .pure-light-node {
  330. width: 16px;
  331. height: 16px;
  332. border-radius: 50%;
  333. border: 2px solid rgba(255, 255, 255, 0.8);
  334. cursor: pointer;
  335. transition: all 0.3s;
  336. }
  337. ::v-deep .pure-light-node.breathe {
  338. animation: light-breathe 2s infinite ease-in-out;
  339. }
  340. ::v-deep .pure-light-node span {
  341. display: flex;
  342. transform: scale(0.75);
  343. align-items: center;
  344. }
  345. ::v-deep .pure-light-node:hover {
  346. transform: scale(1.4);
  347. filter: brightness(1.2);
  348. }
  349. @keyframes light-breathe {
  350. 0%,
  351. 100% {
  352. opacity: 0.7;
  353. transform: scale(1);
  354. }
  355. 50% {
  356. opacity: 1;
  357. transform: scale(1.15);
  358. }
  359. }
  360. ::v-deep .close-btn {
  361. position: absolute;
  362. top: 10px;
  363. right: 12px;
  364. color: #8da6c7;
  365. cursor: pointer;
  366. font-size: 16px;
  367. transition: color 0.3s;
  368. line-height: 1;
  369. z-index: 10;
  370. }
  371. ::v-deep .close-btn:hover {
  372. color: #ffffff;
  373. }
  374. ::v-deep .custom-info-card {
  375. position: relative;
  376. background: rgba(10, 15, 24, 0.95);
  377. border-radius: 10px;
  378. padding: 12px 16px;
  379. min-width: 200px;
  380. border: 1px solid rgba(255, 255, 255, 0.1);
  381. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  382. color: #fff;
  383. }
  384. ::v-deep .card-header {
  385. display: flex;
  386. align-items: center;
  387. margin-bottom: 10px;
  388. }
  389. ::v-deep .status-dot {
  390. width: 18px;
  391. height: 18px;
  392. border-radius: 50%;
  393. display: flex;
  394. justify-content: center;
  395. align-items: center;
  396. margin-right: 8px;
  397. font-size: 12px;
  398. padding: 12px;
  399. box-sizing: border-box;
  400. }
  401. ::v-deep .status-dot span {
  402. transform: scale(0.75);
  403. }
  404. ::v-deep .status-text {
  405. font-size: 15px;
  406. font-weight: bold;
  407. }
  408. ::v-deep .info-line {
  409. display: flex;
  410. margin-bottom: 6px;
  411. font-size: 13px;
  412. align-items: center;
  413. }
  414. ::v-deep .label {
  415. color: #8da6c7;
  416. white-space: nowrap;
  417. }
  418. ::v-deep .value {
  419. color: #ffffff;
  420. }
  421. ::v-deep .digital {
  422. font-family: 'Consolas', monospace;
  423. }
  424. .map-legend {
  425. position: absolute;
  426. bottom: 30px;
  427. right: 40px;
  428. background: rgba(5, 22, 45, 0.9);
  429. border: 1px solid #1e4d8e;
  430. padding: 15px;
  431. border-radius: 6px;
  432. z-index: 100;
  433. }
  434. .legend-title {
  435. color: #fff;
  436. font-size: 14px;
  437. margin-bottom: 12px;
  438. border-bottom: 1px solid #1e4d8e;
  439. padding-bottom: 8px;
  440. }
  441. .legend-item {
  442. display: flex;
  443. align-items: center;
  444. margin-bottom: 10px;
  445. cursor: pointer;
  446. transition: 0.3s;
  447. }
  448. .legend-item.is-inactive {
  449. opacity: 0.2;
  450. filter: grayscale(1);
  451. }
  452. .legend-dot {
  453. margin-right: 10px;
  454. display: flex;
  455. justify-content: center;
  456. align-items: center;
  457. }
  458. .legend-dot:not(.is-status-wrapper) {
  459. width: 20px;
  460. height: 20px;
  461. border-radius: 50%;
  462. }
  463. .legend-dot span {
  464. display: inline-block;
  465. width: 20px;
  466. height: 20px;
  467. font-size: 12px;
  468. color: #FFF;
  469. text-align: center;
  470. transform: scale(0.75);
  471. line-height: 20px;
  472. }
  473. .legend-dot.special-route {
  474. position: relative;
  475. overflow: visible !important;
  476. z-index: 1;
  477. border: none !important;
  478. border-radius: 50%;
  479. }
  480. .legend-dot.special-route span {
  481. position: relative;
  482. z-index: 3;
  483. font-weight: bold;
  484. }
  485. .legend-dot.special-route::after {
  486. content: "";
  487. position: absolute;
  488. top: 50%;
  489. left: 50%;
  490. width: 150%;
  491. height: 3px;
  492. background-color: inherit;
  493. opacity: 0.8;
  494. transform: translate(-50%, -50%) rotate(-45deg);
  495. pointer-events: none;
  496. z-index: 0;
  497. }
  498. .legend-dot.special-route::before {
  499. content: "";
  500. position: absolute;
  501. top: 0;
  502. left: 0;
  503. width: 100%;
  504. height: 100%;
  505. border-radius: 50%;
  506. z-index: 2;
  507. background-color: inherit;
  508. opacity: 1;
  509. }
  510. .legend-label {
  511. flex: 1;
  512. color: #d0d9e2;
  513. font-size: 13px;
  514. }
  515. .legend-status {
  516. font-size: 11px;
  517. color: #5b7da8;
  518. }
  519. .all-select {
  520. border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  521. padding-bottom: 12px;
  522. margin-bottom: 12px !important;
  523. }
  524. .all-select .legend-dot {
  525. border-radius: 2px;
  526. transition: all 0.3s;
  527. width: 10px;
  528. height: 10px;
  529. }
  530. .legend-dot.is-status-wrapper {
  531. width: 28px;
  532. height: 28px;
  533. background-color: transparent !important;
  534. box-shadow: none !important;
  535. border: none !important;
  536. margin-right: 0;
  537. transform: translateX(-15%);
  538. }
  539. .status-icon {
  540. width: 100%;
  541. height: 100%;
  542. object-fit: contain;
  543. display: block;
  544. }
  545. </style>