Select to view content in your preferred language

Map Components - Home - LayerList

70
1
Thursday
ToddGurnee
Occasional Contributor

Two part question here. I was hoping to get some help expanding on the functionality of both the Home component and also the LayerList component.

#1 - How can you change a layer's visibility setting when the user clicks the Home button? (Example: the users has turned on all the layers and I would like the Home button to not only return to the default extent but also the default layers visible.)

#2 - I would like to add the ZoomTo button to the layers in the LayerList. I can add the symbol to the layer but can't get it to trigger.  

I have been able to do these using widgets but struggling converting to components.

For simplicity sake the code I provided is just an expanded version of the code from ESRI's LayerList example

I appreciate any help

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" />
    <title>Map components LayerList Example</title>
    <link rel="icon" href="data:;base64,=" />
    <style>
      html,
      body {
        margin: 0;
      }
     
      .tool-container {
      display: flex;
      gap: 0.5rem;
    }
     
      arcgis-map {
        display: block;
        height: 100vh;
      }
    </style>

    <!-- Load Calcite Components-->
    <script type="module" src="https://js.arcgis.com/calcite-components/3.2.1/calcite.esm.js"></script>

    <!-- Load the ArcGIS Maps SDK for JavaScript -->
    <link rel="stylesheet" href="https://js.arcgis.com/4.33/esri/themes/dark/main.css" />
    <script src="https://js.arcgis.com/4.33/"></script>

   <script type="module" src="https://js.arcgis.com/4.33/map-components/"></script>
  </head>

  <body>
<arcgis-map
    item-id="237b9584339446a0b56317b5962a4971">
 
    <arcgis-home>
    </arcgis-home>

    <arcgis-layer-list
        position="bottom-left">
    </arcgis-layer-list>

</arcgis-map>

<script>
    async function load() {
   
    const Layer = await $arcgis.import("esri/layers/Layer");

    // Reference the map component
    const mapElem = document.querySelector("arcgis-map");
   
    mapElem.addEventListener("arcgisViewReadyChange", () => {
    }, { once: true });
    };

load();

    document.querySelector("arcgis-layer-list").listItemCreatedFunction = (event) => {
        const { item } = event;

        // Exclude group layers, otherwise the legend will be displayed twice
        if (item.layer.type != "group") {
          item.panel = {
            content: "legend",
            open: false,
          };
        }
      }
    </script>

(https://developers.arcgis.com/javascript/latest/references/map-components/arcgis-layer-list/)

0 Kudos
1 Reply
VenkataKondepati
New Contributor

Part 1: Reset Layer Visibility on Home Button Click
The <arcgis-home> component triggers a zoom to the default extent, but doesn't reset layers by default. To do this, manually listen for the click on the Home button and reset layer visibility like this:

const homeBtn = document.querySelector("arcgis-home");

homeBtn.addEventListener("click", () => {
const view = document.querySelector("arcgis-map").view;

// Reset visibility for each layer to default (example: make all layers off)
view.map.layers.forEach((layer) => {
layer.visible = layer.title === "Basemap" ? true : false; // customize per your need
});
});


Replace the logic based on your layer names or default visibility needs.

Part 2: Add ZoomTo Action in LayerList Items
To add a ZoomTo button and make it functional, customize the listItemCreatedFunction:

document.querySelector("arcgis-layer-list").listItemCreatedFunction = (event) => {
const { item } = event;

// Skip group layers
if (item.layer.type !== "group") {
item.panel = {
content: "legend",
open: false,
};

item.actionsOpen = true;
item.actionsSections = [
[
{
title: "Zoom to layer",
className: "esri-icon-zoom-in-magnifying-glass",
id: "zoom-to-layer"
}
]
];
}
};

// Handle the ZoomTo action
document.querySelector("arcgis-layer-list").addEventListener("trigger-action", (event) => {
const actionId = event.action.id;
const layer = event.item.layer;
const view = document.querySelector("arcgis-map").view;

if (actionId === "zoom-to-layer" && layer.fullExtent) {
view.goTo(layer.fullExtent);
}
});

Ensure layers have fullExtent available (some service layers may require loading).

You can extend this to add more actions (e.g., toggle visibility, transparency).

0 Kudos