Solved! Go to Solution.
app.map.on("click", runIdentifies);
// FUNCTION FOR IDENTIFYING MULTIPLE LAYERS AT ONCE //manually define which layers to do the identify on based on visible Layers function createIdentifyParams(layers,evt){ identifyParamsList.length = 0; arrayUtils.forEach(layers, function (layer) { var idParams = new IdentifyParameters(); idParams.width = app.map.width; idParams.height = app.map.height; idParams.geometry = evt.mapPoint; idParams.mapExtent = app.map.extent; idParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE; var visLayers = layer.visibleLayers; if (visLayers !== -1) { //TOC lets the user change visibility, layerIDs must be explicitly defined idParams.layerIds = layer.visibleLayers; }else { idParams.layerIds = []; } idParams.tolerance = 3; idParams.returnGeometry = true; idParams.spatialReference = spatialReference; identifyParamsList.push(idParams); }); return identifyParamsList; } function runIdentifies(evt) { idPoint = evt.mapPoint; app.map.infoWindow.clearFeatures(); var layers = arrayUtils.map(app.map.layerIds, function(layerId) { return app.map.getLayer(layerId); }); layers = arrayUtils.filter(layers, function(layer) { if (layer.visibleLayers[0] !== -1){ return layer.getImageUrl && layer.visible } }); var params = createIdentifyParams(layers,evt); var tasks = arrayUtils.map(layers, function(layer) { return new IdentifyTask(layer.url); }); var IdentifyTaskList = arrayUtils.map(layers, function(layer, index) { task = new IdentifyTask(layer.url); return task.execute(params[index]) //assume you have a list of identifyParameters }); all(IdentifyTaskList).then(showIdentifyResults); } function showIdentifyResults(idResults){ var results = []; idResults = arrayUtils.filter(idResults, function (result) {//filter out any failed tasks return idResults[0]; }); for (i=0;i<idResults.length;i++) { //combines identifyResults var lyrResultLen = idResults.length; var lyrResult = idResults; for (j = 0; j < lyrResultLen; j++) { var featRes = lyrResult; results.push(featRes); } } formatResults = arrayUtils.map(results, function(result){ var feature = result.feature; var layerName = result.layerName; feature.attributes.layerName = result.layerName; feature.setInfoTemplate(generateInfoTemplate); generateInfoTemplate.setTitle("Layer Information"); return feature; }); if (formatResults.length > 0) { app.map.infoWindow.setFeatures(formatResults); app.map.infoWindow.show(idPoint); } }
var legisDistAtt = ['OBJECTID','District']; var prevDistAtt = ['OBJECTID','ID']; var countyAtt = ['OBJECTID', 'COUNTYNAME'];
generateInfoTemplate = new InfoTemplate(); generateInfoTemplate.setTitle("Layer Information"); generateInfoTemplate.setContent(generateWindowContent); function generateWindowContent(graphic) { var layerName = graphic.attributes.layerName;//case sensitive field names var fieldNamesArr = []; for (fieldName in graphic.attributes) { if (graphic.attributes.hasOwnProperty(fieldName) && fieldName !=='Shape' && fieldName !== 'Shape.area' && fieldName !== 'Shape.len') { fieldNamesArr.push(fieldName); } } var attString = ""; switch (layerName) { case "House District": attString = createAttRows(graphic, legisDistAtt); break; case "Senate District": attString = createAttRows(graphic, legisDistAtt); break; case "Congressional District": attString = createAttRows(graphic, legisDistAtt); break; case "County": attString = createAttRows(graphic, countyAtt); break; default: attString = createAttRows(graphic, fieldNamesArr); } return "<h4>"+ layerName +"</h4>" +"<table id='infoWindowTable' data-dojo-type='dojox/grid/DataGrid' data-dojo-props='class:'infoTable'' ><tr><th></th><th></th></tr>" +attString+ "</table>"; } function createAttRows(graphic, attArray) { var returnString = ""; arrayUtils.forEach(attArray, function (attName){ if (attName !== 'OBJECTID' && attName !== 'layerName') { returnString = returnString + "<tr><td><b>"+attName+"</b></td><td>" + graphic.attributes[attName]+ "</td></tr>"; } }); return returnString; }
educationLayer = new ArcGISDynamicMapServiceLayer(pathName+"/ArcGIS/rest/services/BaseMap/Education/MapServer", {id:'educationLayer', visible:false})
var layers = arrayUtils.map(app.map.layerIds, function(layerId) { return app.map.getLayer(layerId); });
var visLayers = layer.visibleLayers; if (visLayers !== -1) { //TOC lets the user change visibility, layerIDs must be explicitly defined idParams.layerIds = layer.visibleLayers; }else { idParams.layerIds = []; }
Does ESRI or any Javascript developer know if this NIM has been fixed at any version of AGS v10.2.x or possibly at v10.3.0? It would be nice for the code to work as advertised in the documentation instead of resorting to workarounds.
Michael it does not seem likely ESRI will change it. Ultimately it would be good to see ESRI provide an Identify widget in the API that tracked visible layers and had capability to identify all visible layers. But for now you must build your own widget. I built an Identify that does this (with much help from Tracy, Adrian and everyone else here..thank you all). It works with the AGS JS TOC Widget mentioned here: MapViewer - Savannah Area GIS
Feel free to use all the code. Just let me know if you find any bugs.
Simple way to get visiblelayerids and assign to identify parameters.
var FilteredLayerIds = app.ElectricGrid_Service.visibleLayers.slice(0);
identifyParams.layerIds = FilteredLayerIds;