功能介绍
主要功能
根据openlayers3.0与easyUI tree 功能实现图层显隐控制功能,达到子节点选择实现单个图层的显隐,父节点选择实现所有图层的显隐。
页面展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/eba4340b2085f14bd530b02c98c1a8d7.gif)
主要代码
HTML+CSS
#xuanfu1 {
position: absolute;
top: 50px;
right: 40px;
background-color: rgba(134,149,237,0.7);
width: 170px;
height: 300px;
z-index: 1;
}
<div id="main">
<div id="xuanfu1">
<ul id="tt" class="easyui-tree" data-options="checkbox:true"></ul>
</div>
<div id="map" style="height: 100%;width:100%;position:relative;background:#ffffff"></div>
</div>
JS
$(document).ready(function () {
$.ajax({
url: "TCKZDataQuery",
success: function (data) {
var datatrans= JSON.parse(data);
var dataArr = datatrans.rows;
CreatTree(dataArr);
AddLayers(dataArr);
},
});
TCKZguanlian();
})
function CreatTree(dataArr) {
$('#tt').tree({
data: [{
id: 1,
text: '功能图层',
state: 'open',
}, {
id: 2,
text: '专题图层',
state: 'open',
children: [{
id: 21,
text: '铁路线',
checked: true,
}, {
id: 22,
text: '车站',
checked: true,
}]
}, {
id: 3,
text: '卫星影像',
checked: true,
}
]
});
var node = $('#tt').tree('find', 1);
var nodes = [{ id: '11', text: '绘制图层', checked: true }];
if (node) {
for (i = 0; i < dataArr.length; i++) {
var kejian;
if (dataArr[i].VISIBLE == 'true') {
kejian = true;
} else {
kejian = false;
}
nodes.push({ id: dataArr[i].ID, text: dataArr[i].TCM, checked: kejian });
}
$('#tt').tree('append', {
parent: node.target,
data: nodes
});
}
}
function TCKZguanlian(){
$("#tt").tree({
onCheck: function (node) {
var nodechi = $('#tt').tree('find', node.id);
var nodechildren = $('#tt').tree("getChildren", nodechi.target);
var AllLayers = map.getLayers().a;
if (nodechildren == null || nodechildren=='') {
for (i = 0; i < AllLayers.length; i++) {
var tcname1 = node.text;
var tcname2 = AllLayers[i].get('title');
if (tcname1 == tcname2) {
AllLayers[i].setVisible(node.checked);
}
}
} else {
for (j = 0; j < nodechildren.length; j++) {
var tcname1 = nodechildren[j].text;
for (i = 0; i < AllLayers.length; i++) {
var tcname2 = AllLayers[i].get('title');
if (tcname1 == tcname2) {
AllLayers[i].setVisible(node.checked);
}
}
}
}
}
})
}
var map = new ol.Map({
controls: ol.control.defaults({
attribution: false
}).extend([
new ol.control.MousePosition({
projection: 'EPSG:4326',
coordinateFormat: ol.coordinate.createStringXY(5)
}),
new ol.control.OverviewMap(),
new ol.control.ScaleLine(),
new ol.control.ZoomSlider(),
]),
target: 'map',
layers: [
new ol.layer.Tile({
title: '铁路线',
visible: true,
source: new ol.source.TileWMS({
url: 'http://127.0.0.1:8085/geoserver/lzjgjt/wms',
params: {
'FORMAT': 'image/png',
'VERSION': '1.1.1',
tiled: true,
"LAYERS": 'lzjgjt:xianlu_polyline',
"exceptions": 'application/vnd.ogc.se_inimage',
tilesOrigin: 93.408493 + "," + 32.439911
}
})
}),
new ol.layer.Tile({
title: '车站',
visible: true,
source: new ol.source.TileWMS({
url: 'http://127.0.0.1:8085/geoserver/lzjgjt/wms',
params: {
'FORMAT': 'image/png',
'VERSION': '1.1.1',
tiled: true,
"LAYERS": 'lzjgjt:chezhan_point',
"exceptions": 'application/vnd.ogc.se_inimage',
tilesOrigin: 93.487889 + "," + 32.441091
}
})
}),
],
view: new ol.View({
center: ol.proj.fromLonLat([104.06684, 34.39373]),
zoom: 6,
minZoom: 4,
maxZoom: 16,
})
});
c#
#region 图层控制
public ActionResult TCKZDataQuery()
{
string sql = " select * from TUCENG_DIC t ";
string jsonData = GetJeJsonData(db.Database.Connection.ConnectionString, sql, Request);
return Content(jsonData);
}
#endregion
图层数据格式
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dfa90fbf003fe09fa0965cd11d01e68a.png)