Select to view content in your preferred language

How to identify multiple layers from 3 services when visible?

753
3
01-25-2013 05:17 AM
CraigMcDade
Deactivated User
I have three services that I need to be identified, only when the layers are visible. Currently my code references 1 service. I have followed the steps outlined in this thread (I think), however, the app still identifies if a layer is turned off in the TOC. Link to my App.

I have also attempted to implement the code sample found here, without any luck.

I guess my question/issue is two pronged to try and get maximum understanding of the processes:
1. How can I make my code ID layers only when visible?
2. How can I add the additional services to my code and ID all three services only when visible?

My current code is:
dojo.require("esri.dijit.Popup"); //Infowindow
 
var identifyTask,identifyParams; 

function mapReady(map){

       dojo.connect(map,"onClick",executeIdentifyTask);
    
    //create identify tasks and setup parameters 
       identifyTask = new esri.tasks.IdentifyTask("https://www.marioncountyfl.org/arcgis/rest/services/Dynamic/Zoning/MapServer");
       
       identifyParams = new esri.tasks.IdentifyParameters();
       identifyParams.tolerance = 3;
       identifyParams.returnGeometry = true;
     //Choose which layers in the array you would like to be ID'd in the popup  
    identifyParams.layerIds = [0,1,2,3,4];
       identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
       identifyParams.width  = map.width;
       identifyParams.height = map.height;
    }   
   function executeIdentifyTask(evt) {
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = map.extent;
    identifyParams.layerIds = visible
       
        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 === 'Zoning Classifications'){
              console.log(feature.attributes.NAME);
              var template = new esri.dijit.PopupTemplate({
       title:"Zoning",
       description:"<b>Zoning:</b> {Zoning Classification} <br/> <b>Description:</b> {Zoning Description}"
       });
              feature.setInfoTemplate(template);
            }
            else if (result.layerName === 'Assisted Living Facilities'){
              var template = new esri.dijit.PopupTemplate({
       title:"Assisted Living Facilities",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Applicant:</b> {BusinessApplicantName} <br/> <b>No. of Residents</b> {NbrResidents}"
       });
              feature.setInfoTemplate(template);
            }
      else if (result.layerName === 'Family Divisions'){
              var template = new esri.dijit.PopupTemplate({
       title:"Family Divisions",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Project Number:</b> {project_nbr} </br> <b>Status:</b> {Status}"
       });
              feature.setInfoTemplate(template);
            }
      else if (result.layerName === 'Policy 120'){
              var template = new esri.dijit.PopupTemplate({
       title:"Policy 120",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Business Name:</b> {BusinessName} </br> <b>Date of Letter:</b> {DateOfLetter}"
       });
              feature.setInfoTemplate(template);
            }
      else if (result.layerName === 'Zoning Changes'){
              var template = new esri.dijit.PopupTemplate({
       title:"Zoning Changes",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Request Number:</b> {REQUEST_NBR} </br> <b>Property Owner:</b> {PROPERTY_OWNER}"
       });
              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);
      }
0 Kudos
3 Replies
CraigMcDade
Deactivated User
I've made some decent progress on my problem. I  have now added a second service that can be identified. I'm still running into some issues that I hope somebody might be able to help with.

The popup is only returning one of the services at full scale (the zoning service), however, if I zoom in to where the parcel service is visible it will only return the parcel service. How can I get it so that it returns both results with one click? Ultimately I only want it to return results when the layer is visible in the TOC.

My new code:
dojo.require("esri.dijit.Popup"); //Infowindow
 
