IdentifyParameters.LAYER_OPTION_VISIBLE still gives identify results on all layers

7920
26
Jump to solution
02-03-2012 09:36 AM
SwenWaschk
New Contributor III
I've used both the create layer list and the identify pop-up samples to create the following code (see first reply)


But if I turn of a layer in the layerlist it still is identified. How do i stop this from happening?
0 Kudos
1 Solution

Accepted Solutions
AdrianMarsden
Occasional Contributor III
It looks like you should be able just to add

identifyParams.layerIds = visible


to

   function executeIdentifyTask(evt) { 
        identifyParams.geometry = evt.mapPoint; 
        identifyParams.mapExtent = map.extent; 


to give you

   function executeIdentifyTask(evt) { 
        identifyParams.geometry = evt.mapPoint; 
        identifyParams.mapExtent = map.extent;
identifyParams.layerIds = visible 


Should work

View solution in original post

0 Kudos
26 Replies
SwenWaschk
New Contributor III
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Rijk van Dommel en Aa</title>
 <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.6/js/dojo/dijit/themes/soria/soria.css">
 <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.6/js/esri/dijit/css/Popup.css">
    <style type="text/css"> 
  html, body { 
   height: 100%; width: 100%; margin: 0; padding: 0;  
  } 
  body{ 
   background-color:#FFF; overflow:hidden; font-family: "Trebuchet MS"; 
  }  
  #map{ 
   overflow:hidden; 
   padding:0; 
  }
  #layerlist{
   background-color: #FFF;
   padding: 5px;
   border: solid 1px #769dc0;
  }
  a:link {
   color: #009FB4;
   text-decoration: none;
  }
  a:visited {
   text-decoration: none;
   color: #009FB4;
  }
  a:hover {
   text-decoration: underline;
   color: #009FB4;
  }
  a:active {
   text-decoration: none;
   color: #009FB4;
  }
  #info {
   color:#F00;
  }
  
  /*Opmaak Popup Window*/

  .esriPopup.myTheme .sizer {
   width: 400px;
  }
  
  .esriPopup.myTheme .titlePane,
  .dj_ie7 .esriPopup.myTheme .titlePane .title {
   background-color: #b7dafb; /*Achtergrondkleur titel*/
   color:#4a4a4a; /*Textkleur titel*/
   font-weight:700; /*dikte letters*/
   z-index:100;
  }

  .esriPopup.myTheme .titlePane {
   border:1px solid #769dc0; /*kleur border titel*/
   z-index: 100;
  }

  .esriPopup.myTheme a {
   color:#0095aa; /*kleur van de hyperlinks*/
   z-index:100;
  }

  .esriPopup.myTheme .titleButton,
  .esriPopup.myTheme .pointer,
  .esriPopup.myTheme .outerPointer,
  .esriPopup.myTheme .esriViewPopup .gallery   .mediaHandle,
  .esriPopup.myTheme .esriViewPopup .gallery .mediaIcon {
   background-image:url(images/popup_sprite.png); /*afbeelding met de opmaak van de popup*/
   z-index:100;
  }

  .esriPopup.myTheme .contentPane,.esriPopup.myTheme   .actionsPane {
   background-color:#ffffff; /*Achtergrondkleur inhoud*/
   color:#000000;/*Tekstkleur inhoud*/
   border-left:1px solid #769dc0;/*border links kleur inhoud*/
   border-right:1px solid #769dc0;/*border rechts kleur inhoud*/
   z-index:100;
  }
  
  /*.esriPopup.myTheme{
   border-bottom: 1px solid #769dc0; /*border onder kleur inhoud*/
   z-index:100;
  }*/
    </style>
0 Kudos
SwenWaschk
New Contributor III
part two of the code

