I'm trying to adapt my code so that I can create a layer list of all layers in multiple services. I'd like them to be able to be turned on/off. My current code builds a dynamic layer list from one service, however, I've recently been asked to add another service to the app and append the layers to the same div.
I've found this question from a while back and am running into the same issue the OP described, where I can toggle one service off, ONCE, but never back on, and I can't toggle the second service at all. The code I have modified is below. Can anybody notice any glaring mistakes or things that I might be missing? I've attempted to contact the OP in the post I linked, but thought this might help get a fresh look.
dojo.require("esri.map");
var layer, parcellayer, map, visible = [];
var startExtent
function init() {
//Set the initial map extent
startExtent = new esri.geometry.Extent({
"xmin": -9199621.530456403,
"ymin": 3365235.5214724857,
"xmax": -9077475.159256855,
"ymax": 3443965.6606061114,
"spatialReference": {
"wkid": 102100
}
});
map = new esri.Map("map", {
extent: startExtent,
logo: false
});
var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
map.addLayer(basemap);
//Use the ImageParameters to set the visible layers in the map service during ArcGISDynamicMapServiceLayer construction.
var imageParameters = new esri.layers.ImageParameters();
imageParameters.layerIds = [0,1,2,3,4];
imageParameters.layerOption = esri.layers.ImageParameters.LAYER_OPTION_SHOW;
//can also be: LAYER_OPTION_EXCLUDE, LAYER_OPTION_HIDE, LAYER_OPTION_INCLUDE
layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gisprod2/ArcGIS/rest/services/Dynamic/Zoning/MapServer", {"imageParameters":imageParameters});
map.addLayer(layer);
//Use the ImageParameters to set the visible layers in the FEMA map service during ArcGISDynamicMapServiceLayer construction.
var FEMAimageParameters = new esri.layers.ImageParameters();
FEMAimageParameters.layerIds = [0];
FEMAimageParameters.layerOption = esri.layers.ImageParameters.LAYER_OPTION_SHOW;
//can also be: LAYER_OPTION_EXCLUDE, LAYER_OPTION_HIDE, LAYER_OPTION_INCLUDE
FEMAlayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gisprod2/arcgis/rest/services/Static/EnvironmentallySensitiveOverlayZones/MapServer", {"FEMAimageParameters":FEMAimageParameters});
map.addLayer(FEMAlayer);
}
//Sets up the ability for dynamic layers to be toggled.
function updateLayerVisibility() {
var inputs = dojo.query(".list_item"), input;
//in this application layer 2 is always on.
visible = [];
for (var i=0, il=inputs.length; i<il; i++) {
if (inputs[i].checked) {
visible.push(inputs[i].id);
}
}
//if there aren't any layers visible set the array value to = -1
if(visible.length === 0){
visible.push(-1);
}
layer.setVisibleLayers(visible);
}
//Sets up the ability for FEMA layers to be toggled.
function updateFEMALayerVisibility(){
var inputs = dojo.query(".FEMA_item"), input;
//Placing layer numbers below will enforce them to be visible.
visible = [];
for (var i = 0, il = inputs.length; i < il; i++) {
if (inputs[i].checked) {
visible.push(inputs[i].id);
}
}
//if there aren't any layers visible set the array value to = -1
if(visible.length === 0){
visible.push(-1);
}
FEMAlayer.setVisibleLayers(visible);
}
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
This sample loads an ArcGISDynamicMapServiceLayer and presents check boxes for only the layers that should be toggled on and off by users. <br />
<b> FEMA: <span id="FEMA"></b>
<br>
<input type='checkbox' class='FEMA_item' checked='checked' id='0' value='0' onclick='updateFEMALayerVisibility();' /> FEMA Layers
<br>
</span><br />
<b> Zoning: <span id="Zoning"></b>
<br>
<input type='checkbox' class='list_item' checked='checked' id='0' value=0 onclick='updateLayerVisibility();'/> Zoning Classifications
<br>
<br />
<div id="map" class="claro" style="width:1500px; height:700px; border:1px solid #000;"></div>
</div>