Identifying layers from multiple ArcGIS Server instances using ArcGIS API for JavaScript 2?

First, here is a simplified JavaScript API example to show the concept of using DeferredList to process multiple identify tasks:

//Assume that map is your map object
var idTask1, idTask2, idParams = new esri.tasks.IdentifyParameters();
var url1 = "<server1 url>", var url2 = "<server2 url>";
dojo.connect(map, "onLoad", initIdentifies);
function initIdentifies(map) { //map.onLoad passes in the map object
    idTask1 = new esri.tasks.IdentifyTask(url1);
    idTask2 = new esri.tasks.IdentifyTask(url2);
    //A few sample constant parameters. Set more or less as you need
    idParams.tolerance = 12;
    idParams.returnGeometry = true;
    idParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
    dojo.connect(map, "onClick", runIdentifies);
}
function runIdentifies(evt) {
    var defTask1 = new dojo.Deferred(), defTask2 = new dojo.Deferred;
    var dlTasks = new dojo.DeferredList([defTask1, defTask2]);
    dlTasks.then(showResults); //defTasks will fire after defTask1 and defTask2 have completed
    //These parameters change with each request
    idParams.width = map.width;
    idParams.height = map.height;
    idParams.geometry = evt.mapPoint;
    idParams.mapExtent = map.extent;
    try {
        idTask1.execute(idParams, defTask1.callback, defTask1.errback); //Pass the response into the callback on defTask1
    } catch (e) {
        console.log("Error caught");
        console.log(e);
        defTask1.errback(e); //If you get an error, execute the errback
    }
    try {
        idTask2.execute(idParams, defTask2.callback, defTask2.errback); //Pass the response into the callback on defTask2
    } catch (e) {
        console.log("Error caught");
        console.log(e);
        defTask2.errback(e); //If you get an error, execute the errback
    }
}
function showResults(r) {
    //The format of 'r' is [[Boolean task 1 success, [task 1 results]],[Boolean task 2 success, [task 2 results]]]
    //using the array 'r', build and show your infoWindow as normal
}

Then here is an example in jsFiddle that I think does what you want, runs using all visible layers in all visible dynamic map layers in the map.

http://jsfiddle.net/blordcastillo/mULcz/

All typos are fixed now :)

The basic idea is that whenever the map is clicked or visibility is toggled, the identify is rerun. When the identify is run, the number of identity tasks fired depends on the number of layers visible, and it waits until all layers return to display its results.


The identify task can only reference one map service, so you will have to either:

  • Put all the layers you wish to run Identity on in one map service
  • Run multiple IdentifyTasks per map click

I ran into a similar situations with an app where I wanted to be able to identify on a DEM map service, and on a sea level rise result map service from a geoprocessing task. I chose to run two IdentifyTasks. The only thing you really have to add is figuring out when both tasks are done.

The basic flow is (this was using Silverlight/C#)

  • setup boolean variables for DEM and SLR identifyTaskcomplete
  • Run IdentifyTask for DEM
  • set bool for DEMidentifyTaskComplete to false
  • Run IdentifyTask for SLR (using same general parameters as for DEM)
  • set bool for SLRidentifyTaskComplete to false
  • In DEMIdentifyTask_ExecuteCompleted event listener, I set DEMidentifyTaskcomplete to true and then check to see if SLRidentifyTaskcomplete is true (set up the opposite for SLRIdentifyTask_ExecuteCompleted)
  • Whichever task finished last, both bools will be true, and calls IdentifyTasksComplete which parses both results into a custom graphic object which I add to the map, then set slr and demidentiftytaskcomplete to false