3

I need to get the extent of a set of filtered features on a feature layer and zoom to that.

I tried the queryExtent method on the layer object, but with no success. The promise got back with an object that had the right count set on it, but there was no extent present in the result:

    var query = new Query();
    query.where = "application_id= '" + applicationId + "'";
    query.returnGeometry = true;
    layer.queryExtent(query).then(function(result){
        console.log('result', result) 
        // result: { count: 2, _ssl: undefined }
        if(result.count > 0) {
            zoomTo(result.extent);
        }
    });

How can I zoom to the extent of a filtered set of features on the layer?

2
  • 1
    Which version of the JSAPI are you using? – Bjorn Svensson Oct 17 '17 at 4:09
  • I have this same problem in ArcGIS JS-API 3.29 – pmiranda Nov 6 '19 at 18:21
2

Turns out the queryExtent method does not support this type of query.

Instead I had to use queryFeatures instead, and build up the extent manually from the resulting featureSet.

var query = new Query();
query.where = "application_id= '" + applicationId + "'";
query.returnGeometry = true;

layer.queryFeatures(query).then(function(featureSet){
    console.log('featureSet', featureSet) 

    if(featureSet.features.length > 0) {
        var extent = featureSet.features[0].geometry.getExtent();
        featureSet.features.forEach(function(feature) {
            extent = extent.union(feature.geometry.getExtent());
        })

        zoomTo(extent.expand(1.3));                                        

    }
});
0

It is easier to use "esri/graphicsUtils" that allow to obtain the extent of an graphic array.

var query = new Query();
query.where = "application_id= '" + applicationId + "'";
query.returnGeometry = true;

layer.queryFeatures(query).then(function(featureSet){
    console.log('featureSet', featureSet) 

    if(featureSet.features.length > 0) {

        var extent = graphicsUtils.graphicsExtent(featureSet.features)
        zoomTo(extent.expand(1.3));                                        

    }
});
0

If you want to avoid loading all features just to get the extent, use a QueryTask and add returnExtentOnly=true to its URL. This parameter was added in ArcGIS Server 10.4, see here.

I modified this sample to do just that:

var queryTask = new QueryTask("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3?returnExtentOnly=true");

var query = new Query();

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

function execute() {
    query.where = "state_name='" + dom.byId("stateName").value + "'";
    queryTask.execute(query, showResults);
}

function showResults(results) {
     resultItems = [results.extent.xmin, results.extent.ymin, results.extent.xmax, results.extent.ymax];
     dom.byId("info").innerHTML = resultItems.join(",");
}

If you have set a layer definition on the layer you are querying, make sure to add that to the Query's where property too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.