Query task with setSelection and Zooming

239
1
Jump to solution
03-04-2019 08:06 AM
KevinHibma-EsriCA
Esri Contributor

Hi,

I'm wondering if there is another approach I can take to performing a Query, passing the result to setSelection so I can ultimately zoom to the selected features. What I have now works, but my "problem" with this approach is I need to return the geometry in the Query. If I do not return the the geometry, getSelectedFeatures won't return features with geometry because the query result did not have any. I was hoping to avoid asking for geometry because some rather large and complex polygons can be queried and the delay is noticeable with those queries.

Any other ways I could tackle this?

For reference, here is the relevant code:

//press button, do query.
_performQuery: function(id){
	this.layer = this.map.getLayer(id);
    var query = new Query();
    query.where = this.sqlquery.value;
    query.outFields = ["*"];
    query.returnGeometry = true;    
    
    new QueryTask(this.url).execute(query, lang.hitch(this, function(results){
        this.selectionManager.setSelection(this.layer, results.features).then(lang.hitch(this, function(){
			//some button stuff
        }));
    }),lang.hitch(this,function(error){
        var quoteMsg = "";
        if (this.fieldSelect[this.fieldSelect.selectedIndex].innerHTML.indexOf("number") > 0 
          && !this.removeQuotes.checked){
            quoteMsg = "\n Removing the quotes (') may fix the query.";
        }
        new Message({
            message: "There was a problem selecting." + quoteMsg
        });
        console.log(error);
    }));
},

//press another button, zoom to selected features

_zoom:function(features){
	var features = this.layer.getSelectedFeatures();
    try {

        //https://community.esri.com/thread/193429-zoom-to-selected
        if (this.layer.geometryType === "esriGeometryPoint" && features.length === 1){
            // single point  
            var p = features[0].geometry;
            if (this.map.spatialReference.wkid !== p.spatialReference.wkid){
                p = projection.project(p, this.mSR);
            }
            var maxZoom = this.map.getMaxZoom();  
            this.map.centerAndZoom(p, maxZoom- 1);
        }
        else {
            //multiple points and lines and polys
            var geom = this.selectionManager.getUnionGeometryBySelectedFeatures(this.layer);
            var ext = geom.getExtent().expand(1.2);
            if (this.map.spatialReference.wkid !== ext.spatialReference.wkid){
                ext = projection.project(ext, this.mSR);
            }
            this.selectionManager.map.setExtent(ext, true);
        }             
    }
    catch(err){
        new Message({
            message: "Failed to zoom to selection. Tried to zoom to " +features.length+ " features."
        });
        console.log(err);
    } 
    
},
0 Kudos
1 Solution

Accepted Solutions
KevinHibma-EsriCA
Esri Contributor

I think I can self answer this. Instead of doing a true QueryTask, I can just use selectFeatures off the layer. It seems to work equally well for both Map and FeatureServices

_performQuery: function(){
    var query = new Query();
    query.where = this.sqlquery.value;
    query.outFields = ["*"];
    this.layer.selectFeatures(query).then(lang.hitch(this, function(result){
      this.selectionManager.setSelection(this.layer, result).then(lang.hitch(this, function(){
           // some button stuff
      }));
    }));
}
    ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Reference:  https://community.esri.com/message/759029-not-getting-any-selection-results-jsapi-321-with-wabuilder...  

View solution in original post

0 Kudos
1 Reply
KevinHibma-EsriCA
Esri Contributor

I think I can self answer this. Instead of doing a true QueryTask, I can just use selectFeatures off the layer. It seems to work equally well for both Map and FeatureServices

_performQuery: function(){
    var query = new Query();
    query.where = this.sqlquery.value;
    query.outFields = ["*"];
    this.layer.selectFeatures(query).then(lang.hitch(this, function(result){
      this.selectionManager.setSelection(this.layer, result).then(lang.hitch(this, function(){
           // some button stuff
      }));
    }));
}
    ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Reference:  https://community.esri.com/message/759029-not-getting-any-selection-results-jsapi-321-with-wabuilder...  

0 Kudos