How to know which dynamc map service the visible layer cam from in Nliu's TC widget

817
6
11-04-2014 09:04 AM
ZhujingXia
Occasional Contributor II

I am using Nliu's TC widget in my application, it is great.  When I used the identifyTask to get the info for the visible layers, I could only identify layers from one map service. I used the code as following.

identifyParameters.layerIds =  dynamicMapServiceLayer.visibleLayers;

It seems I have to manually assigned which map service I am going to use. I have multiple map services show in the TOC. Is there a way could know that which map service the visible layer belong to?

Thanks

Zhujing

0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor

Take a look at the code in this post. It contains multiple map services that are utilized in the identifyTask.

0 Kudos
ZhujingXia
Occasional Contributor II

Ken,

I saw the this sample code before and tried it.

I could get result back from this code for the layers:

var layers = dojo.map(map.layerIds, function (layerId) {

return map.getLayer(layerId);

});

But I run into error for this code , no result for layer, the layer is undefined. Do you know why is that?

layers = dojo.filter(layers, function (layer) {

if (layer.visibleLayers[0] !== -1) {

return layer.getImageUrl && layer.visible

}

});

Zhujing

>>> Ken Buja <geonet@esri.com> 11/5/2014 2:14 PM >>>

Ken Buja replied to the discussion

"How to know which dynamc map service the visible layer cam from in Nliu's TC widget"

To view the discussion, visit: https://community.esri.com/message/435667?et=watches.email.thread#435667

0 Kudos
KenBuja
MVP Esteemed Contributor

Can you post your code? This sample is working correctly here.

0 Kudos
ZhujingXia
Occasional Contributor II

I have a big project, here is my identfy.js file It could only identify one map service.

var map_click_handle_Identify; //event procedure for map-onClick

var map_identify_first_time = true;

var mapClickX = 0;

var mapClickY = 0;

var identifyTreeDisplayData = [];

var selectedFeature;

var myFeatureScroll;

var myIdentifyScroll;

//Identify tool

function initIdentifyTool() {

//currently nothing needs to be initialized

}

function setActiveToolIdentify() {

//attach identify tool to map click event

map_click_handle_Identify = dojo.connect(map, "onClick", doIdentify);

map.setMapCursor("crosshair");

}

function ShowIdentifyPanel() {

$('#identifywindow').dialog({

title: "Map Identify Tool",

show: { effect: "fade", duration: 250 },

hide: ,

modal: false,

//position: ,

height: 320,

width: 470,

resizable: true,

closeOnEscape: false,

close: function () { },

resize: function (event, ui) {

var fpwidth = $("#identifywindow_featurepanel_wrapper").width();

$("#identifywindow_attributepanel").css({ "left": String(fpwidth + 10) + "px", "width": "auto" });

//update iscroll

$("#identifywindow_featurepanel").css({ "min-height": String($("#identifywindow_featurepanel_wrapper").height()) + "px" });

myFeatureScroll.refresh();

$("#scroller").css({ "min-height": String($("#wrapper").height() + 3) + "px" });

myIdentifyScroll.refresh();

}

});

if (map_identify_first_time) {

//set window position on first time only

$('#identifywindow').dialog({ position: { my: "right top", at: "right-25 top+100", collision: "flipfit"} });

map_identify_first_time = false;

}

}

//capture map click event - select parcel

var lastCalledIdentifyTimestamp = new Date();

