Select to view content in your preferred language

How to Deal with a Query in a Loop in order to Set Extent based on all Results

1514
2
Jump to solution
03-17-2017 08:10 AM
WilliamMiller4
Deactivated User

Hello,

I have a query in a loop. I have added .then to the end of the queryTask (see below) to add a graphic for each result upon resolution, but I want to set the extent based on all the results. How do I do this?

queryTask.execute(query, lang.hitch(this, this._onQueryResult, symbolColor, include, def, i), lang.hitch(this, function(error){
  def.resolve({state: 'failure', value: error});
})).then(this.list._addResults(i));

Any help is greatly appreciated.

William

0 Kudos
1 Solution

Accepted Solutions
WilliamMiller4
Deactivated User

Hi Robert,

I found a question similar to mine (Executing a series of queries inside a loop) that helped me resolve the problem.

For anyone looking at this post in the future, below is basically what worked for me.

_requestSucceeded: function(response, io){
  var indexLength = response.length;
  if(indexLength !== 0){
    this.ResultLayer = new GraphicsLayer();
    this.map.addLayer(this.ResultLayer);
    var queries = [];
    for(var i = 0; i < indexLength; i++){
      var def = new Deferred();
      var accountNum = response[i].AccountNum;
      var address = response[i].Address;
      this.list.add({
        accountNum: accountNum,
        address: address
      });
      var queryString = "AccountNum = " + response[i].AccountNum;
      var query = new Query();
      var queryTask = new QueryTask("arcgis/rest/services/Dynamic/AccountNumber/MapServer/0");
      query.where = queryString;
      query.returnGeometry = true;
      query.outFields = ["OBJECTID"];
      queries.push(queryTask.execute(query, lang.hitch(this, this._onSuccessfulAccNumQuery, symbolColor, include, def, i, indexLength), lang.hitch(this, function(error){
        def.resolve({state: 'failure', value: error});
      })).then(this.list._addResult(i, indexLength)));
    }
    all(queries).then(lang.hitch(this, function (){
      var resultExtent = [];
      for(var i = 0,  len = indexLength; i < len; i++){
        if(this.list.items[i].graphic){
          resultExtent.push(this.list.items[i].graphic);
        }
      }
      var gExt = graphicsUtils.graphicsExtent(resultExtent);
      this.map.setExtent(gExt.expand(1.5), true);
    }));
  } else{
    new Message({
      titleLabel: "Ratio Query",
      message: "No Results were found."
    });
  }
},

_onSuccessfulAccNumQuery: function(symbolColor, include, def, i, indexLength, results){
  var features = results.features;
  for(var j = 0, len = features.length; j < len; j++){
    var line = Color.fromRgb("rgb(" + symbolColor + ")");
    var color = Color.fromRgb("rgb(" + symbolColor + ",0.25)");
    var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, line, 2), color);
    var graphic = features[j];
    graphic.setSymbol(symbol);
    graphic.setAttributes({...});
    var infoTemplate = new InfoTemplate();
    infoTemplate.setTitle("${TITLE}");
    infoTemplate.setContent("CONTENT");
    graphic.setInfoTemplate(infoTemplate);
    this.list.items[i].graphic = graphic;
    var graphicItem = this.ResultLayer.add(graphic);
    if(include){
      graphicItem.show();
    } else{
      graphicItem.hide();
    }
  }
  def.resolve({state: 'success', value: results});
}

Thank you.

William

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

William,

   If you have to loop over multiple results and run a query on each then you need to use dojo/promise/all There are some examples on the dojo site of using a promise all in conjunction with multiple deferreds.

WilliamMiller4
Deactivated User

Hi Robert,

I found a question similar to mine (Executing a series of queries inside a loop) that helped me resolve the problem.

For anyone looking at this post in the future, below is basically what worked for me.

_requestSucceeded: function(response, io){
  var indexLength = response.length;
  if(indexLength !== 0){
    this.ResultLayer = new GraphicsLayer();
    this.map.addLayer(this.ResultLayer);
    var queries = [];
    for(var i = 0; i < indexLength; i++){
      var def = new Deferred();
      var accountNum = response[i].AccountNum;
      var address = response[i].Address;
      this.list.add({
        accountNum: accountNum,
        address: address
      });
      var queryString = "AccountNum = " + response[i].AccountNum;
      var query = new Query();
      var queryTask = new QueryTask("arcgis/rest/services/Dynamic/AccountNumber/MapServer/0");
      query.where = queryString;
      query.returnGeometry = true;
      query.outFields = ["OBJECTID"];
      queries.push(queryTask.execute(query, lang.hitch(this, this._onSuccessfulAccNumQuery, symbolColor, include, def, i, indexLength), lang.hitch(this, function(error){
        def.resolve({state: 'failure', value: error});
      })).then(this.list._addResult(i, indexLength)));
    }
    all(queries).then(lang.hitch(this, function (){
      var resultExtent = [];
      for(var i = 0,  len = indexLength; i < len; i++){
        if(this.list.items[i].graphic){
          resultExtent.push(this.list.items[i].graphic);
        }
      }
      var gExt = graphicsUtils.graphicsExtent(resultExtent);
      this.map.setExtent(gExt.expand(1.5), true);
    }));
  } else{
    new Message({
      titleLabel: "Ratio Query",
      message: "No Results were found."
    });
  }
},

_onSuccessfulAccNumQuery: function(symbolColor, include, def, i, indexLength, results){
  var features = results.features;
  for(var j = 0, len = features.length; j < len; j++){
    var line = Color.fromRgb("rgb(" + symbolColor + ")");
    var color = Color.fromRgb("rgb(" + symbolColor + ",0.25)");
    var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, line, 2), color);
    var graphic = features[j];
    graphic.setSymbol(symbol);
    graphic.setAttributes({...});
    var infoTemplate = new InfoTemplate();
    infoTemplate.setTitle("${TITLE}");
    infoTemplate.setContent("CONTENT");
    graphic.setInfoTemplate(infoTemplate);
    this.list.items[i].graphic = graphic;
    var graphicItem = this.ResultLayer.add(graphic);
    if(include){
      graphicItem.show();
    } else{
      graphicItem.hide();
    }
  }
  def.resolve({state: 'success', value: results});
}

Thank you.

William

0 Kudos