From 3b79ee564d4c77189484711ece4dc80786f79080 Mon Sep 17 00:00:00 2001
From: 徐旺旺 <11530253@qq.com>
Date: 星期一, 17 五月 2021 14:34:20 +0800
Subject: [PATCH] 定位、高亮重封装

---
 src/components/LayerController/service/LayerFactory.js |  185 +++++++++++++++++++---------------------------
 1 files changed, 76 insertions(+), 109 deletions(-)

diff --git a/src/components/LayerController/service/LayerFactory.js b/src/components/LayerController/service/LayerFactory.js
index adb3d9f..68d8d64 100644
--- a/src/components/LayerController/service/LayerFactory.js
+++ b/src/components/LayerController/service/LayerFactory.js
@@ -1,16 +1,32 @@
 import WfsLayerService from './WfsLayerService'
+import { logicMapper, SERVICE_TYPE } from '../../../conf/Constants'
 import WmsLayerService from './WmsLayerService'
-import { clone } from '../../../utils/utils'
-import { logicMapper } from '../../../conf/Constants'
+
+/**
+ *  init 鍙垵濮嬪寲涓�娆�
+ *  start 姣忔璋冪敤鍥惧眰鏄剧ずshow()鏃讹紝閮戒細璋冪敤
+ *  destory 姣忔璋冪敤鍥惧眰闅愯棌hide()鏃讹紝閮戒細璋冪敤
+ *
+ */
 class LayerFactory {
   constructor (options) {
     this.L = options.L
     this.map = window.map
     this.layers = {}
     this.layersLogic = {}
+    this.minZoomLayers = {}
+    this.wmsLayers = []
+    this.wmsLayerService = null
   }
 
   init (layerConfig) {
+    // wms鏈嶅姟鍙渶瑕佸垵濮嬪寲涓�娆�
+    this.wmsLayerService = new WmsLayerService()
+    this.wmsLayerService.init()
+    this.initConfig(layerConfig)
+  }
+
+  initConfig (layerConfig) {
     // 1. 閬嶅巻layer config
     if (layerConfig) {
       for (var i = 0; i < layerConfig.length; i++) {
@@ -18,20 +34,37 @@
         var layers = config.layers
         var childLayer = config.childLayer
         var checked = config.checked
-        layers && this.init(config.layers)
-        childLayer && this.init(config.childLayer)
+        /* if (config.groupName) {
+          const wmsGroupLayerService = new WmsLayerGroupService(config)
+          wmsGroupLayerService.init()
+          continue
+        } */
+        layers && this.initConfig(config.layers)
+        childLayer && this.initConfig(config.childLayer)
 
+        this.initMinZoom(config)
         this.loadLogic(config)
-        this.toggleZoomByConfig(config)
         checked && this.show(config)
       }
     }
   }
 
+  initMinZoom (config) {
+    const minZoom = parseInt(config.minZoom)
+    if (minZoom) {
+      var configs = this.minZoomLayers[minZoom]
+      if (configs) {
+        configs[configs.length] = config
+      } else {
+        configs = [config]
+      }
+      this.minZoomLayers[minZoom] = configs
+    }
+  }
+
   loadLogic (config) {
     var code = config.code
-    var wfs = config.wfs
-    var wms = config.wms
+    var type = config.type
 
     const file = logicMapper[code]
     var logic = this.layersLogic[code]
@@ -39,23 +72,30 @@
       if (file) {
         var BusiLayer = require('../logic/' + file)
         logic = new BusiLayer()
-      } else if (wfs) {
+      } else if (type === SERVICE_TYPE.WFS) {
         logic = new WfsLayerService(config)
-      } else if (wms) {
-        logic = new WmsLayerService(config)
       }
     }
     this.layersLogic[code] = logic
     return logic
   }
 