function doIdentify(evt) {

var thisCallTimestamp = new Date();

dmsg(thisCallTimestamp - lastCalledIdentifyTimestamp);

if ((thisCallTimestamp - lastCalledIdentifyTimestamp) < 500) { //on mobile, screen touch may fire up twice - use timer to eliminate the second request just in case

lastCalledIdentifyTimestamp = thisCallTimestamp;

return;

}

lastCalledIdentifyTimestamp = thisCallTimestamp;

mapClickX = evt.mapPoint.x;

mapClickY = evt.mapPoint.y;

if (cfg_multiLayerIdentifyToolEnabled == 1 || cfg_featureToolEnabled == 1) {

dmsg('5.1 on map clicked');

showLoading();

map.graphics.clear();

if (cfg_multiLayerIdentifyToolEnabled == 1) {

require(["esri/tasks/IdentifyTask", "esri/tasks/IdentifyParameters"], function (IdentifyTask, IdentifyParameters) {

ShowIdentifyPanel();

//alert('not implemented');

if (map.getScale() > cfg_identifyMinScale) {

jsapimessage("Please zoom in closer.");

hideLoading();

return;

}

var identifyParameters = new IdentifyParameters();

identifyParameters.geometry = evt.mapPoint;

identifyParameters.returnGeometry = true;

identifyParameters.mapExtent = map.extent;

identifyParameters.height = map.height;

identifyParameters.width = map.width;

identifyParameters.spatialReference = map.spatialReference;

//identifyParameters.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE; //this will take default visible layers from MXD, ignoring current state in the TOC control

identifyParameters.layerOption = IdentifyParameters.LAYER_OPTION_ALL;

identifyParameters.layerIds = dynamicMapServiceLayer.visibleLayers;

identifyParameters.tolerance = cfg_identifyTolerance;

if (cfg_tocEnabled == 1) {

//if toc control is enabled, the default visibility may have changed, the option LAYER_OPTION_VISIBLE may not represent currrent visible layers

//identifyParameters.layerIds = map.getLayer("dynamicmap").visibleLayers;

}

var identifyServer = cfg_identifyServerURL.checkAppendToken();

var identifyTask = new IdentifyTask(identifyServer);

identifyTask.execute(identifyParameters, function (idResults) { processIdentifyResult(idResults, evt); }, identifyError);

});

} else if (cfg_featureToolEnabled == 1) {

require(["esri/tasks/QueryTask", "esri/tasks/query", "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol"], function (QueryTask, Query, SimpleFillSymbol, SimpleLineSymbol) {

var queryServer = (cfg_queryServerURL + "/" + kv[cfg_featureToolLayerName.toUpperCase()].toString()).checkAppendToken();

var queryTask = new QueryTask(queryServer);

var query = new Query();

query.returnGeometry = true;

query.outFields = ["*"];

query.geometry = evt.mapPoint;

query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;

queryTask.execute(query, function (featureSet) {

if (featureSet.features.length == 0) {

jsapimessage("Nothing is selected.");

hideLoading();

} else {

//get feature

var graphic = featureSet.features[0];

//setup graphic layer and symbol

map.graphics.clear();

var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_NULL, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color(), 4), new dojo.Color());

//add graphic to map

graphic.setSymbol(symbol);

map.graphics.add(graphic);

//get extent

var ext

var geometry = graphic.geometry;

var ext = getExtentFromGeometry(geometry);

var featurekey = graphic.attributes[cfg_featureToolLayerKeyField];

executeFeatureFunction(featurekey);

hideLoading();

}

});

});

}

dmsg('5.2 on map clicked done');

} else {

alert("Identify tool and/or feature tool has not been configured for this application.");

}

}

function identifyError(error) {

hideLoading();

alert('There was an error executing identify task.\n' + error.get_message());

}

function processIdentifyResult(idResults, evt) {

//array variable to hold all known layer names

var layerNameList = [];

//reset result object, create root node

identifyTreeDisplayData.length = 0;

identifyTreeDisplayData.push({ id: "Identify Layers", name: "Layer Name", features: "" })

//clear existing content in attribute pabel

$("#identifywindow_featurepanel").html("");

//check if identify result contains any feature

if (idResults.length == 0) {

$("#identifywindow_featurepanel").html("

No features found.

");

hideLoading();

return;

}

for (var i = 0, il = idResults.length; i < il; i++) {

idResult = idResults;

if (layerNameList.length == 0 || layerNameList.indexOf(idResult.layerName) == -1) { //check if we need to insert a new layer node

layerNameList.push(idResult.layerName); //add this layer to list of known layer

identifyTreeDisplayData.push({ id: idResult.layerName, name: idResult.layerName, features: "", parent: "Identify Layers" }); //insert layer node into identifyTreeDisplayData

}

identifyTreeDisplayData.push(); //insert feature into identifyTreeDisplayData

}

require([

"dojo/_base/window",

"dojo/store/Memory",

"dijit/tree/ObjectStoreModel",

"dijit/Tree",

"dojo/domReady!"],

function (win, Memory, ObjectStoreModel, Tree) {

var identifyStore = new Memory({

data: identifyTreeDisplayData,

getChildren: function (object) {

return this.query({ parent: object.id }); //return all elements in which element.parent = this-object.id

}

});

var identifyModel = new ObjectStoreModel(

});

//clear out existing identify tree object

var oldtree = dijit.byId('identifyTree');

if (oldtree) {

oldtree.destroy();

}

var identifyTree = new Tree(,

autoExpand: true,

onClick: identifyTreeOnClick,

style: "overflow: hidden;"

});

identifyTree.placeAt("identifywindow_featurepanel").startup();

identifyTree.on("load", function () {

if (myFeatureScroll != null) {

myFeatureScroll.refresh();

} else {

myFeatureScroll = new iScroll('identifywindow_featurepanel_wrapper', { hScrollbar: false, hideScrollbar: false });

}

});

// Automaticalyl select the first feature (node #3 in the tree) --> Root Node \ First Layer Node \ First Feature Node

identifyTree.onLoadDeferred.then(function () {

if (identifyTreeDisplayData.length > 2) {

identifyTree.set("path", [identifyTreeDisplayData[0].id, identifyTreeDisplayData[1].id, identifyTreeDisplayData[2].id]);

var selectedNode = identifyTreeDisplayData[2];

identifyTreeOnClick(selectedNode);

hideLoading();

}

});

});

}

