派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-02-22 b89ad0355f2d466304ecc2dad86407e26aa69db4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint-disable no-debugger */
import AjaxUtils from '@/utils/AjaxUtils'
 
/**
 * 底图管理助手,负责底图创建及开关
 */
class BasemapHelper {
  constructor (options) {
    this.map = options.map
    this.L = window.L
    this.basemapList = []
    this.basemapMap = new Map()
    this.basemapLayerGroup = this.L.layerGroup().addTo(options.map)
  }
 
    /**
     * 该方法负责各种底图加载到地图上
     * @param map
     * @param defBasemapName 初始化完成后,默认显示的图层
     */
    initBasemap = (config, isIntranet) => {
      if (isIntranet) { // 内网
        this._getToken(config) // 获取token后,并按配置加载地图
      } else { // 外网
        this._createBasemapByConfig(config)
      }
 
      return this.basemapMap
    }
 
    /**
     * 获取所有的底图列表
     * @returns {null} 结构:[{名称, 图层引用}]
     */
    getBasemapList = () => {
      return this.basemapList
    }
 
    /**
     * 通过名称获取底图对象
     * @param map 结构:[{名称, 图层引用}]
     * @param code 名称
     */
    getBasemap = (map, code) => {
      return this.basemapMap.get(code)
    }
 
    /**
     * 显示某个图层
     * @param layer 待显示图层引用
     * @param isHideOthers 是否先关闭其他图层,默认是true
     */
    showBasemap = (code, showAnnotation, isHideOthers = true) => {
      const basemap = this.basemapMap.get(code)
      if (isHideOthers) {
        this.basemapLayerGroup.clearLayers()
      }
      this.basemapLayerGroup.addLayer(basemap.layer)
      if (showAnnotation) {
        this.basemapLayerGroup.addLayer(basemap.annotation)
      }
    }
 
    /**
     * 隐藏某个图层
     * @param layer 待关闭图层引用
     */
    hideBasemap = (code) => {
      const basemap = this.basemapMap.get(code)
      this.map.removeLayer(basemap.layer)
      this.map.removeLayer(basemap.annotation)
    }
 
    // 公网创建地图部分
    _createBasemapByConfig (config) {
      const internetBasemaps = config.mapConfig.InternetBaseMaps
      for (let i = 0, len = internetBasemaps.length; i < len; ++i) {
        const basemapConfig = internetBasemaps[i]
        const basemapLayer = this.L.tileLayer(basemapConfig.map.url, basemapConfig.map.option)
        const basemapAnnotationLayer = this.L.tileLayer(basemapConfig.annotation.url, basemapConfig.annotation.option)
 
        const 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 = (config) => {
      const params = config.TokenConfig
      AjaxUtils.GetDataAsynByUrl(params.url, params.option, (token) => {
        this._showTDT(token, config)
      })
    }
 
    // 内网地图加载,并加载到地图
    _showTDT = (token, config) => {
      const intranetBasemaps = config.mapConfig.IntranetBaseMaps
      for (let i = 0, len = intranetBasemaps.length; i < len; ++i) {
        const basemapConfig = intranetBasemaps[i]
        const basemapLayer = this.L.tileLayer(basemapConfig.map.url, basemapConfig.map.option)
        const basemapAnnotationLayer = this.L.tileLayer(basemapConfig.annotation.url, basemapConfig.annotation.option)
 
        const 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)
        }
      }
    }
}
 
export default BasemapHelper