Select to view content in your preferred language

Toggle Visbility

1191
7
11-07-2011 05:14 PM
ShaunWeston
Frequent Contributor
I think I must be missing something very basic here, but all I want to do is have toggle working for map services. When the checkbox is checked, show the layer and when the checkbox is unchecked, hide the layer. The showing the layer bit works, but the hide does not. I've tried a lot of different methods and the only one that works is removeAllLayers()?

 function visibleDPLayers() {
  var DPlayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.mstn.govt.nz/ArcGIS/rest/services/DistrictPlan/MapServer");
  map.addLayer(DPlayer);
  
  if (legend.DPCheckbox.checked == true) {
   alert ('show')
   DPlayer.show();
  }
  else {
   alert ('hide')
   DPlayer.hide();
  }
 } 
0 Kudos
7 Replies
KellyHutchins
Esri Notable Contributor
Are you listening to an event that lets you know when the checkbox value changes? The Legend Widget sample in the ArcGIS API for JavaScript resource center shows how to toggle layer visibility via a checkbox:

http://help.arcgis.com/en/webapi/javascript/arcgis/demos/widget/widget_legendvisible.html
0 Kudos
JeffPace
MVP Alum
I think I must be missing something very basic here, but all I want to do is have toggle working for map services. When the checkbox is checked, show the layer and when the checkbox is unchecked, hide the layer. The showing the layer bit works, but the hide does not. I've tried a lot of different methods and the only one that works is removeAllLayers()?

 function visibleDPLayers() {
  var DPlayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.mstn.govt.nz/ArcGIS/rest/services/DistrictPlan/MapServer");
  map.addLayer(DPlayer);
  
  if (legend.DPCheckbox.checked == true) {
   alert ('show')
   DPlayer.show();
  }
  else {
   alert ('hide')
   DPlayer.hide();
  }
 } 



Yes your logic is a little scrambled (mixing visibility and adding layer).  I would suggest having 2 functions, an add layer function and a toggle visibility function.  Otherwise you will be adding your layer over and over

so
 function visibleDPLayers() {
  var DPlayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.mstn.govt.nz/ArcGIS/rest/services/DistrictPlan/MapServer");
  map.addLayer(DPlayer);
 
called from your init

and then an onClick(toggleVis) function

function toggleVis(){
if (legend.DPCheckbox.checked == true) {
   alert ('show')
   DPlayer.show();
  }
  else {
   alert ('hide')
   DPlayer.hide();
  }
}
0 Kudos
ShaunWeston
Frequent Contributor
Using the logic above it works for adding the layer, but when I want to remove the layer it just does not work. I've tried everything, but I can't get a layer to either remove or become invisible.
0 Kudos
by Anonymous User
Not applicable
looks like you're not actually getting the layer from the map, just from the JS variable.  In my experience, it's much more reliable use getLayer to return the actual layer from the map.  Try this:

map.getLayer('theIDoftheLayer').hide();

That should get it to hide.

One trick I've used is to wire up a function to the click event on your check box that looks to see if it's checked.  If yes, call show() on the layer from the map.  If not, call hide().  Since it's in a separate function, the state of the box will be set by the user click before calling the function.  You could wire it up in the HTML markup, or using dojo.connect();

function toggleLayer(){

      if(dojo.hasClass('mycheckboxid','checked')){
          map.getLayer('theLayerID').show();
      }
      else{
          map.getLayer('selectLyr').hide();
      }
            
}


Hope this helps,

Jeff
0 Kudos
ChrisBrannin
Regular Contributor
Is there a way to have the layers turned off by default. Also have only a particular layer active from map initiate?

Basically, I only want one or maybe 2 layers to be showing instead of all! Thanks in advance!

var landscapingLayer = new esri.layers.FeatureLayer("http://webgis.uwm.edu/ArcGISUWM/rest/services/Sustain1/MapServer/0", {id:'landscaping'});
        legendLayers.push({layer:landscapingLayer,title:'Green Landscaping'});
0 Kudos
HemingZhu
Frequent Contributor
Is there a way to have the layers turned off by default. Also have only a particular layer active from map initiate? 

Basically, I only want one or maybe 2 layers to be showing instead of all! Thanks in advance! 

var landscapingLayer = new esri.layers.FeatureLayer("http://webgis.uwm.edu/ArcGISUWM/rest/services/Sustain1/MapServer/0", {id:'landscaping'});
        legendLayers.push({layer:landscapingLayer,title:'Green Landscaping'});


("http://webgis.uwm.edu/ArcGISUWM/rest/services/Sustain1/MapServer/0", {id:'landscaping', visible: false});

0 Kudos
ChrisBrannin
Regular Contributor
Thank you, that was exactly what I was looking for. Thanks!
0 Kudos