本文基于腾讯课堂老胡的课《跟我学Openlayers--基础实例详解》做的学习笔记,使用的openlayers 5.3.x api。
源码 见 1021.html ,对应的 官网示例 https://openlayers.org/en/latest/examples/tile-transitions.html?q=setOpacity
image.png image.png<!DOCTYPE html>
<html>
<head>
<title>多图层切换
</title>
<link rel="stylesheet" href="../include/ol.css" type="text/css">
<script src="../include/ol.js"></script>
<script src="../include/jquery-3.1.1.min.js"></script>
</head>
<style>
#layertree li>span {
cursor: pointer;
}
</style>
<body>
<div id="layertree">
<h5>点击下面的图层节点,试着调整图层的参数</h5>
<ul>
<li><span>OSM 层</span>
<fieldset id="layer0">
<label class="checkbox" for="visible0">
<input id="visible0" class="visible" type="checkbox" />可见
</label>
<label>透明度</label>
<input class="opacity" type="range" min="0" max="1" step="0.01" />
</fieldset>
</li>
<li><span>图层组</span>
<fieldset id="layer1">
<label class="checkbox" for="visible1">
<input id="visible1" class="visible" type="checkbox" />可见
</label>
<label>透明度</label>
<input class="opacity" type="range" min="0" max="1" step="0.01" />
</fieldset>
<ul>
<li><span>食物危机主题图层</span>
<fieldset id="layer10">
<label class="checkbox" for="visible10">
<input id="visible10" class="visible" type="checkbox" />可见
</label>
<label>透明度</label>
<input class="opacity" type="range" min="0" max="1" step="0.01" />
</fieldset>
</li>
<li><span>世界陆地边界图</span>
<fieldset id="layer11">
<label class="checkbox" for="visible11">
<input id="visible11" class="visible" type="checkbox" />可见
</label>
<label>透明度</label>
<input class="opacity" type="range" min="0" max="1" step="0.01" />
</fieldset>
</li>
</ul>
</li>
</ul>
</div>
<div id="map" class="map"></div>
<script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}), new ol.layer.Group({
layers: [
new ol.layer.Tile({
source: new ol.source.TileJSON({
url: 'https://api.tiles.mapbox.com/v4/mapbox.20110804-hoa-foodinsecurity-3month.json?securee&access_token=pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiY2pzbmg0Nmk5MGF5NzQzbzRnbDNoeHJrbiJ9.7_-_gL8ur7ZtEiNwRfCy7Q',
crossOrigin: 'anonymous' //跨域访问问题
})
}),
new ol.layer.Tile({
source: new ol.source.TileJSON({
url: 'https://api.tiles.mapbox.com/v4/mapbox.world-borders-light.json?secure&access_token=pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiY2pzbmg0Nmk5MGF5NzQzbzRnbDNoeHJrbiJ9.7_-_gL8ur7ZtEiNwRfCy7Q',
crossOrigin: 'anonymous'
})
})
]
})
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([37.40570, 8.81566]),
zoom: 4
})
});
//核心函数,控制图层的显隐和透明度
function bindInputs(layerid, layer) {
var visibilityInput = $(layerid + ' input.visible');
visibilityInput.on('change', function () {
layer.setVisible(this.checked); //设置图层的显隐
});
visibilityInput.prop('checked', layer.getVisible());
var opacityInput = $(layerid + ' input.opacity');
opacityInput.on('input change', function () {
layer.setOpacity(parseFloat(this.value)); //设置图层的透明度
});
opacityInput.val(String(layer.getOpacity()));
}
//获取到map所有图层数组,针对每一个图层,绑定透明度和图层显隐
map.getLayers().forEach(function (layer, i) {
bindInputs('#layer' + i, layer);
if (layer instanceof ol.layer.Group) {
layer.getLayers().forEach(function (sublayer, j) {
bindInputs('#layer' + i + j, sublayer);
});
}
});
//图层组ul/li DOM 展开效果
$('#layertree li > span').click(function () {
$(this).siblings('fieldset').toggle();
}).siblings('fieldset').hide();
</script>
</body>
</html>
网友评论