From 3eda2a49edc60f3961604223f557dcd6ab6db94f Mon Sep 17 00:00:00 2001
From: wangrui <zephyrs0894@163.com>
Date: 星期三, 23 十二月 2020 16:52:54 +0800
Subject: [PATCH] 底图切换组件开发完成

---
 src/components/helpers/BasemapHelper.js |  118 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 98 insertions(+), 20 deletions(-)

diff --git a/src/components/helpers/BasemapHelper.js b/src/components/helpers/BasemapHelper.js
index d608cf8..652c7e8 100644
--- a/src/components/helpers/BasemapHelper.js
+++ b/src/components/helpers/BasemapHelper.js
@@ -1,58 +1,136 @@
+/* eslint-disable no-debugger */
+import AjaxUtils from '@/utils/AjaxUtils'
 /**
  * 搴曞浘绠$悊鍔╂墜锛岃礋璐e簳鍥惧垱寤哄強寮�鍏�
  */
-function BasemapHelper(map, L) {
-    this.map = map
-    this.L = L
-    this.basemapList = []
+class BasemapHelper{
+    constructor(options) {
+        this.map = options.map
+        this.L = options.L
+        this.basemapList = []
+        this.basemapMap = new Map()
+        this.basemapLayerGroup = options.L.layerGroup().addTo(options.map)
+    }
 
     /**
      * 璇ユ柟娉曡礋璐e悇绉嶅簳鍥惧姞杞藉埌鍦板浘涓�
      * @param map
      * @param defBasemapName 鍒濆鍖栧畬鎴愬悗锛岄粯璁ゆ樉绀虹殑鍥惧眰
      */
-    this.initBasemap = (map, defBasemapName) => {
-        console.log(map, defBasemapName)
-        // todo 鍒涘缓瀹屼互鍚庯紝鎶婂浘灞傞兘鏀惧埌basemapList瀵硅薄淇濆瓨璧锋潵锛屾柟渚垮叾浠栨帴鍙h皟鐢�
+    initBasemap = (mapConfig, isIntranet) => {
+        if(isIntranet) { // 鍐呯綉
+            this._getToken(mapConfig); // 鑾峰彇token鍚庯紝骞舵寜閰嶇疆鍔犺浇鍦板浘
+        }else { // 澶栫綉
+            this._createBasemapByConfig(mapConfig)
+        }
 
-        return null
+        return this.basemapMap
     }
 
     /**
      * 鑾峰彇鎵�鏈夌殑搴曞浘鍒楄〃
      * @returns {null} 缁撴瀯锛歔{鍚嶇О, 鍥惧眰寮曠敤}]
      */
-    this.getBasemapList = (map) => {
-        console.log(map)
-        return null
+    getBasemapList = () => {
+        return this.basemapList
     }
 
     /**
      * 閫氳繃鍚嶇О鑾峰彇搴曞浘瀵硅薄
      * @param map 缁撴瀯锛歔{鍚嶇О, 鍥惧眰寮曠敤}]
-     * @param name 鍚嶇О
+     * @param code 鍚嶇О
      */
-    this.getBasemap = (map, name) => {
-        console.log(map, name)
+    getBasemap = (map, code) => {
+        return this.basemapMap.get(code)
     }
 
     /**
      * 鏄剧ず鏌愪釜鍥惧眰
-     * @param map 鍦板浘瀵硅薄
      * @param layer 寰呮樉绀哄浘灞傚紩鐢�
      * @param isHideOthers 鏄惁鍏堝叧闂叾浠栧浘灞傦紝榛樿鏄痶rue
      */
-    this.showBasemap = (map, layer, isHideOthers) => {
-        console.log(map, layer, isHideOthers)
+    showBasemap = (code, isHideOthers = true) => {
+        debugger
+        let basemap = this.basemapMap.get(code)
+        if(isHideOthers) {
+            for(let i = 0, len = this.basemapList.length; i < len; ++i){
+                this.map.removeLayer(this.basemapList[i].layer)
+                this.map.removeLayer(this.basemapList[i].annotation)
+            }
+        }
+        this.map.addLayer(basemap.layer)
+        this.map.addLayer(basemap.annotation)
     }
 
     /**
      * 闅愯棌鏌愪釜鍥惧眰
-     * @param map 鍦板浘瀵硅薄
      * @param layer 寰呭叧闂浘灞傚紩鐢�
      */
-    this.hideBasemap = (map, layer) => {
-        console.log(map, layer)
+    hideBasemap = (code) => {
+        debugger
+        let basemap = this.basemapMap.get(code)
+        this.map.removeLayer(basemap.layer)
+        this.map.removeLayer(basemap.annotation)
+    }
+
+    // 鍏綉鍒涘缓鍦板浘閮ㄥ垎
+    _createBasemapByConfig(mapConfig){
+        console.log(mapConfig)
+        let internetBasemaps = mapConfig.mapConfig.InternetBaseMaps
+        for(let i = 0, len = internetBasemaps.length; i < len; ++i) {
+            let basemapConfig = internetBasemaps[i]
+            let basemapLayer = this.L.tileLayer(basemapConfig.map.url, basemapConfig.map.option)
+            let basemapAnnotationLayer = this.L.tileLayer(basemapConfig.annotation.url, basemapConfig.annotation.option)
+
+            let basemap = {
+                code: basemapConfig.code,
+                name: basemapConfig.name,
+                conf: basemapConfig,
+                layer: basemapLayer,
+                annotation: basemapAnnotationLayer
+            }
+         
+            this.basemapList.push(basemap);
+            this.basemapMap.set(basemapConfig.code, basemap)
+            if(typeof basemapConfig.isAddToMap !== 'undefined' && basemapConfig.isAddToMap) {
+                this.basemapLayerGroup.addLayer(basemapLayer)
+                this.basemapLayerGroup.addLayer(basemapAnnotationLayer)
+            }
+        }
+    }
+
+    // 鍐呯綉鍦板浘鍒涘缓閮ㄥ垎
+    // 鑾峰彇鍐呯綉鍦板浘token锛屽苟鍔犺浇鍒板湴鍥句腑
+    _getToken = (mapConfig) => {
+        let params = mapConfig.TokenConfig
+        AjaxUtils.GetDataAsynByUrl(params.url, params.option, (token) => {
+            this._showTDT(token, mapConfig)
+        })
+    }
+
+    // 鍐呯綉鍦板浘鍔犺浇锛屽苟鍔犺浇鍒板湴鍥�
+    _showTDT = (token, mapConfig) => {
+        let intranetBasemaps = mapConfig.mapConfig.IntranetBaseMaps
+        for(let i = 0, len = intranetBasemaps.length; i < len; ++i) {
+            let basemapConfig = intranetBasemaps[i]
+            let basemapLayer = this.L.tileLayer(basemapConfig.map.url, basemapConfig.map.option)
+            let basemapAnnotationLayer = this.L.tileLayer(basemapConfig.annotation.url, basemapConfig.annotation.option)
+
+            let basemap = {
+                code: basemapConfig.code,
+                name: basemapConfig.name,
+                conf: basemapConfig,
+                layer: basemapLayer,
+                annotation: basemapAnnotationLayer
+            }
+         
+            this.basemapList.push(basemap);
+            this.basemapMap.set(basemapConfig.code, basemap)
+            if(typeof basemapConfig.isAddToMap !== 'undefined' && basemapConfig.isAddToMap) {
+                this.basemapLayerGroup.addLayer(basemapLayer)
+                this.basemapLayerGroup.addLayer(basemapAnnotationLayer)
+            }
+        }
     }
 }
 

--
Gitblit v1.8.0