0

在这个ESRI 教程LayerList 小部件中,是否可以在地图上显示单个图层?

每次单击图层时,前一个图层都应停用。所以你在地图上总是只有一层。

米歇尔

在此处输入图像描述

4

2 回答 2

2

使用 API 的第 4 版更新了答案。可以使用 2 个功能在创建小部件时添加功能。

listItemCreatedFunction 函数-

https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList.html#listItemCreatedFunction

根据api:

指定访问每个 ListItem 的函数。每个列表项都可以根据其可修改的属性进行修改。可以使用 ListItem 的 actionsSections 属性将操作添加到列表项。

和 operationItems 属性-

https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList.html#operationalItems

根据api:

表示业务层的 ListItems 的集合。

var LayerListWidget = new LayerList({
      listItemCreatedFunction: (event) => {
        var itemView = event.item; // layer-view of selection
        itemView.watch("visible", (event) => {
          LayerListWidget.operationalItems.forEach((layerView) => {
            if (layerView.layer.id != itemView.layer.id) {
              layerView.visible = false;
            }
          });
        });
      },
      view: view,
    });
于 2020-07-21T08:35:44.913 回答
1

I managed to write a code for you . Check it and let me know :

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Layer List Dijit</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.20/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="https://js.arcgis.com/3.20/esri/css/esri.css">
<script language="javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<style>
html, body, .container, #map {
height:100%;
width:100%;
margin:0;
padding:0;
margin:0;
font-family: "Open Sans";
}
#map {
padding:0;
}
#layerListPane{
width:25%;
}
.esriLayer{
  background-color: #fff;
}
.esriLayerList .esriList{
    border-top:none;
}
.esriLayerList .esriTitle {
  background-color: #fff;
  border-bottom:none;
}
.esriLayerList .esriList ul{
  background-color: #fff;
}

</style>
<script>var dojoConfig = { parseOnLoad: true };var busy=false;</script>
<script src="https://js.arcgis.com/3.20/"></script>
<script>
require([
"esri/arcgis/utils",
"esri/dijit/LayerList",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/domReady!"
], function(
arcgisUtils,
LayerList
) {
//Create a map based on an ArcGIS Online web map id
arcgisUtils.createMap("f63fed3f87fc488489e27c026fa5d434", "map").then(function(response){
    var myWidget = new LayerList({
       map: response.map,
       layers: arcgisUtils.getLayerList(response)
    },"layerList");

    myWidget.on("toggle",function(evt){
        if(busy) return;

        selectedLayerSubIndex = evt.subLayerIndex;
        if(selectedLayerSubIndex) return;

        selectedLayerIndex = evt.layerIndex;
        visibility = evt.visible;

        elm = $("#layerListPane input[type=checkbox][data-layer-index="+selectedLayerIndex+"]:not([data-sublayer-index])");


        otherCheckedElems = $("#layerListPane input[type=checkbox][data-layer-index!="+selectedLayerIndex+"]:not([data-sublayer-index]):checked");
        if(visibility){
            busy=true;
            otherCheckedElems.each(function () {
                $(this).click();
            });
            busy=false;
        }
        else{
            checkedLength = otherCheckedElems.length
            if(checkedLength==0) setTimeout("elm.click();", 100);
        }
    })

    myWidget.startup();
});

});
</script>
</head>
<body class="claro">
<div class="container" data-dojo-type="dijit/layout/BorderContainer"
data-dojo-props="design:'headline',gutters:false">
<div id="layerListPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'right'">
    <div id="layerList"></div>
</div>
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>
</div>
</body>
</html>
于 2017-04-23T18:43:09.987 回答