派生自 wuyushui/SewerAndRainNetwork

wangrui
2020-12-23 3eda2a49edc60f3961604223f557dcd6ab6db94f
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
135
136
137
/* eslint-disable no-debugger */
import AjaxUtils from '@/utils/AjaxUtils'
/**
 * 底图管理助手,负责底图创建及开关
 */
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)
    }
 
    /**
     * 该方法负责各种底图加载到地图上
     * @param map
     * @param defBasemapName 初始化完成后,默认显示的图层
     */
    initBasemap = (mapConfig, isIntranet) => {
        if(isIntranet) { // 内网
            this._getToken(mapConfig); // 获取token后,并按配置加载地图
        }else { // 外网
            this._createBasemapByConfig(mapConfig)
        }
 
        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, 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 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)
            }
        }
    }
}
 
export default BasemapHelper