Zoom to extent of all features

It can be convenient and useful for users to zoom to the extent of all features in a FeatureLayer once an app loads, when a layer is added to the map, or when a layer's definitionExpression is updated.

The FeatureLayer API provides a method called queryExtent(), which allows you to calculate the full extent of features at runtime that satisfy a given query. If no query parameters are set, then the method queries for the extent of all features in the layer according to its definitionExpression.

1
2
3
4
5
6
7
8
9
10
11
// When the layer is loaded, query for the extent
// of all features in the layer. Then set the view's
// extent to the returned extent of all features.

layer
  .when(() => {
    return layer.queryExtent();
  })
  .then((response) => {
    view.goTo(response.extent);
  });

The FeatureLayer has a fullExtent property that contains an extent object saved to the feature service. Since some FeatureLayers are created from client-side features, other services, or feature services that don't contain an accurate fullExtent of the data, using the fullExtent to zoom to all features in the layer can be unreliable in some cases. The queryExtent() method is more reliable when working with layers that either don't have a fullExtent, or the fullExtent doesn't accurately reflect the extent of the data.

1
2
3
4
5
// using the fullExtent can be unreliable and inconsistent across layers
// depending on the service and how the data was created
layer.when(() => {
  view.goTo(layer.fullExtent);
});

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.

The developer dashboard has moved

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close