Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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> 

share|improve this question
    
Just my two cents, not sure if I am helping you: I just noticed that in the "list_item" definition, the field "value" has a value of 0 with NO quotations, while in "FEMA_item" has simple quotations. A part of that, I would recommend you to use different id names, such as O/1 or id1/id2, because JS is, in general, quite tricky with types. And finally: if you want to show/hide a layer depending on a checkbox, why don't you use .hide() and .show() methods? I think it is easier. –  iamgin Dec 10 '12 at 19:35
    
thanks for your comment. I've experimented with the quotes and they don't seem to make a difference. I've also changed up the ids with no difference as well. Can you explain or give an example of the hide and show methods and how to use them with building a layer list? –  Craig Dec 10 '12 at 19:46
    
Oh well, I was thinking in doing a loop through your array layer list and on position [i] apply .show() or .hide() depending on what layer you want to highlight. But I am not sure if that matches your schema as yesterday I wrote one a rush. In my last app, I had 4 feature layers (without an array) and depending what the user checked/unchecked in my toggle box I applied those functions to the layer. I imagine that if those layers are in a vector, you can switch them ON/OFF accessing to the [i] position. –  iamgin Dec 11 '12 at 19:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.