Multiple query tasks

5275
20
07-06-2015 06:37 AM
OliviaGill1
New Contributor

Hi, I have carried out a queryTask on a map service that returns a polygon when I input a postcode. Within the same service, there is an additional layer where I want to query a local authority name on. I would like to carry out a querytask on this layer at the same time so that the map returns a polygon relating to the postcode input and also tells me what local authority it is contained within. I will ultimately show the local authority as text in a separate widget. Can anyone help please? Here's my code so far with the second queryTask cancelled out near the middle as I'm not sure where it should go??

var query = new Query();
  query.returnGeometry = true;
  query.outFields = [
  "POSTCODE"
  ];

  on(dom.byId("execute"), "click", execute);

  function execute() {
  query.text = dom.byId("Postcode").value;
  queryTask.execute(query, showResults);
  }
  function showResults(results) {
  var resultItems = [];
  var resultCount = results.features.length;
  for (var i = 0; i < resultCount; i++) {
  var featureAttributes = results.features.attributes;

  var graphic = results.features;
  graphic.setSymbol(symbol);
  map.graphics.add(graphic);

  //var queryTask2 = new QueryTask("http://rest/services/live/SEARCH/MapServer/15");
  //query2 = new.esri.tasks.Query();
  //query2.returnGeometry - false;
  //query2.where = ""
var strNum = 100;
  var newExtent = graphic.geometry.getExtent();
  newExtent.xmin = newExtent.xmin - strNum;
  newExtent.ymin = newExtent.ymin - strNum;
  newExtent.xmax = newExtent.xmax + strNum;
  newExtent.ymax = newExtent.ymax + strNum;
  map.setExtent(newExtent);

  for (var attr in featureAttributes) {
  resultItems.push("<b>" + attr + ":</b> " + featureAttributes[attr] + "<br>");
  }
  resultItems.push("<br>");
  }
  dom.byId("info").innerHTML = resultItems.join("");

  }
  dom.byId("results")

  function addToMap(result) {
  debugger;
  }
 });
Tags (1)
0 Kudos
20 Replies
RobertScheitlin__GISP
MVP Emeritus

Olivia,

  This is a perfect situation to use dojo/promise/all

        var promises = [];
        promises.push(queryTask.execute(query));
        promises.push(queryTask2.execute(query2));
        var allPromises = new all(promises); 
        allPromises.then(function (taskResults) {
            // results will be an Array 
            for (var i=0;i<taskResults.length;i++){
                var taskResult = taskResults;
                showResults(taskResult);
            }
        });
0 Kudos
thejuskambi
Occasional Contributor III

Not necessarily, you could easily build a whereClause within the loop or get a Union of all the geometries and than use it in the query after the looping.

0 Kudos
OliviaGill1
New Contributor

Thank you  - so do I just need to add this to the end of a the 2 consecutive queryTasks that I carry out?? Sorry but I'm stuck!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Olivia,

   Sorry I misunderstood your needs. I thought you needed to execute both queries then work with the results of both, I did not understand that the second query was dependent on the result of the first.

I sounds like you need to take the geometry of the first query result and use that as the Query geometry for the second Query. Is this the use case?

It also sounds like you are wanting to add a single attribute from the second query to the resultItems object that you are creating from the first query Is this correct?

0 Kudos
thejuskambi
Occasional Contributor III

you code should be something like this. It has not been tested. The example shows using the geometry, you could also build where clause as well

function showResults(results) {
    var resultItems = [];
    var inBuffer= [];
    var resultCount = results.features.length; 
    for (var i = 0; i < resultCount; i++) {
        inBuffer.push(results.features.geometry);

        var featureAttributes = results.features.attributes; 

        var graphic = results.features; 
        graphic.setSymbol(symbol); 
        map.graphics.add(graphic); 
     
        var strNum = 100; 
       
        var newExtent = graphic.geometry.getExtent(); 
        newExtent.xmin = newExtent.xmin - strNum; 
        newExtent.ymin = newExtent.ymin - strNum; 
        newExtent.xmax = newExtent.xmax + strNum; 
        newExtent.ymax = newExtent.ymax + strNum; 
        map.setExtent(newExtent); 
     
        for (var attr in featureAttributes) {
            resultItems.push("<b>" + attr + ":</b> " + featureAttributes[attr] + "<br>"); 
        } 
            resultItems.push("<br>"); 
    }
   
    var query = new Query();         
    query.geometry = geometryEngine.union(inBuffer);     
    //your second query should come here
    //var queryTask2 = new QueryTask("http://rest/services/live/SEARCH/MapServer/15"); 
    //query2 = new.esri.tasks.Query(); 
    //query2.returnGeometry - false; 
    //query2.where = "" 
    dom.byId("info").innerHTML = resultItems.join(""); 
}
0 Kudos
OliviaGill1
New Contributor

It work to an extent although I still don't get the local authority name back?? I've attached my code for info...Perhaps I've been staring at it for too long??

0 Kudos
Steveway1
New Contributor II

It doesn't look like you are executing your second query task

thejuskambi
Occasional Contributor III

you need the execute the second query task.  Please make sure geometryEngine and array are part of the require/define modules.

function showResults(results) {
            var resultItems = [];
            var inBuffer = [];
            var resultCount = results.features.length;
            for (var i = 0; i < resultCount; i++) {
                inBuffer.push(results.features.geometry);

                var featureAttributes = results.features.attributes;

                var graphic = results.features;
                graphic.setSymbol(symbol);
                map.graphics.add(graphic);
               
                var strNum = 100;
                var newExtent = graphic.geometry.getExtent();
                newExtent.xmin = newExtent.xmin - strNum;
                newExtent.ymin = newExtent.ymin - strNum;
                newExtent.xmax = newExtent.xmax + strNum;
                newExtent.ymax = newExtent.ymax + strNum;
                map.setExtent(newExtent);

                for (var attr in featureAttributes) {
                    resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");
                }
                resultItems.push("<br>");
            }
            var query2 = new Query();
            query.geometry = geometryEngine.union(inBuffer);
            query2.returnGeometry = false;
            query2.outFields = [ "NAME" ];

            var resultNames = [];
            var queryTask2 = new QueryTask("http://rest/services/live/SEARCH/MapServer/15");
            queryTask2.execute(query2, function(result){
                if(result && result.features.length > 0){
                    array.forEach(result.features, function(feature){
                        resultNames.push(feature.attributes.NAME);
                    }
                }
            });
           
            dom.byId("info").innerHTML = resultItems.join("") + "<BR>" + resultNames.join(",");
}
thejuskambi
Occasional Contributor III

I dont understand your problem, do you want to use the result of one querytask to make a query on another layer. is that what you are asking for?