I have two polygon layers in a webmap service and want to do a dynamic query on click, such that when the user clicks on a lake (layer A) with ID = 100, the corresponding watershed boundary (layer B) with ID = 100 is highlighted. So basically, I want to pass the ID found on click from layer A and use that ID to query layer B by attribute. How do I do this?
You can write js code like
on(dom.byId("execute"), "click", execute);
function execute (evt) {
var aQuery = new esri.tasks.QueryTask("http://mymaps.com/arcgis/rest/services/myservice/MapService/0"); //layerA
var aParams = new esri.tasks.Query();
//...set your parameters
aParams.geometry = evt.mapPoint;
aQuery.execute(aParams, function (aResults) {
var aQuery2 = esri.tasks.QueryTask("http://mymaps.com/arcgis/rest/services/myservice/MapService/1"); //layerB
var aParams2 = new esri.tasks.Query();
// ... set query parameters
aParams2.where = "ID = " + aResults.features[0].attributes["ID"];
aQuery2.execute(aParams2, function (aResults2) {
//visualize in map
});
});
}
Thanks for your reply Domenico. I'm trying to use your example below with my published layers, and have yet to get it working. I think the issue is related to the visualize map section - I'm not too sure about how to handle that (I'm new to this)
on(dom.byId("execute"), "click", execute);
function execute (evt) {
map.graphics.clear();
var Symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new Color([255, 0, 0]), 3), new Color([125, 125, 125, 0.35]));
var aQuery = new esri.tasks.QueryTask("http://arcgis.geosyntec.com/arcgis/rest/services/BOS_MWBP/BOS_MWBP/MapServer/1"); //lakes
var aParams = new esri.tasks.Query();
aParams.outFields = ["*"];
aParams.returnGeometry = true;
aParams.geometry = evt.mapPoint;
aQuery.execute(aParams, function (aResults) {
var aQuery2 = esri.tasks.QueryTask("http://arcgis.geosyntec.com/arcgis/rest/services/BOS_MWBP/BOS_MWBP/MapServer/2"); //layerB
var aParams2 = new esri.tasks.Query();
aParams2.outFields = ["*"];
aParams2.returnGeometry = true;
aParams2.where = "ID = " + aResults.features[0].attributes["ID"];
aQuery2.execute(aParams2, function (aResults2) {
graphic.setSymbol(Symbol);
map.graphics.add(graphic);
});
});
}