I am in the process of developing a replacement web map for a response map (http://gis.doh.state.fl.us/ESF8PlanningMap/). After giving up on ArcGIS Online's ability to crate a useful map, I am now giving ArcGIS for JavaScript a try. I am running into a fairly common problem in that I would like to be able to load a group of layers that can be switched on and off by the users as needed. I have seen many suggestions, none of which work. This code is how I call the layer. It works.
//======================================== Agency for Persons with Disabilities Contractors
var APD = new esri.layers.FeatureLayer("http://gis.doh.state.fl.us/ArcGIS/rest/services/DEMO/HealthCareFacilities/MapServer/0")
APD.setVisibility(true);
map.addLayer(APD);
This is the checkbox I use to turn on/off the layer. It works:
<label><input type='checkbox' name="APD";
onclick='ChangeLayerVisiblity(name,this.checked);'>APD Contractors</label><br/>
This is my latest failed code:
//======================================== Layer Visibility
function ChangeLayerVisiblity(LayerName, IsChecked){
if (LayerName === "APD"){
if(IsChecked == true){
window.alert(LayerName + " is " + "ON" );
layer.APD.setVisibility(false);
}
else{
window.alert(LayerName + " is " + "OFF" );
layer.APD.setVisibility(true);
}
};
NOTE: if I comment out the two setVisibility lines the code works, but of course it doesn't turn off/on the layer. The setVisibility line in the constructor does work, but not in the function.
Surely, if the layer is constructed with a visibility attribute, it can be manipulated from code otherwise, why have it?
What am I missing here?