TongzhouTrafficMap.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <div class="map-wrapper">
  3. <div ref="mapContainer" class="map-container"></div>
  4. <div class="map-header" v-if="initialized">
  5. <div class="search-form">
  6. <input type="text" v-model="searchQuery" placeholder="请输入路段或设备名称" class="search-input"
  7. @keyup.enter="handleSearch" />
  8. <button class="search-btn" @click="handleSearch">查询</button>
  9. </div>
  10. <div class="action-box" @click="toggleAll">全选 ▾</div>
  11. </div>
  12. <div class="map-legend" v-if="initialized">
  13. <div class="legend-title">图例</div>
  14. <div class="legend-list">
  15. <div v-for="item in legendConfig" :key="item.type" class="legend-item"
  16. :class="{ 'is-hidden': !activeLegends.includes(item.type) }" @click="handleLegendClick(item.type)">
  17. <span class="legend-dot" :style="{ backgroundColor: item.color }"></span>
  18. <span class="legend-label">{{ item.label }}</span>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import AMapLoader from '@amap/amap-jsapi-loader';
  26. export default {
  27. name: "TrafficMap",
  28. props: {
  29. amapKey: { type: String, default: '您的Key' },
  30. securityJsCode: { type: String, default: '您的安全密钥' }
  31. },
  32. data() {
  33. return {
  34. AMap: null,
  35. map: null,
  36. infoWindow: null,
  37. initialized: false,
  38. searchQuery: '', // 搜索内容绑定
  39. activeLegends: [],
  40. legendConfig: [
  41. { type: 'center_plan', label: '中心计划', color: '#32c5ff' },
  42. { type: 'trunk_coord', label: '干线协调', color: '#3ee68d' },
  43. { type: 'service_route', label: '勤务路线', color: '#ffcc33' },
  44. { type: 'periodic', label: '定周期控制', color: '#00ccff' },
  45. { type: 'induction', label: '感应控制', color: '#7ed3b2' },
  46. { type: 'adaptive', label: '自适应控制', color: '#8ca1ff' },
  47. { type: 'manual', label: '手动控制', color: '#cc8d66' },
  48. { type: 'special', label: '特殊控制', color: '#ffb33b' },
  49. { type: 'offline', label: '离线', color: '#6d7791' },
  50. { type: 'degraded', label: '降级', color: '#c4a737' },
  51. { type: 'fault', label: '故障', color: '#ff4d4f' }
  52. ],
  53. overlayGroups: {}
  54. };
  55. },
  56. mounted() {
  57. this.initAMap();
  58. },
  59. beforeDestroy() {
  60. if (this.map) {
  61. this.map.destroy();
  62. }
  63. },
  64. methods: {
  65. async initAMap() {
  66. window._AMapSecurityConfig = { securityJsCode: this.securityJsCode };
  67. try {
  68. const AMap = await AMapLoader.load({
  69. key: this.amapKey,
  70. version: "2.0",
  71. plugins: ['AMap.InfoWindow', 'AMap.Polyline', 'AMap.Marker']
  72. });
  73. this.AMap = AMap;
  74. this.map = new AMap.Map(this.$refs.mapContainer, {
  75. zoom: 13,
  76. center: [116.6612, 39.9125],
  77. mapStyle: "amap://styles/darkblue",
  78. viewMode: "3D"
  79. });
  80. this.infoWindow = new AMap.InfoWindow({
  81. isCustom: true,
  82. offset: new AMap.Pixel(0, -15)
  83. });
  84. this.map.on("complete", () => {
  85. this.initialized = true;
  86. this.activeLegends = this.legendConfig.map(l => l.type);
  87. this.drawTrafficScene();
  88. });
  89. this.map.on('click', () => this.infoWindow && this.infoWindow.close());
  90. } catch (e) {
  91. console.error("地图加载失败:", e);
  92. }
  93. },
  94. // 搜索查询逻辑
  95. handleSearch() {
  96. if (!this.searchQuery.trim()) {
  97. alert("请输入查询内容");
  98. return;
  99. }
  100. console.log("正在执行搜索:", this.searchQuery);
  101. // 这里可以扩展具体的搜索逻辑,比如搜索 POI 或在已有 overlayGroups 中高亮匹配项
  102. alert(`已提交查询:${this.searchQuery}`);
  103. },
  104. drawTrafficScene() {
  105. const roads = [
  106. { type: 'center_plan', name: '新华南北路 - 中心控制段', path: [[116.665, 39.940], [116.665, 39.885]], hasDots: true },
  107. { type: 'trunk_coord', name: '通胡大街 - 干线协调(北)', path: [[116.620, 39.930], [116.650, 39.920], [116.700, 39.930]] },
  108. { type: 'trunk_coord', name: '运河东大街 - 干线协调(南)', path: [[116.615, 39.900], [116.660, 39.910], [116.710, 39.900]] },
  109. { type: 'service_route', name: '新华东街 - 勤务专用线', path: [[116.625, 39.905], [116.700, 39.905]] },
  110. { type: 'fault', name: '路县故城周边 - 设备异常', path: [[116.685, 39.920], [116.690, 39.905]] }
  111. ];
  112. roads.forEach(road => this.renderRoadWithStyle(road));
  113. },
  114. renderRoadWithStyle(road) {
  115. const config = this.legendConfig.find(l => l.type === road.type);
  116. const group = [];
  117. const glow = new this.AMap.Polyline({
  118. path: road.path, strokeColor: config.color, strokeOpacity: 0.15, strokeWeight: 20, map: this.map
  119. });
  120. const line = new this.AMap.Polyline({
  121. path: road.path, strokeColor: config.color, strokeWeight: 6, map: this.map
  122. });
  123. [glow, line].forEach(item => {
  124. item.on('click', (e) => this.openDetailWindow(road, e.lnglat));
  125. group.push(item);
  126. });
  127. if (road.hasDots) {
  128. const dots = this.calculatePathDots(road.path, 12);
  129. dots.forEach((pos, index) => {
  130. const marker = new this.AMap.Marker({
  131. position: pos,
  132. content: `<div class="pulse-dot"></div>`,
  133. offset: new this.AMap.Pixel(-6, -6),
  134. map: this.map
  135. });
  136. marker.on('click', (e) => this.openDetailWindow({ ...road, name: `${road.name}-监测点${index + 1}` }, e.lnglat));
  137. group.push(marker);
  138. });
  139. }
  140. this.overlayGroups[road.type] = (this.overlayGroups[road.type] || []).concat(group);
  141. },
  142. calculatePathDots(path, count) {
  143. const p1 = path[0], p2 = path[1];
  144. const dots = [];
  145. for (let i = 1; i < count; i++) {
  146. dots.push([p1[0] + (p2[0] - p1[0]) * (i / count), p1[1] + (p2[1] - p1[1]) * (i / count)]);
  147. }
  148. return dots;
  149. },
  150. openDetailWindow(data, lnglat) {
  151. const config = this.legendConfig.find(l => l.type === data.type);
  152. const content = `
  153. <div class="custom-info-card">
  154. <div class="card-header" style="background: ${config.color}22; border-left: 4px solid ${config.color}">
  155. <span class="title">${data.name}</span>
  156. </div>
  157. <div class="card-body">
  158. <div class="info-row"><span class="label">管控类型:</span><span class="value" style="color:${config.color}">${config.label}</span></div>
  159. <div class="info-row"><span class="label">当前状态:</span><span class="value" style="color:#3ee68d">运行中</span></div>
  160. </div>
  161. </div>
  162. `;
  163. this.infoWindow.setContent(content);
  164. this.infoWindow.open(this.map, lnglat);
  165. },
  166. handleLegendClick(type) {
  167. const isVisible = this.activeLegends.includes(type);
  168. this.activeLegends = isVisible ? this.activeLegends.filter(t => t !== type) : [...this.activeLegends, type];
  169. const group = this.overlayGroups[type];
  170. if (group) group.forEach(o => isVisible ? o.hide() : o.show());
  171. if (isVisible) this.infoWindow.close();
  172. },
  173. toggleAll() {
  174. const allTypes = this.legendConfig.map(l => l.type);
  175. const isShowingAll = this.activeLegends.length === allTypes.length;
  176. allTypes.forEach(type => {
  177. const group = this.overlayGroups[type];
  178. if (group) group.forEach(o => isShowingAll ? o.hide() : o.show());
  179. });
  180. this.activeLegends = isShowingAll ? [] : allTypes;
  181. this.infoWindow.close();
  182. }
  183. }
  184. };
  185. </script>
  186. <style scoped>
  187. .map-wrapper {
  188. position: relative;
  189. width: 100%;
  190. height: 100%;
  191. min-height: 500px;
  192. background: #010813;
  193. overflow: hidden;
  194. }
  195. .map-container {
  196. width: 100%;
  197. height: 100%;
  198. }
  199. /* 头部样式:增加了搜索表单布局 */
  200. .map-header {
  201. position: absolute;
  202. top: 20px;
  203. left: 20px;
  204. right: 20px;
  205. display: flex;
  206. justify-content: space-between;
  207. align-items: center;
  208. z-index: 10;
  209. }
  210. /* 搜索表单容器 */
  211. .search-form {
  212. display: flex;
  213. background: rgba(13, 35, 67, 0.9);
  214. border: 1px solid #1a4a8d;
  215. border-radius: 4px;
  216. overflow: hidden;
  217. }
  218. .search-input {
  219. background: transparent;
  220. border: none;
  221. padding: 10px 15px;
  222. color: #fff;
  223. width: 240px;
  224. outline: none;
  225. font-size: 13px;
  226. }
  227. .search-input::placeholder {
  228. color: #5b7da8;
  229. }
  230. .search-btn {
  231. background: #1a4a8d;
  232. border: none;
  233. color: #fff;
  234. padding: 0 20px;
  235. cursor: pointer;
  236. font-size: 13px;
  237. transition: background 0.2s;
  238. }
  239. .search-btn:hover {
  240. background: #2660b3;
  241. }
  242. .action-box {
  243. background: rgba(13, 35, 67, 0.9);
  244. border: 1px solid #1a4a8d;
  245. padding: 10px 18px;
  246. color: #fff;
  247. border-radius: 4px;
  248. font-size: 13px;
  249. cursor: pointer;
  250. }
  251. /* 图例与其它样式保持不变 */
  252. .map-legend {
  253. position: absolute;
  254. right: 25px;
  255. bottom: 40px;
  256. width: 170px;
  257. background: rgba(8, 20, 36, 0.95);
  258. border: 1px solid rgba(38, 74, 124, 0.8);
  259. border-radius: 12px;
  260. padding: 18px;
  261. z-index: 100;
  262. box-shadow: 0 0 25px rgba(0, 0, 0, 0.6);
  263. }
  264. .legend-title {
  265. color: #fff;
  266. font-size: 18px;
  267. margin-bottom: 18px;
  268. font-weight: bold;
  269. }
  270. .legend-item {
  271. display: flex;
  272. align-items: center;
  273. margin-bottom: 14px;
  274. cursor: pointer;
  275. transition: 0.2s;
  276. }
  277. .legend-item.is-hidden {
  278. opacity: 0.2;
  279. filter: grayscale(1);
  280. }
  281. .legend-dot {
  282. width: 12px;
  283. height: 12px;
  284. border-radius: 3px;
  285. margin-right: 12px;
  286. }
  287. .legend-label {
  288. color: #d0d9e2;
  289. font-size: 14px;
  290. }
  291. ::v-deep .pulse-dot {
  292. width: 12px;
  293. height: 12px;
  294. background: #fff;
  295. border-radius: 50%;
  296. box-shadow: 0 0 10px #fff, 0 0 20px rgba(50, 197, 255, 0.5);
  297. }
  298. </style>
  299. <style>
  300. /* 弹窗样式 */
  301. .custom-info-card {
  302. background: rgba(5, 22, 45, 0.98);
  303. border: 1px solid #1e4d8e;
  304. border-radius: 6px;
  305. width: 240px;
  306. color: #fff;
  307. overflow: hidden;
  308. }
  309. .card-header {
  310. padding: 12px 15px;
  311. font-size: 14px;
  312. font-weight: bold;
  313. }
  314. .card-body {
  315. padding: 15px;
  316. font-size: 13px;
  317. }
  318. .info-row {
  319. margin-bottom: 8px;
  320. display: flex;
  321. }
  322. .info-row .label {
  323. color: #8da6c7;
  324. width: 70px;
  325. }
  326. .amap-info-content {
  327. background: transparent !important;
  328. border: none !important;
  329. padding: 0 !important;
  330. }
  331. .amap-info-sharp {
  332. display: none !important;
  333. }
  334. </style>