Zooming to query result extent

02-14-2014 01:21 PM
New Contributor
I am trying to zoom to the extent of a query I have.  I am having a problem when I call esri.graphicsUtils.graphicsExtent(resultFeatures) to get the extent.  It is returning NaN.  It works for one query I have that returns 42 records but for all the other queries I have it doesn't work.  

       queryTask.execute(query, function (featureSet) {
            var resultFeatures = featureSet.features;
            var myFeatureExtent = esri.graphicsUtils.graphicsExtent(resultFeatures);
               var layerDefinitions = [];
               layerDefinitions[0] = queryString;

Any help would be appreciated.


0 Kudos
7 Replies
Regular Contributor III
Try something along these lines, using union to grow the extent to encompass all features:

var extent = featureSet.features[0].geometry.getExtent();
for (var i = 1; i < featureSet.features.length; i++) {
    extent = extent.union(featureSet.features.geometry.getExtent());
Occasional Contributor
graphicsUtils.graphicsExtent is a lot faster than union for this purpose, especially when you are dealing with a large number of features.

Your code looks fine, all that could be wrong is your query, if you have forgot to set returnGeometry = true.
0 Kudos
Occasional Contributor II
First of all, try to see if your query parameters and layers are correct,

Also, see what the returned featureSet object is, especially if the features have proper geometries and spatial reference.
0 Kudos
Frequent Contributor
I'm having problems with this too, especially in IE 8, which is still our version for IE.  In the code

var selExtent = graphicsUtils.graphicsExtent(featureSet.features);

the variable selExtent is not found, it returns null. 
There are features in my featureSet, which is a result of a queryTask.   This works just fine in Firefox, but not in either Chrome or IE.
0 Kudos
Frequent Contributor
I'm mistaken, it doesn't work properly in Firefox either.  It seems to be moving to the general vicinity of my featureSet, but it isn't set to the extent.  Instead, if I zoom out several times manually, I can see the points. 

I'm wondering of the features returned as the results of a queryTask are the same as the results of a selectFeatures on a featureLayer.  Both say they are a featureSet.
0 Kudos
Frequent Contributor
Yes, I am selecting points, but I have an if statement that sets the map to center and zoom if there is only one feature returned.  I thought maybe my queryTask wasn't done executing, so I changed it to have a queryTask.on('complete') trying to make sure the executing is done before it finishes.

The queryTask is also populating a dGrid, which looks just fine, so I know the queryTask is executing and returning what I would expect.  The problem looks to be limited to extent of the results.  The idea of a union isn't going to work with points.  I wonder if my problem is that the map is in a floating pane, and that there is some sort of delay.  Even after that pane is open, it still doesn't set the extent correctly.  From what I can see, it is almost always centered on the first point returned from the query.

I've seen a few other threads with people having the same problem, I have to wonder if there is an issue with graphicsExtent itself. 

0 Kudos
New Contributor III

For future references... It's 2020 and I'm having the same issue you described using graphicsUtils.graphicsExtent with a point layer. I develop using js api version 3.x. Turns out, that for some reason one feature in the array returned from the REST query is coming with the following geometry:

  "geometry": {
    "x": "NaN",
    "y": "NaN",
    "spatialReference":  ...
  "attributes": {



Our users add points to this layer using Arcmap 10.5 and Arcmap 10.7 and I couldn't find out why sometimes this null geometry happens.

If you query the database to view this bad registry with sqldeveloper, you will find some null data inside shape column:

At least, that was what happened here. I would recomend checking the array for NaN data before using graphicsUtils.graphicsExtent to filter out bad data like this:

//note that NaN here is a string

var goodFeatures = features.filter(f => f.geometry.x === "NaN" || f.geometry.y === "NaN");

var extent = graphicsUtils.graphicsExtent(goodFeatures)

When I set the map with a bad extent, the map simply disapear and breaks the app.

0 Kudos