function identifyTreeOnClick(item) {

if (!item.root) {

$("#identifywindow_attributepanel").empty();

if (item.features !== "") { //check to make sure this is a feature node, not a layer node

selectedFeature = item;

var content = "";

content += "

" + item.id + ""
//header row closing tags
content += "

";

content += "

";
content += "

";

//attribute fields
dojo.forEach(LayersFieldName, function (layer) {
if (layer.layerName === item.parent) {
dojo.forEach(layer.layerFields, function (field) {
if ($.inArray(field.name.toUpperCase(), cfg_identifyFieldToHide) == -1 && $.inArray(field.alias.toUpperCase(), cfg_identifyFieldToHide) == -1) {
var fv = item.features.attributes[field.alias];
if (fv.toUpperCase().startsWith('HTTP://') || fv.toUpperCase().startsWith('HTTPS://')) {
fv = String.format("

",fv)
}
content += "

";
content += "

" + field.name + "

";
content += "

" + fv + "

";
content += "

";
content += "

";
}
});
}
});

//end table
content += ""; //scroller
content += ""; //wrapper

//end table

//apply result html to attribute panel

$("#identifywindow_attributepanel").html(content);

myIdentifyScroll = new iScroll('wrapper', );

}

}

}

function hyperLinkField(item, field) {

var content = item.features.attributes[field.alias];

dojo.forEach(ConfigLayersWithAttachment, function (layerWithAttachment) {

if (item.parent == layerWithAttachment.layerName && field.name == layerWithAttachment.fieldName) {

content = "" + content + "";

}

});

return content;

}

function zoomToIdentifyFeature() {

var layerID = selectedFeature.layerId;

if (layerID == undefined) return;

var objectID = selectedFeature.features.attributes.OBJECTID;

//initialize query task

require(["esri/tasks/QueryTask", "esri/tasks/query"], function (QueryTask, Query) {

var queryTask = new QueryTask((cfg_dynamicMapLayerURL + "/" + layerID.toString()).checkAppendToken());

var query = Query();

query.returnGeometry = true;

query.outSpatialReference = map.spatialReference;

query.outFields = ["*"];

query.where = "OBJECTID ='" + objectID + "'";

queryTask.execute(query, showIdentifyResult);

});

}

function showIdentifyResult(featureSet) {

var resultFeatures = featureSet.features;

map.graphics.clear()

require(["esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleMarkerSymbol", ], function (SimpleFillSymbol, SimpleLineSymbol, SimpleMarkerSymbol) {

var myFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color(), 4), new dojo.Color());

var myLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color(), 4);

var myMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color(), 1), new dojo.Color());

dojo.forEach(resultFeatures, function (feature) {

TempGraphic = feature;

switch (feature.geometry.type) {

case "point":

case "multipoint":

TempGraphic.setSymbol(myMarkerSymbol);

agsjsZoomToLocationXY(feature.geometry.x, feature.geometry.y, cfg_ds_point_zoom_width);

break;

case "line":

case "polyline":

TempGraphic.setSymbol(myLineSymbol);

var extent = feature.geometry.getExtent().expand(cfg_ds_polygon_zoom_factor);

break;

default:

TempGraphic.setSymbol(myFillSymbol);

var extent = feature.geometry.getExtent().expand(cfg_ds_polygon_zoom_factor);

break;

}

map.setExtent(extent, true);

map.graphics.add(TempGraphic);

return;

});

});

}

>>> Ken Buja <geonet@esri.com> 11/5/2014 2:49 PM >>>

Ken Buja replied to the discussion

"How to know which dynamc map service the visible layer cam from in Nliu's TC widget"

To view the discussion, visit: https://community.esri.com/message/435669?et=watches.email.thread#435669

0 Kudos
ZhujingXia
Occasional Contributor II

if i add this code into  function doIdentify(evt)

var layers = dojo.map(map.layerIds, function (layerId) { 

                    return map.getLayer(layerId); 

                });  //I could get result back for layers

var layers = dojo.map(map.layerIds, function (layerId) { 

                    return map.getLayer(layerId); 

                });  //But I run into error for this code , no result for layer, the layer is undefined.

0 Kudos
YungKaiChin
Occasional Contributor

I know nothing about the widget, but I can think of a possible solution/walkaround.

1. Create an Array of all layers (IDs) used by the widgets.

2. When needed, loop through the array, and find what is visible (layer.visible || layer.visibleAtMapScale);

0 Kudos