Retrieve layer name in QueryTask response. Possible??

2380
2
09-09-2015 07:23 AM
PeterLen
Occasional Contributor

Hello,

I am using the QueryTask.execute call to do some queries against some map service layers.  The response to the callback function is a feature set.  Currently, I am making my QueryTask calls in a synchronous fashion is that I wait until a QueryTask request is completed before making another request to the next map layer.  I was doing this so that I could keep track of which feature set went with which layer.  I want to be able to run the QueryTask requests asynchronously, however, so that all the results come back much faster.  In that case, I am just in a loop for each layer I want to query and submit a QueryTask.  The loop has completed before any of the features sets are returned.  The problem is that when I am in the callback function with the feature set response, I can't tell which layer the feature set results are for.  I can't set any local variables because those variable values will change before they can be appropriately used.  I tried looking at the various feature set properties but could not find anything that would tell me which layer was queried.

Does anyone have insight into whether I can obtain the layer name / url that was used in the QueryTask withi the callback function once the Query Task has completed?

Thanks - Peter

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Peter,

   When you execute your queryTask.execute(queryParams, lang.hitch(this, onQueryFinish, SomeVarYouWantToPass, SomeOtherVarYouWantToPass, etc), lang.hitch(this, onQueryErr));

Using this way you can pass as many vars to the handler along with the query results.

0 Kudos
KenBuja
MVP Esteemed Contributor

When using a promise to hold the results of querying different layers, remember that the results will be returned in the same order that they are supplied.

This example shows that in action. The parcels query is executed first, so its result is the first object in the promise

parcels = app.qtParcels.execute(app.qParcels);
buildings = app.qtBuildings.execute(app.qBuildings);
console.log("deferreds: ", parcels, buildings);
promises = all([parcels, buildings]);
promises.then(handleQueryResults);


function handleQueryResults(results) {
          console.log("queries finished: ", results);
          var parcels, buildings;

          // make sure both queries finished successfully
          if ( ! results[0].hasOwnProperty("features") ) {
            console.log("Parcels query failed.");
          }

          if ( ! results[1].hasOwnProperty("features") ) {
            console.log("Buildings query failed.");
          }
0 Kudos