TongzhouTrafficMap.vue 18 KB

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