+  /**
+   * 1. 鍏堣皟鐢ㄥ鐞嗛�昏緫鐨� initLayer 锛屽鏋滄病鏈� 灏卞垱寤轰竴涓� featureGroup
+   * 2. 濡傛灉瀛樺湪浜嬩欢閫昏緫鐨勮瘽锛岀粦瀹歵ooltip,click浜嬩欢
+   * 3. 灏唋ayer娣诲姞鍒癿ap
+   * 4. 杩斿洖layer
+   * @param config
+   * @returns layer
+   */
   addLayer (config) {
     var code = config.code
     var logic = this.loadLogic(config)
     var layer = (logic && logic.initLayer && logic.initLayer((this.L))) || this.L.featureGroup({})
 
     if (logic.bindTooltip) {
-      layer.bindTooltip(logic.bindTooltip)
+      // 鍏ㄥ眬tips浣嶇疆
+      layer.bindTooltip(logic.bindTooltip, { direction: 'top', offset: [0, -15], sticky: false })
     }
     // 璋冪敤click浜嬩欢
     if (logic.clickListener) {
@@ -66,17 +106,26 @@
     return layer
   }
 
+  /**
+   * 濡傛灉 瀛樺湪宸茬粡鍔犺浇浜嗙殑瀵硅薄锛屽氨鐩存帴鍔犲埌map
+   * 濡傛灉 涓嶅瓨鍦ㄥ垯 璋冪敤 addLayer 鍙� 閫昏緫绫荤殑init 杩涜鍒濆鍖栨搷浣�
+   * 濡傛灉 瀛樺湪start鍑芥暟锛屽垯璋冪敤
+   * @param config
+   */
   show (config) {
+    var index = config.index
     var layer = this.layers[config.code]
     var logic = this.loadLogic(config)
     if (layer) {
       if (!this.map.hasLayer(layer)) {
+        index && layer.setZIndex(index)
         layer.addTo(this.map)
       }
     } else {
       logic && logic.init(this.addLayer(config), this.L)
     }
-    logic && logic.create && logic.create()
+    logic && logic.start && logic.start()
+    this.wmsLayerService && this.wmsLayerService.add(config)
   }
 
   hide (config) {
@@ -85,6 +134,7 @@
     layer && this.map.removeLayer(layer)
     const logic = this.loadLogic(config)
     logic && logic.destory && logic.destory()
+    this.wmsLayerService && this.wmsLayerService.remove(config)
   }
 
   /**
@@ -92,107 +142,24 @@
      * @param layerConfig
      */
   initEvent (layerConfig) {
-    this.map.on('zoomend ', (e) => this.toggleZoomByLayer(layerConfig))
+    // this.map.on('zoomend ', () => this.toggleByZoom())
   }
 
-  toggleZoomByLayer (layerConfig) {
-    var config = layerConfig
-    if (Array.isArray(layerConfig)) {
-      for (var i = 0, l = layerConfig.length; i < l; i++) {
-        config = layerConfig[i]
-        var layers = config.layers
-        var childLayer = config.childLayer
-        layers && this.toggleZoomByLayer(layers)
-        childLayer && this.toggleZoomByLayer(childLayer)
-        this.toggleZoomByConfig(config)
-      }
-    } else {
-      this.toggleZoomByConfig(layerConfig)
-    }
-  }
-
-  toggleZoomByConfig (config) {
+  toggleByZoom () {
     const zoom = this.map.getZoom()
-    var checked = config.checked
-    if (checked && config.minZoom) {
-      if (zoom > config.minZoom) {
-        this.show(config)
-      } else {
-        this.hide(config)
-      }
-    }
-  }
-
-  /**
-   *
-   * 鏍规嵁浼犵殑 feature瀵硅薄瀹氫綅锛�
-   * @param code
-   * @param feature
-   */
-  flyByFeature (feature, code) {
-    const type = feature.geometry.type
-    var point = []
-    switch (type) {
-      case 'Point':
-        point = clone(feature.geometry.coordinates)
-        break
-      case 'MultiLineString':
-        var coordinates = feature.geometry.coordinates
-        point = coordinates[parseInt(coordinates.length / 2)][0]
-        break
-    }
-    window.map.flyTo(point.reverse(), 15)
-    code && this.openPopup(code, feature.id)
-  }
-
-  openPopup (layerId, id) {
-    const layer = this.layers[layerId]
-
-    if (layer.eachLayer) {
-      layer.eachLayer(function (layer) {
-        const layers = layer.getLayers()
-        for (var i = 0; i < layers.length; i++) {
-          const lay = layers[i]
-          const feature = lay.feature
-          lay.closePopup()
-          if (feature.id === id) {
-            lay.openPopup()
-            break
-          }
+    for (var k in this.minZoomLayers) {
+      const configs = this.minZoomLayers[k]
+      for (var j in configs) {
+        const config = configs[j]
+        const checked = config.checked
+        // console.log(zoom)
+        // console.log(k)
+        if (checked && zoom > k) {
+          this.show(config)
+        } else if (checked && zoom < k) {
+          this.hide(config)
         }
-      })
-    }
-    /* for (var k in this.layers) {
-          var layerGroup = this.layers[k]
-          layerGroup.eachLayer(function (layer) {
-            console.log(layer)
-            console.log(layer.getAttribution())
-          })
-          var layers = layerGroup.getLayers()
-          if (layers) {
-            for (var m = 0; m < layers.length; m++) {
-              var layer = layers[m]
-              console.log(layer)
-              console.log(layer.getLayerId(val.id))
-              /!* var feature = layer.feature
-              if (feature.id === layerId) {
-                this.map.flyToBounds(bound)
-                return layer
-              } *!/
-            }
-          }
-        } */
-    return null
-  }
-
-  findLayerById (layer, id) {
-    const layers = layer.getLayers
-    if (layers) {
-      this.findLayerById(layer.getLayers(), id)
-    } else {
-      layer.eachLayer(function (layer) {
-        console.log(layer)
-      })
+      }
     }
   }
 }

--
Gitblit v1.8.0