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
(https://developers.arcgis.com/javascript/latest/references/map-components/arcgis-layer-list/)
Solved! Go to Solution.
Hi @ToddGurnee ,
I noticed a couple things leading to the behavior described above.
For the Home component not responding, in your latest app you were listening for the "go" event using the "on()" method but these are properties and methods on the legacy widget not the web component. Either creating a new button or listening to the Home component's "arcgisGo" method should fix that issue.
In your arcgisViewReadyChange event listener you were trying to get the view from "event.detail" you need to get it from "event.target", event detail is null. You could also use the reference to the element "arcgisMap".
The last thing I saw was in your query results you need to check to see if the returned geometry is a point. Point's don't have extents and this was breaking the script.
This codepen has the updates I made:
https://codepen.io/sagewall/pen/WbQRyRy?editors=1010
Hope this helps 🙂
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).
Thanks for the quick response. I have implemented both of your code snippets. Adjusting the Home code to turn off the Trailheads layer. The magnifying glass shows up in the LayerList. I receive no errors but the neither of the Visibility or ZoomTo functionality get triggered.
I've added console.logs at various points in the code to gain some insight but even these never fire. My attempts with AI also lead to this same result. The code below is how I have implemented your code. Is there something I have missed or perhaps another way of going about this? Thanks again.
Hi @ToddGurnee ,
Take a look at this sample it shows how to add custom actions to the layer list. Your last code snippet is really close, it looks like you just need to rename the event your listening too. There is a slight difference between the Core API event "trigger-action" and the component event's name "arcgisTriggerAction". Try renaming the event listener to listen to "arcgisTriggerAction" as shown here:
https://developers.arcgis.com/javascript/latest/sample-code/layer-list-actions/
Thank you Sage_Wall. You got me pointed in the right direction as far as the LayerList. I've gotten that code to work. It includes some code for another app of mine that has filtered jurisdictional boundaries in the webmap. So now it goes to the geometry and not full extent.
But I am unable to get anything to even trigger (arcgisTriggerAction or arcgisViewReadyChange or numerous other things) using with the Home component. If you have any additional thoughts on that I would greatly appreciate it.
Below is my updated code:
Hi @ToddGurnee ,
I noticed a couple things leading to the behavior described above.
For the Home component not responding, in your latest app you were listening for the "go" event using the "on()" method but these are properties and methods on the legacy widget not the web component. Either creating a new button or listening to the Home component's "arcgisGo" method should fix that issue.
In your arcgisViewReadyChange event listener you were trying to get the view from "event.detail" you need to get it from "event.target", event detail is null. You could also use the reference to the element "arcgisMap".
The last thing I saw was in your query results you need to check to see if the returned geometry is a point. Point's don't have extents and this was breaking the script.
This codepen has the updates I made:
https://codepen.io/sagewall/pen/WbQRyRy?editors=1010
Hope this helps 🙂
Thank you so much for your help!