<script type="text/javascript"> 
      var djConfig = { 
        parseOnLoad: true 
      }; 
    </script>
 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.6"></script>
 <script type="text/javascript">
   dojo.require("dijit.dijit"); // optimize: load dijit layer 
      dojo.require("dijit.layout.BorderContainer"); 
      dojo.require("dijit.layout.ContentPane");
      dojo.require("esri.map");
   dojo.require("esri.dijit.Popup");

      var layer, map, visible = [];
   var identifyTask,identifyParams;

      function init() {
   
  //Popup
  esri.bundle.widgets.popup.NLS_searching = "Zoeken...";
  esri.bundle.widgets.popup.NLS_noInfo = "Niets gevonden";
  esri.bundle.widgets.popup.NLS_nextFeature = "Volgende";
  esri.bundle.widgets.popup.NLS_prevFeature = "Vorige";
  esri.bundle.widgets.popup.NLS_maximize = "Maximaliseren";
  esri.bundle.widgets.popup.NLS_restore = "Vorig formaat";
  esri.bundle.widgets.popup.NLS_close = "Sluiten";
  esri.bundle.widgets.popup.NLS_zoomTo = "Zoom in op selectie";
   
        var initialExtent = new esri.geometry.Extent({"xmin":157741,"ymin":378600,"xmax":181240,"ymax":394100,"spatialReference":{"wkid":28992}});
  
  //setup the popup window  
        var popup = new esri.dijit.Popup({ 
          fillSymbol: new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,255,255]), 2), new dojo.Color([255,255,0,0])) 
        }, dojo.create("div"));
   
        map = new esri.Map("map",{
   infoWindow:popup, 
   extent: initialExtent,
   logo: false});
   
  dojo.addClass(map.infoWindow.domNode, "myTheme");//instellen dat de popup "myTheme" gebruikt
  dojo.connect(map, 'onLoad', mapReady);
  dojo.connect(map, "onLoad", function() {
   map.disableDoubleClickZoom();
  });
   
  basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://atlas.sre.nl/ArcGIS/rest/services/TOPOGRAFIE/OSM_2011_ZW/MapServer");
  map.addLayer(basemap);  

        layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://atlas.sre.nl/ArcGIS/rest/services/REGIO/lijst/MapServer", {opacity:.5});

        if (layer.loaded) {
          buildLayerList(layer);
        }
        else {
          dojo.connect(layer, "onLoad", buildLayerList);
        }
      }

      function buildLayerList(layer) {
        var items = dojo.map(layer.layerInfos,function(info,index){
          if (info.defaultVisibility) {
            visible.push(info.id);
          }
          return "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" + info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label><br>";
        });

        dojo.byId("layer_list").innerHTML = items.join("");

        layer.setVisibleLayers(visible);
        map.addLayer(layer);

      }

      function updateLayerVisibility() {
        var inputs = dojo.query(".list_item"), input;
    
        visible = [];

        dojo.forEach(inputs,function(input){
          if (input.checked) {
              visible.push(input.id);
          }
          });
        //if there aren't any layers visible set the array to be -1
        if(visible.length === 0){
          visible.push(-1);
        }
        layer.setVisibleLayers(visible);
      }

    function mapReady(map){ 
 
       dojo.connect(map,"onClick",executeIdentifyTask); 
        
       //create identify tasks and setup parameters  
       identifyTask = new esri.tasks.IdentifyTask("http://atlas.sre.nl/ArcGIS/rest/services/REGIO/lijst/MapServer"); 
        
       identifyParams = new esri.tasks.IdentifyParameters(); 
       identifyParams.tolerance = 10; 
       identifyParams.returnGeometry = true; 
       identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE; 
       identifyParams.width  = map.width; 
       identifyParams.height = map.height; 
        
        
       //resize the map when the browser resizes 
       dojo.connect(dijit.byId('map'), 'resize', map,map.resize); 
   
      } 
       
   function executeIdentifyTask(evt) { 
        identifyParams.geometry = evt.mapPoint; 
        identifyParams.mapExtent = map.extent; 
        
        var deferred = identifyTask.execute(identifyParams); 
 
        deferred.addCallback(function(response) {      
          // response is an array of identify result objects     
          // Let's return an array of features. 
          return dojo.map(response, function(result) { 
            var feature = result.feature; 
            feature.attributes.layerName = result.layerName; 
            if(result.layerName === 'Archeologie'){ 
              console.log(feature.attributes.PARCELID); 
              var template = new esri.InfoTemplate("", "<p><strong>Monumentnummer:</strong> ${MONUMENTNR}<br><strong>Waarde:</strong> ${WAARDE}<br><strong>Type:</strong> ${COMPLEX}<br><strong>Toponiem:</strong> ${TOPONIEM}<br><strong>Begin periode:</strong> ${BEGIN_PERI}<br><strong>Eind periode:</strong> ${EIND_PERIO}</p><p><strong>Toelichting:</strong><br/>${TOELICHTING}</p>"); 
              feature.setInfoTemplate(template);
            }
   else if(result.layerName === 'Monumenten'){ 
              console.log(feature.attributes.PARCELID); 
              var template = new esri.InfoTemplate("", "<p><strong>Monumentstatus:</strong> ${Monumentstatus}<br/><strong>Monumentnummer:</strong> ${Monumentnummer}<br/><strong>Aanduiding:</strong> ${Aanduiding}<br><strong>Adres:</strong> ${Adres}<br><strong>Postcode:</strong> ${Postcode}<br><strong>Gemeente:</strong> ${Gemeente}<br><strong>Bouwjaar:</strong> ${Bouwjaar}<br><strong>Architect:</strong> ${Architect}</p><p><strong><a id=info href=${html} target=${target}>Meer informatie</a></strong></p>"); 
              feature.setInfoTemplate(template); 
            } 
            else if (result.layerName === 'Rijksbeschermd stads/dorps-gezichten'){ 
              var template = new esri.InfoTemplate("", "<p><strong>Monumentstatus:</strong> Rijksbeschemd stads/dorps-gezicht<br/><strong>Naam: </strong>${NAAM}</p><p><strong><a id=info href=${KICHLINK} target=${target}>Meer informatie</a></strong></p>"); 
              feature.setInfoTemplate(template); 
            } 
            
            return feature; 
          }); 
        }); 
 
       
        // InfoWindow expects an array of features from each deferred 
        // object that you pass. If the response from the task execution  
        // above is not an array of features, then you need to add a callback 
        // like the one above to post-process the response and return an 
        // array of features. 
        map.infoWindow.setFeatures([ deferred ]); 
        map.infoWindow.show(evt.mapPoint); 
      }
 
      //show map on load 
      dojo.addOnLoad(init); 

    </script>


  </head>
  <body class="soria"> 
    <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" 
    style="width: 100%; height: 100%; margin: 0;"> 
      <div id="map" dojotype="dijit.layout.ContentPane" region="center">
  <div style="position:absolute; top: 5px; text-align: left; right:5px; z-index:50" id="layerlist">
     <strong>Kaartlagen:</strong><br>
   <span id="layer_list" ></span>
   <br>
   <span style="font-size:x-small;">Ondergrond: © <a href="http://www.openstreetmap.org" title="OpenStreetMap" target="_blank">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Creative Commons Attribution-ShareAlike" target="_blank">CC-BY-SA</a></span></div>
      </div> 
    </div> 
  </body>
