Select to view content in your preferred language

Selecting specific layer from multiple different layers

1385
9
04-01-2013 01:05 PM
StephenFricke
Deactivated User
Hey all,

I have a map service that currently has multiple layers.  I would like to be able to have all of these layers turned off by default, and then allow the user to turn on whichever layer they would like, and then be able to select this layer, which is then used as the clipping geometry in a geoprocessing task.  Right now in my code I am able to select a layer, by creating a query task and then hard coding the layer I want to query:

var queryTask = new esri.tasks.QueryTask("TheURLwhereMyLayerIs/MapServer/1");

This problem is, when I have multiple layers within a map service, I want this line to change so that I will be querying whichever layer is turned on in the map service, not just some layer that I have hard coded.  So the two things I need to figure out, how can all of the layers in the map service be turned OFF by default, and how can I insert the name of the layer which is turned on into this line for the layer that should be queried in the query task?  Any help would be much appreciated!  Thank you!
0 Kudos
9 Replies
JohnGravois
Deactivated User
the ArcGISDynamicMapServiceLayer.visibleLayers property will return an array of the layerIds which are currently visible.  you could loop through those results and pass to a series of QueryTasks, but if the client needs to interact with the geometry from all these individual layers (and perhaps even send a featureSet back to ArcGIS Server), maybe it would be more convenient to work with FeatureLayers and skip the Querying altogether instead?
0 Kudos
StephenFricke
Deactivated User
the ArcGISDynamicMapServiceLayer.visibleLayers property will return an array of the layerIds which are currently visible.  you could loop through those results and pass to a series of QueryTasks, but if the client needs to interact with the geometry from all these individual layers (and perhaps even send a featureSet back to ArcGIS Server), maybe it would be more convenient to work with FeatureLayers and skip the Querying altogether instead?


Hey thanks for the reply, I guess I may have not specified my question correctly, but I am working with feature layers.  I use the query task to select the layer in the map based on the point I click on in the map:

var queryTask = new esri.tasks.QueryTask("http://inside-dev2.nkn.uidaho.edu:6080/arcgis/rest/services/REACCH/REACCHBase/MapServer/1");
var query = new esri.tasks.Query();
query.returnGeometry = true;
query.outSpatialReference = map.spatialReference;

map.graphics.clear();
query.geometry = evt.mapPoint;
query.outFields = ["*"];
query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_INTERSECTS;
queryTask.execute(query);

There may be another way to go about this, but this is what I have gotten to work and what I am comfortable with.  The problem is, I want to be able to potentially select from multiple different feature layers.  So the var queryTask would not just be that feature layer that I have hard coded in, but whichever feature layer that is turned on in the map service.  I am not sure how to write the code so that whichever feature layer that is turned on is passed to this var queryTask.  Let me know if you have any ideas.  Thanks again!
0 Kudos
JohnGravois
Deactivated User
thanks for clarifying.  in this case you shouldn't need to fire a QueryTask to ask the server about a feature under a mouse click because the geometry and attributes of the features have already been downloaded to the client.

Because FeatureLayers inherit from GraphicsLayers, the GraphicsLayer "onClick" event will give you access to the feature itself (along with screenPoint and mapPoint).
0 Kudos
StephenFricke
Deactivated User
thanks for clarifying.  in this case you shouldn't need to fire a QueryTask to ask the server about a feature under a mouse click because the geometry and attributes of the features have already been downloaded to the client.

Because FeatureLayers inherit from GraphicsLayers, the GraphicsLayer "onClick" event will give you access to the feature itself (along with screenPoint and mapPoint).


Hey John, thanks for the reply, but I guess I am not understanding your solution.  I want to be able to select a clipping geometry, which is the polygon that I click on(feature layer in the map service) which is then sent as a parameter in a geoprocessing task. The way my code currently works, this feature layer that I click on is then sent to the geoprocessing task as the clipping extent for a dataset.  So for example, if the feature layer is all states in the US, and someone clicks on Nebraska, the dataset would then be clipped by the state of Nebraska.  The problem is there may be multiple feature layers which someone might want to clip by, county boundaries, watersheds, etc.  I want the user to be able to turn on a specific feature layer, whatever it may be, and then which feature layer is turned on is going to be the one that is queried in the onclick event.  So basically I need to find a way for this var queryTask to be whichever layer is turned on in the map service, rather than just one of the layers that I hard coded in.
0 Kudos
JohnGravois
Deactivated User
is the feature you are trying to use as clipping geometry being drawn in the app as part of a ArcGISDynamicMapServiceLayer, as a FeatureLayer, or in some other way?
0 Kudos
StephenFricke
Deactivated User
is the feature you are trying to use as clipping geometry being drawn in the app as part of a ArcGISDynamicMapServiceLayer, as a FeatureLayer, or in some other way?


It is being drawn in the map as a feature layer.
0 Kudos
JohnGravois
Deactivated User
to use your example, if you are loading the US states as a feature layer there is no need to wire up a QueryTask when the map is clicked because the FeatureLayer has already ensured that the graphics have already been downloaded to the client.

instead, writing code which listens for the "onClick" event of the FeatureLayer will give you immediate access to the graphic which has been clicked.

dojo.connect(myFeatureLayer, "onClick", myFunction);

function myFunction(evt) {
  console.log(evt.graphic);
}
0 Kudos
StephenFricke
Deactivated User
to use your example, if you are loading the US states as a feature layer there is no need to wire up a QueryTask when the map is clicked because the FeatureLayer has already ensured that the graphics have already been downloaded to the client.

instead, writing code which listens for the "onClick" event of the FeatureLayer will give you immediate access to the graphic which has been clicked.

dojo.connect(myFeatureLayer, "onClick", myFunction);

function myFunction(evt) {
  console.log(evt.graphic);
}


Hey, thanks for the info.  So this evt.graphic will be the State that I click on, without having to do the query task?  I kind of built my code for selecting a layer around this example: http://help.arcgis.com/en/webapi/javascript/arcgis/jssamples/query_bypoly.html, where I utilized the code for querying a polygon.  Well I guess then my other issue is, when I load my map service there are multiple layers on top of each other, so this onClick event would go to the function for the top layer I am assuming?  I am having a hard time figuring out how to have all of the layers turned off by default, so that user can just turn on the layer they want and select from.  For example if there is a layer with all the state boundaries, and another with all the county boundaries, the user might want to click a certain county, but since the state boundaries layer is on top of the counties boundary layer, the state which the county is within will be selected in the onClick event.  So any idea on how to have all the layers turned off by default, so the user can manually turn on the layer in which they would like to select from?  Thanks again for your help!
0 Kudos
JohnGravois
Deactivated User
0 Kudos