<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Zoom to extent of all features | Sample | ArcGIS Maps SDK for JavaScript</title>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
<script type="module" src="https://js.arcgis.com/5.0/"></script>
<arcgis-scene basemap="dark-gray-vector">
<arcgis-zoom slot="top-left"></arcgis-zoom>
<arcgis-navigation-toggle slot="top-left"></arcgis-navigation-toggle>
<arcgis-compass slot="top-left"> </arcgis-compass>
<arcgis-layer-list slot="top-right"></arcgis-layer-list>
const [FeatureLayer, Collection] = await $arcgis.import([
"@arcgis/core/layers/FeatureLayer.js",
"@arcgis/core/core/Collection.js",
const featureLayer = new FeatureLayer({
outFields: ["STATION_NAME", "COUNTRY", "TEMP"],
// autocasts as new PortalItem()
id: "3a177da3f6524d61980fb41125b2349c",
title: "Temperature on Jan, 4, 2017",
// When the layer is loaded, query for the extent
// of all features in the layer that satisfy the
// definitionExpression. Then set the viewElement's
// extent to the returned extent of all features.
featureLayer.when(() => {
featureLayer.definitionExpression = createDefinitionExpression("");
zoomToLayer(featureLayer);
/*****************************************************************
* Setup the Scene component and add the feature layer
*****************************************************************/
const viewElement = document.querySelector("arcgis-scene");
await viewElement.viewOnReady();
viewElement.map.add(featureLayer);
// setup the Layer List component
const layerList = document.querySelector("arcgis-layer-list");
await layerList.componentOnReady();
layerList.listItemCreatedFunction = createActions;
// definitionExpressions used by each action
// listed in the LayerList
const expressions = new Collection([
expression: "TEMP > 50 AND TEMP <=75",
expression: "TEMP > 25 AND TEMP <=50",
expression: "TEMP <= 25",
expression: "LATITUDE >= 66.5",
id: "north-temperate-zone",
expression: "LATITUDE < 66.5 AND LATITUDE >= 23.5",
expression: "LATITUDE < 23.5 AND LATITUDE >= -23.5",
// When an action is triggered, the definitionExpression
// is set on the layer and the viewElement's extent updates
// to match the features visible in the layer
layerList.addEventListener("arcgisTriggerAction", async (event) => {
const { action, item } = event.detail;
const layer = item.layer;
const subExpression = expressions.find((item) => {
return item.id === action.id;
layer.definitionExpression = createDefinitionExpression(subExpression);
function createActions(event) {
className: "esri-icon-zoom-out-fixed",
className: "esri-icon-zoom-out-fixed",
className: "esri-icon-zoom-out-fixed",
className: "esri-icon-zoom-out-fixed",
title: "Above Arctic Circle",
className: "esri-icon-zoom-out-fixed",
title: "North Temperate Zone",
className: "esri-icon-zoom-out-fixed",
id: "north-temperate-zone",
className: "esri-icon-zoom-out-fixed",
// Appends a definitionExpression to a base expression
// the base expression only returns features in
// Canada, USA, and Mexico. It excludes some US territories
// located on the other side of the dateline
function createDefinitionExpression(subExpression) {
"( COUNTRY LIKE '%United States Of America%' OR " +
"COUNTRY LIKE '%Canada%' OR " +
"COUNTRY LIKE '%Mexico%') AND NOT" +
"(COUNTRY LIKE '%Johnston/Wake/Xmas%' OR " +
"COUNTRY LIKE '%Hawaii%' OR " +
"COUNTRY LIKE '%Marshall Islands%' OR " +
"STATION_NAME = 'Eareckson/Shemya' OR " +
"COUNTRY LIKE '%Guam%' )";
return subExpression ? baseExpression + " AND (" + subExpression + ")" : baseExpression;
// Zooms to the extent of the layer as defined by
// its definitionExpression
// This method will work for all FeatureLayers, even
// those without a saved `fullExtent` on the service.
function zoomToLayer(layer) {
return layer.queryExtent().then((response) => {
viewElement.goTo(response.extent).catch((error) => {