Select WMTS sublayer
这个示例显示如何通过 WMTSLayer 的 activeLayer 来切换 WMTSLayer 的子图层。
通常来说,WMTS 服务充当了 WMTS 图层的目录,可以用 activeLayer 指定图层(某则默认为第一个子图层)。WMTSLayer 最开始会执行 WMTS getCapabilities 请求,该请求可能需要跨域。
数据说明:示例程序使用了由 NASA/GSFC/ESDIS 运营的 GIBS 提供的影像。
程序完整源代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link rel="stylesheet" href="https://js.arcgis.com/4.5/esri/css/main.css">
<title>Select WMTSLayer sublayer - 4.5</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 95%;/* 为图层切换工具留出空间 */
width: 100%;
}
select,
label {
font-family: "Avenir Next W00", "Helvetica Neue", Helvetica, Arial, sans-serif
}
#selectDiv {
background-color: lightgrey;
padding: 10px;
}
#theLabel {
visibility: hidden;
}
.esri-layer-list {
width: 310px;
}
</style>
<script src="https://js.arcgis.com/4.5/"></script>
<script>
var map, view;
require([
"esri/config",
"esri/Map",
"esri/Viewpoint",
"esri/geometry/Extent",
"esri/views/MapView",
"esri/layers/WMTSLayer",
"esri/widgets/LayerList",
"esri/core/watchUtils",
"dojo/dom",
"dojo/dom-construct",
"dojo/on",
"dojo/domReady!"
], function(
esriConfig,
Map,
Viewpoint,
Extent,
MapView,
WMTSLayer,
LayerList,
watchUtils,
dom,
domConstruct,
on
) {
// CORS 处理
esriConfig.request.corsEnabledServers.push(
"https://gibs.earthdata.nasa.gov");
layer = new WMTSLayer({
url: "https://gibs.earthdata.nasa.gov/wmts/epsg4326/best",
copyright: "<a href='https://earthdata.nasa.gov'>Earthdata</a> by NASA",
activeLayer: {
id: "SRTM_Color_Index"
}
});
map = new Map({
layers: [layer]
});
view = new MapView({
container: "viewDiv",
map: map
});
view.then(function() {
// 图层列表小部件
layerList = new LayerList({
view: view
});
view.ui.add(layerList, "bottom-left");
layer.load().then(function() {
// 添加所有子图层到 select 元素
layer.sublayers.forEach(function(sublayer, i) {
selectSublayer.options[selectSublayer.options.length] =
new Option('(' + i + ") " + sublayer.title, sublayer.id);
});
// 显示下拉框
var theDiv = document.getElementById('theLabel');
theDiv.style.visibility = "visible";
});
});
// 切换图层
document.getElementById("selectSublayer").onchange = function() {
layer.activeLayer = layer.findSublayerById(event.target.value).clone();
};
});
</script>
</head>
<body>
<div id="selectDiv">
<label id="theLabel">
从 WMTS 目录选择一个图层 <select id="selectSublayer"></select>
</label>
</div>
<div id="viewDiv"></div>
</body>
</html>
程序运行效果: