41. Module 4: Manipulating Layers (Replicating Your Widgets)#
Goal: Programmatically turn layers on and off. This is the exact logic inside your
layer-toggle-buttonvslayer-focus-button.
This is the “meat” of your custom widgets. Once you have the jimuMapView (from Module 3), you can start controlling layers.
41.1. 1. Accessing Layers (jimuLayerViews)#
The jimuMapView object has a property called jimuLayerViews. This is a dictionary of all the layers the map knows about.
Format:
{
"layer-123": JimuLayerView,
"layer-456": JimuLayerView,
// ...
}
To loop through them, we use a standard for...in loop:
const jimuLayerViews = jimuMapView.jimuLayerViews;
for (const viewId in jimuLayerViews) {
const layerView = jimuLayerViews[viewId];
console.log("Found layer: " + layerView.layer.title);
}
41.4. 4. Key Property: layer.visible#
The ArcGIS JS API is very simple here.
layer.visible = true(Shows)layer.visible = false(Hides)
That’s it. You don’t need to call .refresh() or .redraw(). The map reacts immediately.
41.5. 🎓 Quiz: Module 4#
Q1: How do you access the list of layers from the JimuMapView?
A) jimuMapView.layers
B) jimuMapView.jimuLayerViews
C) window.layers
Q2: What is the correct way to hide a layer?
A) layer.opacity = 0
B) layer.hide()
C) layer.visible = false
Q3: In the Focus Button logic, what happens to layers that are NOT in your “target” list?
A) Nothing, they stay as they are.
B) They are turned off (visible = false).
C) They are deleted from the map.
41.5.1. Reference Answers#
Q1: B. ExB wraps layers in
JimuLayerViewobjects, accessible via that dictionary.Q2: C. Setting the boolean
visibleproperty is the standard way.Q3: B. The specific goal of a “Focus” button is to isolate content, so it actively hides non-target layers.