Просмотр исходного кода

feat: 优化地图模式切换实现逻辑,提升性能

sequoia tungfang 1 месяц назад
Родитель
Сommit
df3931ee62
1 измененных файлов с 17 добавлено и 53 удалено
  1. 17 53
      src/components/TongzhouTrafficMap.vue

+ 17 - 53
src/components/TongzhouTrafficMap.vue

@@ -86,7 +86,7 @@ export default {
     mode: {
       handler(newMode) {
         this.updateMapByMode();
-        this.reDrawMap();
+        this.updateMapDisplay();
       },
       immediate: false
     }
@@ -147,39 +147,24 @@ export default {
           this.activeLegends = ["中心计划", "干线协调", "勤务路线", "定周期控制", "感应控制", "自适应控制", "手动控制", "特殊控制", "离线", "降级", "故障"];
       }
     },
-    reDrawMap() {
-      // 清理旧的路线和标记
+
+    updateMapDisplay() {
+      // 关闭信息窗口
       if (this.infoWindow) this.infoWindow.close();
       
-      // 清理普通的 polylines 数组
-      this.polylines.forEach(p => {
-        if (p && typeof p.setMap === 'function') p.setMap(null);
-      });
-      
-      // 清理 routeGroups
-      Object.values(this.routeGroups).forEach(g => {
-        if (!g) return;
-        
-        if (Array.isArray(g)) {
-          // 如果是数组,遍历每个成员销毁
-          g.forEach(overlay => {
-            if (overlay && typeof overlay.setMap === 'function') {
-              overlay.setMap(null);
-            }
-          });
-        } else if (typeof g.setMap === 'function') {
-          // 如果是旧版的 OverlayGroup 或单个覆盖物
-          g.setMap(null);
+      // 根据activeLegends数组更新地图上路线和标记的显示/隐藏状态
+      Object.keys(this.routeGroups).forEach(name => {
+        const overlays = this.routeGroups[name];
+        if (overlays && overlays.length > 0) {
+          if (this.activeLegends.includes(name)) {
+            // 如果在激活列表中,添加到地图
+            this.map.add(overlays);
+          } else {
+            // 如果不在激活列表中,从地图中移除
+            this.map.remove(overlays);
+          }
         }
       });
-      
-      // 清空 routeGroups
-      this.routeGroups = {};
-      
-      // 重新绘制路线
-      if (this.map) {
-        this.drawStaticRoutes();
-      }
     },
     // 修改后的 initAMap
     async initAMap() {
@@ -205,29 +190,8 @@ export default {
       const AMap = this.AMap;
 
       this.legendConfig.forEach((config, index) => {
-        // 根据mode过滤路线
-        if (this.mode) {
-          switch (this.mode) {
-            case '路口':
-              // 路口模式:去掉'干线协调'和'勤务路线'
-              if (['干线协调', '勤务路线'].includes(config.name)) {
-                return;
-              }
-              break;
-            case '干线':
-              // 干线模式:只处理'干线协调'
-              if (config.name !== '干线协调') {
-                return;
-              }
-              break;
-            case '特勤':
-              // 特勤模式:只处理'勤务路线'
-              if (config.name !== '勤务路线') {
-                return;
-              }
-              break;
-          }
-        }
+        // 绘制所有路线,不根据mode过滤
+        // 后续通过updateMapDisplay方法控制显示/隐藏
         
         setTimeout(() => {
           const driving = new AMap.Driving({