Wednesday, 11 April 2018

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


This applies to ArcGIS Server 9.3 with the JavaScript API 2.4.


My goal is to click on the map and have the Identify task run on all visible Dynamic layers.


IdentifyTask requires a URL to the REST endpoint, with IdentifyParameters specifying which layerIds the identify operation should be performed on.


It seems the Identify tool is expecting all layers to be accessible from the same REST endpoint (ie, the same ArcGIS Server).


In my case, layers are being served from multiple ArcGIS Server instances - how can the Identify tool support this? For example, if the Buildings and Parcels layers in this map were coming from separate ArcGIS Servers.


(This is related to my previous question, but I realise now I need to answer this question first)



Answer



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 = "", var url2 = "";
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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...