</html>
0 Kudos
ShawnHolyoak
Occasional Contributor
I also have this issue.  I'm using deferreds to identify from multiple services, but even if a particular layer in the service is not visible, it is still identified.  My identifyParameters variable is set to
identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
.  ESRI, what gives?
0 Kudos
danbecker
Occasional Contributor III
I was having the same issue. identifyParams.LAYER_OPTION_VISIBLE would still identify layers that are hidden (not checked in the dijit.legend).

I *think* the LAYER_OPTION_VISIBLE pertains to the scale visibility, set in either the rest service or when the feat. layer is added to the map. So, if your at a scale within the layer visible scale range, it will identify, regardless if it's 'turned on' or not in the dijit.legend widget. I even tried adding LAYER_OPTION_VISIBLE param to the begining of the click event, so the layer visibility list would be 'more up to date' but it didn't work.

I did find that the identifyParams.layerIds does restrict the identify layers, so I simply added this to the beginning of the onClick function, prior to identifyTask.execute(identifyParams):

//lets manually set the identifyParams.layerIds
//to the layers that are currently checked in the legend widget
var lids = [];
dojo.forEach(legendLayers, function(layer){
    if (layer.layer.visible === true){
        lids.push(map.getLayer(layer.layer.id).layerId)
    }
});
identifyParams.layerIds = lids;
0 Kudos
SwenWaschk
New Contributor III
I was having the same issue. identifyParams.LAYER_OPTION_VISIBLE would still identify layers that are hidden (not checked in the dijit.legend).

