Disable show/hide button from LayerList ArcGIS Javascript

677
1
02-27-2023 10:59 PM
aggarwalarpit93
New Contributor II

Hello, I have a map and I have added a feature layer. It is displayed on the LayerList as well.

What I want is, currently user can show or hide this layer from the LayerList but I want it to be permanent on the Map. So I want to disable or hide this eye button which show/hide the layer on the map.

Here is my sample code:

<script>
        require([
            "esri/config",
            "esri/Map",
            "esri/views/MapView",
            "esri/layers/FeatureLayer",
            "esri/Basemap",
            "esri/Graphic",
            "esri/layers/GraphicsLayer",
            "esri/symbols/PictureMarkerSymbol",
            "esri/layers/support/LabelClass",
            "esri/rest/support/Query",
            "esri/geometry/Point",
            "esri/views/layers/LayerView",
            "esri/rest/locator",
            "esri/geometry/Polygon",
            "esri/symbols/SimpleLineSymbol",
            "esri/symbols/SimpleFillSymbol",
            "esri/widgets/LayerList",
            "esri/widgets/Expand",
        ], (esriConfig, Map, MapView, FeatureLayer, Basemap, Graphic, GraphicsLayer, PictureMarkerSymbol, LabelClass, Query, Point, LayerView, locator, Polygon, SimpleLineSymbol, SimpleFillSymbol, LayerList, Expand) => {

            esriConfig.apiKey= API_KEY;

            const map = new Map();

            // const map = new Map({
            //     basemap: "arcgis-topographic" // Basemap layer
            // });

            const view = new MapView({
                map: map,
                center: [85.7780685, 25.8560264],
                scale: 1800000,
                container: "viewDiv",
                constraints: {
                    snapToZoom: false
                }
            });

            // District feature layer
            // Add district layer custom layout
            const districtLayerBorderLayout = {
                type: "simple",
                symbol: {
                    type: "simple-line",
                    style: "short-dot",
                    color: "#000000",
                    width: "1px"
                }
            };

            const districtLayerTextLayout = new LabelClass({
                symbol: {
                    type: "text",
                    color: "#ff00ff",
                    font: {
                        size: 10
                    }
                },
                labelExpressionInfo: {
                    expression: "$feature.D_NAME"
                }
            });

            // Add district layer to the mapview
            const districtLayer = new FeatureLayer({
                url: "https://gis.fmiscwrdbihar.gov.in/arcgis/rest/services/GisAtlas/GIS_Atlas_2023/MapServer/22",
                outFields:["*"],
                renderer: districtLayerBorderLayout,
                labelingInfo: [districtLayerTextLayout]
            });
            map.add(districtLayer, 0);

            // layer list widget
            ////////////////////////// Add legend //////////////////////////
            const layerList = new LayerList({
                view: view
            });

            const layerListExpand = new Expand({
                expandIconClass: "esri-icon-layers",
                expandTooltip: "LayerList",
                view: view,
                expanded: false,
                content: layerList,
                group: "expandable-widgets"
            });

            view.ui.add([layerListExpand], "top-right")

        });
    </script>
0 Kudos
1 Reply
Sage_Wall
Esri Contributor

Hi @aggarwalarpit93, There really isn't a supported way to disable the ability to show and hide the layer in the layer list.  The primary propose of the layer list is to allow users to control the visibility of layers.  You could remove the layer you want to be visible all the time from the layer list by setting the listMode on your feature layer to `hide` and the visible property to `true`.  This would remove that layer from the layer list and make it visible.  Another option would be to use the legend widget instead of the layer list. The legend doesn't allow users to control visibility and provides a descriptions of the symbols used to represent the layers in the map.

0 Kudos