缩放至所有要素的范围

尝试一下在线预览

对于用户来说,在应用程序加载后、将图层添加到地图或更新图层的 definitionExpression 时,缩放到 FeatureLayer 中所有要素的范围既方便又有用。

FeatureLayer API 提供了一个名为 queryExtent() 的方法,该方法允许您在运行时计算满足给定查询的要素的完整范围。如果未设置查询参数,然后,该方法根据 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);
  });

FeatureLayer 具有 fullExtent 属性,其中包含保存到要素服务的范围对象。由于某些 FeatureLayers 是根据客户端要素、其他服务或不包含准确数据的 fullExtent 的要素服务创建的,因此在某些情况下,使用 fullExtent 缩放至图层中的所有要素可能不可靠。当使用没有 fullExtentfullExtent 不能准确反映数据范围的图层时,queryExtent() 方法更可靠。

     
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.