I *think* the LAYER_OPTION_VISIBLE pertains to the scale visibility, set in either the rest service or when the feat. layer is added to the map. So, if your at a scale within the layer visible scale range, it will identify, regardless if it's 'turned on' or not in the dijit.legend widget. I even tried adding LAYER_OPTION_VISIBLE param to the begining of the click event, so the layer visibility list would be 'more up to date' but it didn't work.

I did find that the identifyParams.layerIds does restrict the identify layers, so I simply added this to the beginning of the onClick function, prior to identifyTask.execute(identifyParams):

//lets manually set the identifyParams.layerIds
//to the layers that are currently checked in the legend widget
var lids = [];
dojo.forEach(legendLayers, function(layer){
    if (layer.layer.visible === true){
        lids.push(map.getLayer(layer.layer.id).layerId)
    }
});
identifyParams.layerIds = lids;


Thanks I've added your code but it's still not working. It still identifies sublayers which are turned off in the layerlist. I thought it is something with the lids and the build layer list. But I can't really pinpoint it.
0 Kudos
danbecker
Occasional Contributor III
Thanks I've added your code but it's still not working. It still identifies sublayers which are turned off in the layerlist. I thought it is something with the lids and the build layer list. But I can't really pinpoint it.


i should have added, my map contains all featureLayers. I then add the layer definition info to a legendLayers array; pretty sure I got this from the visible layers sample.

var legendLayers = []
var parcels = new esri.layers.FeatureLayer("restURL",{
        mode:esri.layers.FeatureLayer.MODE_SNAPSHOT,
        id: 'parcels',
        outFields:["*"]
});
legendLayers.push({layer:parcels,title:"Property Boundaries"});


after the click event, you loop over the legendLayers array, checking to see which layers are visible; if visible, add the layerId to the lids array.

console.log(lids) will tell you what layerId's are currently visible
0 Kudos
AdrianMarsden
Occasional Contributor III
I had the same issue and resolved it like this.

I use this function from one of the samples

function updateLayerVisibility() {
var inputs = dojo.query(".list_item"), input;

visible = [];

dojo.forEach(inputs,function(input){
  if (input.checked) {
   visible.push(input.id);
  }
 
  });
//if there aren't any layers visible set the array to be -1
if(visible.length === 0){
  visible.push(-1);
}
dynamicMapServiceLayer.setVisibleLayers(visible);

}


I then added


identifyParams.layerIds = visible


In the relevant place and it all worked!

I was quite surprised

ACM
0 Kudos
SwenWaschk
New Contributor III
Thanks, I'm going to try it and I'll get back to you
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
I think you are experiencing [NIM042441 The layerOption LAYER_OPTION_VISIBLE not working as expected while using Identify Task.]. The LAYER_OPTION_VISIBLE seems only response to the default visible layers in the map service.

To workaround this, as amarsden did, please assign the list of visible layer to the IdentifyParameters.layerIds property, which can force the identify task to only perform the find operation on the  specified layers.
0 Kudos