-
Notifications
You must be signed in to change notification settings - Fork 0
/
layerselect.html
240 lines (230 loc) · 8.66 KB
/
layerselect.html
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>加载地质图层</title>
<script src="js/ol-debug.js"></script>
<link rel="stylesheet" href="js/ol.css">
<style type="text/css">
body, html, div, ul, li, iframe, p, img {
border: none;
padding: 0;
margin: 0;
font-size: 14px;
font-family: "微软雅黑";
}
#mapCon {
width: 100%;
height:100%;
position: absolute;
}
/* 图层控件层样式设置 */
.layerControl {
position: absolute;
bottom: 5px;
min-width: 200px;
max-height: 200px;
right: 0px;
top: 5px;
/*在地图容器中的层,要设置z-index的值让其显示在地图上层*/
z-index: 2001;
color: #ffffff;
background-color: #4c4e5a;
/*边缘的宽度*/
border-width: 10px;
/*圆角的大小 */
border-radius: 10px;
/*边框颜色*/
border-color: #000 #000 #000 #000;
}
.layerControl .title {
font-weight: bold;
font-size: 15px;
margin: 10px;
}
.layerTree li {
list-style: none;
margin: 5px 10px;
}
/* 鼠标位置控件层样式设置 */
#mouse-position {
float: left;
position: absolute;
bottom: 5px;
width: 330px;
height: 20px;
/*在地图容器中的层,要设置z-index的值让其显示在地图上层*/
z-index: 2000;
}
</style>
</head>
<body>
<div id="mapCon">
<div id="mouse-position"></div>
<div id="layerControl" class="layerControl">
<div class="title"><label>图层列表</label></div>
<ul id="layerTree" class="layerTree"></ul>
</div>
</div>
<script type="text/javascript">
//map中的图层数组
var layer = new Array();
//图层名称数组
var layerName = new Array();
//图层可见属性数组
var layerVisibility = new Array();
/**
* 加载图层列表数据
* @param {ol.Map} map 地图对象
* @param {string} id 图层列表容器ID
*/
function loadLayersControl(map, id) {
//图层目录容器
var treeContent = document.getElementById(id);
//获取地图中所有图层
var layers = map.getLayers();
for (var i = 0; i < layers.getLength() -3; i++) {
//获取每个图层的名称、是否可见属性
layer[i] = layers.item(i);
layerName[i] = layer[i].get('name');
layerVisibility[i] = layer[i].getVisible();
//新增li元素,用来承载图层项
var elementLi = document.createElement('li');
// 添加子节点
treeContent.appendChild(elementLi);
//创建复选框元素
var elementInput = document.createElement('input');
elementInput.type = "checkbox";
elementInput.name = "layers";
elementLi.appendChild(elementInput);
//创建label元素
var elementLable = document.createElement('label');
elementLable.className = "layer";
//设置图层名称
setInnerText(elementLable, layerName[i]);
elementLi.appendChild(elementLable);
//设置图层默认显示状态
if (layerVisibility[i]) {
elementInput.checked = true;
}
//为checkbox添加变更事件
addChangeEvent(elementInput, layer[i]);
}
layerName[3]="1:500万中国地质图(ArcGIS版)图层1";
layerName[4]="1:500万中国地质图(ArcGIS版)图层2";
layerName[5]="1:500万中国地质图(ArcGIS版)图层3";
for (var i = 3; i < layers.getLength(); i++) {
//获取每个图层的名称、是否可见属性
layer[i] = layers.item(i);
layerVisibility[i] = layer[i].getVisible();
//新增li元素,用来承载图层项
var elementLi = document.createElement('li');
// 添加子节点
treeContent.appendChild(elementLi);
//创建复选框元素
var elementInput = document.createElement('input');
elementInput.type = "checkbox";
elementInput.name = "layers";
elementLi.appendChild(elementInput);
//创建label元素
var elementLable = document.createElement('label');
elementLable.className = "layer";
//设置图层名称
setInnerText(elementLable, layerName[i]);
elementLi.appendChild(elementLable);
//设置图层默认显示状态
if (layerVisibility[i]) {
elementInput.checked = true;
}
//为checkbox添加变更事件
addChangeEvent(elementInput, layer[i]);
}
}
/**
* 为checkbox元素绑定变更事件
* @param {input} element checkbox元素
* @param {ol.layer.Layer} layer 图层对象
*/
function addChangeEvent(element, layer) {
element.onclick = function () {
if (element.checked) {
//显示图层
layer.setVisible(true);
}
else {
//不显示图层
layer.setVisible(false);
}
};
}
/**
* 动态设置元素文本内容(兼容)
*/
function setInnerText(element, text) {
if (typeof element.textContent == "string") {
element.textContent = text;
} else {
element.innerText = text;
}
}
var key = 'Q57tupj2UBsQNQdju4xL~xBceblfTd6icjljunbuaCw~AhwA-whmGMsfIpVhslZyknWhFYq-GvWJZqBnqV8Zq1uRlI5YM_qr7_hxvdgnU7nH';
//实例化Map对象加载地图
var map = new ol.Map({
//地图容器div的ID
target: 'mapCon',
//地图容器中加载的图层
layers: [
//加载瓦片图层数据
new ol.layer.Tile({
source: new ol.source.OSM(),
name: '底图'
}),
new ol.layer.Tile({
source: new ol.source.BingMaps({ key: key, imagerySet: 'Road' }),
name: "Bing Road"
}),
new ol.layer.Tile({
source: new ol.source.BingMaps({ key: key, imagerySet: 'Aerial' }),
name: "Bing Aerial"
}),
new ol.layer.Tile({
opacity: 0.7, //图层透明度
source: new ol.source.TileWMS({
//WMS服务基地址
url: 'http://219.142.81.85/arcgis/services/MAS12P51000011/MapServer/WMSServer',
//图层等参数
params: { 'LAYERS':'0', 'TILED': true }
})
}),
new ol.layer.Tile({
opacity: 0.7, //图层透明度
source: new ol.source.TileWMS({
//WMS服务基地址
url: 'http://219.142.81.85/arcgis/services/MAS12P51000011/MapServer/WMSServer',
//图层等参数
params: { 'LAYERS':'1', 'TILED': true }
})
}),
new ol.layer.Tile({
opacity: 0.7, //图层透明度
source: new ol.source.TileWMS({
//WMS服务基地址
url: 'http://219.142.81.85/arcgis/services/MAS12P51000011/MapServer/WMSServer',
//图层等参数
params: { 'LAYERS':'2', 'TILED': true }
})
})
],
//地图视图设置
view: new ol.View({
//地图初始中心点
center: [12406869, 4711989],
//地图初始显示级别
zoom: 4
})
});
//加载图层列表数据
loadLayersControl(map, "layerTree");
</script>
</body>
</html>