This is the way I add the layer name (and service URL) to my IdentifyTask results for multiple services
function runIdentifies(evt) {
var idPoint = evt.mapPoint;
var layers = arrayUtils.map(map.layerIds, function (layerId) {
return map.getLayer(layerId);
});
//this filters out the basemaps and layers with no visible layers
layers = arrayUtils.filter(layers, function (layer) {
if (layer.visibleLayers !== undefined) {
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 defTasks = arrayUtils.map(tasks, function (task) {
return new Deferred();
});
var promises = [];
for (var i = 0; i < tasks.length; i++) {
promises.push(tasks.execute(params));
}
var allPromises = new all(promises);
allPromises.then(function (r) { showIdentifyResults(r, tasks, idPoint); });
}
function createIdentifyParams(layers, evt) {
var identifyParamsList = [];
arrayUtils.forEach(layers, function (layer) {
var idParams = new IdentifyParameters();
idParams.width = map.width;
idParams.height = map.height;
idParams.geometry = evt.mapPoint;
idParams.mapExtent = map.extent;
idParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE;
idParams.layerIds = layer.visibleLayers;
idParams.tolerance = 3;
idParams.returnGeometry = true;
idParams.spatialReference = map.spatialReference;
identifyParamsList.push(idParams);
});
return identifyParamsList;
}
function showIdentifyResults(r, tasks, idPoint) {
var results = [];
var taskUrls = [];
r = arrayUtils.filter(r, function (results) {
return r[0];
});
for (var i = 0; i < r.length; i++) {
results = results.concat(r);
for (var j = 0; j < r.length; j++) {
taskUrls = taskUrls.concat(tasks.url);
}
}
var formatResults = arrayUtils.map(results, function (result, index) {
var feature = result.feature;
//this adds new attributes to the feature for the layerName and service URL
feature.attributes.layerName = result.layerName;
feature.attributes.taskUrl = taskUrls[index];
return feature;
});
if (formatResults.length > 0) {
//this is a separate function to put the results into a TabContainer with each service on its own tab
if (buildTabContainer(formatResults)) {
map.infoWindow.show(idPoint);
}
}
}