var identifyTask,identifyParams; 

      function mapReady(map){

       dojo.connect(map,"onClick",executeIdentifyTask);
       
       //create identify tasks and setup parameters 
       identifyTaskZoning = new esri.tasks.IdentifyTask("https://www.marioncountyfl.org/arcgis/rest/services/Dynamic/Zoning/MapServer");
       identifyTaskParcels = new esri.tasks.IdentifyTask("https://www.marioncountyfl.org/arcgis/rest/services/Dynamic/Parcels/MapServer");
       
   //Set Zoning Parameters
       identifyParamsZoning = new esri.tasks.IdentifyParameters();
       identifyParamsZoning.tolerance = 7;
       identifyParamsZoning.returnGeometry = true;
       identifyParamsZoning.layerIds = [0,1,2,3,4];
       identifyParamsZoning.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
       identifyParamsZoning.width  = map.width;
       identifyParamsZoning.height = map.height;
   //Set Parcel Parameters
       identifyParamsParcels = new esri.tasks.IdentifyParameters();
       identifyParamsParcels.tolerance = 15;
       identifyParamsParcels.returnGeometry = true;
       identifyParamsParcels.layerIds = [0,1,2,3,4];
       identifyParamsParcels.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
       identifyParamsParcels.width  = map.width;
       identifyParamsParcels.height = map.height;
       
       //resize the map when the browser resizes
       dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
      };

   function executeIdentifyTask(evt){
     identifyParamsParcels.geometry = evt.mapPoint;
     identifyParamsParcels.mapExtent = map.extent;
     var deferred = identifyTaskParcels.execute(identifyParamsParcels);
     deferred.addCallback(function(response){
       if (response.length > 0) {
//         console.log(response.length)
//         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 === 'TaxParcel'){
              console.log(feature.attributes.NAME);
              var template = new esri.dijit.PopupTemplate({
       title:"Parcels",
       description:"<b>Parcel ID:</b> {Parcel Identification Number} <br/> <b>Address:</b> {Site Address}"
       });
              feature.setInfoTemplate(template);
            }
        
    return feature;
        });
        }
      else {
          identifyParamsZoning.geometry = evt.mapPoint;
          identifyParamsZoning.mapExtent = map.extent;
          var deferred = identifyTaskZoning.execute(identifyParamsZoning);
          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 === 'Zoning Classifications'){
              console.log(feature.attributes.NAME);
              var template = new esri.dijit.PopupTemplate({
       title:"Zoning",
       description:"<b>Zoning:</b> {Zoning Classification} <br/> <b>Description:</b> {Zoning Description}"
       });
              feature.setInfoTemplate(template);
            }
            else if (result.layerName === 'Assisted Living Facilities'){
              var template = new esri.dijit.PopupTemplate({
       title:"Assisted Living Facilities",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Applicant:</b> {BusinessApplicantName} <br/> <b>No. of Residents</b> {NbrResidents}"
       });
              feature.setInfoTemplate(template);
            }
      else if (result.layerName === 'Family Divisions'){
              var template = new esri.dijit.PopupTemplate({
       title:"Family Divisions",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Project Number:</b> {project_nbr} </br> <b>Status:</b> {Status}"
       });
              feature.setInfoTemplate(template);
            }
      else if (result.layerName === 'Policy 120'){
              var template = new esri.dijit.PopupTemplate({
       title:"Policy 120",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Business Name:</b> {BusinessName} </br> <b>Date of Letter:</b> {DateOfLetter}"
       });
              feature.setInfoTemplate(template);
            }
      else if (result.layerName === 'Zoning Changes'){
              var template = new esri.dijit.PopupTemplate({
       title:"Zoning Changes",
       description:"<b>Parcel ID:</b> {Parcel} <br/> <b>Request Number:</b> {REQUEST_NBR} </br> <b>Property Owner:</b> {PROPERTY_OWNER}"
       });
              feature.setInfoTemplate(template);
            }
            return feature;
            });
          });
      };
        map.infoWindow.setFeatures([deferred]);
        map.infoWindow.show(evt.mapPoint);          
      });
        map.infoWindow.setFeatures([deferred]);
        map.infoWindow.show(evt.mapPoint);      
      };
0 Kudos
DavidBoyd
Emerging Contributor
Craig - I'm having the exact same problem with layers displaying identify results when turned off in the TOC (using the TOc widget).  Other posts suggesting adding "identifyParams.layerIds = visible" to the initfunctionality parameters, but this doesn't work and seems to disable the identify function all together.  If you figured this out or found a work around I'd love to hear about it.

I'm also trying to exclude the contentpanes from the tabcontainer when identify results are null.  This is probably a separate issue with a possible separate fix.

Thanks, Dave
0 Kudos
AndyBurns
Regular Contributor
Hi

This might help as im doing something similar by activating only the layers i want from a url paramater ie layers=1,2,3,4,5. Its different from your requirements but might help with trying to pass the layer id's yu want to the service.

Firstly i setup my dynamic service with a variable - lbsr in this case.

lbsr = new esri.layers.ArcGISDynamicMapServiceLayer("http://sccw08ags/ArcGIS/rest/services/APILBSR/MapServer", {
                    "opacity": 0.7
                });  
    


then i see if the layers url parameter is there, the xlgsl code is just a case loop where i have hardcoded lgslids to layer id's but this can be dynamic -

// Do we have an xlgsl code in the url? 
      if (urlObject.query.lgsl) {
        var lklgsl = urlObject.query.lgsl;
        layers = matchLgsl(lklgsl);
        console.log("lgsl code:", lklgsl);
      }
                        //set the visable layers that where passed in the url specified by the lgsl code and if it has matched the matchLgsl function
                        lbsr.setVisibleLayers(layers);
       console.log("Layer ID's being drawn", layers);
                    }


then all i do is pass the layers var as a parameter to my identify function -

//pass the layers var to the setUp function to query the data
                    setUpIdentify(layers);


function setUpIdentify(layers) {
   //when user clicks on map, call doIndentify()
   //map.graphics.clear();
   dojo.connect(map, "onClick", doIdentify); 
            console.log("Setting up identify");
   identifyTask = new esri.tasks.IdentifyTask("http://sccw08ags/ArcGIS/rest/services/APILBSR/MapServer");
   identifyParams = new esri.tasks.IdentifyParameters();
   //identifyParams.spatialReference = new esri.SpatialReference({ wkid: 27700});
   identifyParams.tolerance = 5;
   identifyParams.returnGeometry = true;
   identifyParams.layerIds = layers;
0 Kudos