ArcGIS Javascript - 缩放以显示所有点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11801011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArcGIS Javascript - Zoom to show all points
提问by John F.
I am trying to add some functionality that will zoom the map in/out depending on the points that are returned from a query. So for example, say we're zoomed in on the state of Texas. If I execute a query and the service returns back points that are in Texas AND some located in California, I would like the map to then zoom out and then display both California and Texas. I have been looking through the ArcGIS JS APIto see how I could implement it but I'm having trouble figuring out what properties and/or methods to use to accomplish this.
我正在尝试添加一些功能,根据查询返回的点放大/缩小地图。例如,假设我们放大了德克萨斯州。如果我执行查询并且服务返回位于德克萨斯州和位于加利福尼亚州的点,我希望地图缩小并显示加利福尼亚州和德克萨斯州。我一直在查看ArcGIS JS API以了解如何实现它,但我无法弄清楚要使用哪些属性和/或方法来完成此操作。
回答by shrichards
The FeatureSet
provided to the QueryTask
's onComplete
callback has the property features
that is an array of Graphics
.
将FeatureSet
提供给QueryTask
的onComplete
回调具有这样的特性features
即阵列Graphics
。
The javascript api provides the esri.graphicsExtent(graphics)
function that can accept that array of Graphics
and calculate their extent. Once the extent has been calculated, map.setExtent(extent)
can be used to zoom the map to that extent.
javascript api 提供了esri.graphicsExtent(graphics)
可以接受该数组Graphics
并计算其范围的函数。计算完范围后,map.setExtent(extent)
可用于将地图缩放到该范围。
It should be noted that the documentationfor esri.graphicsExtent(...)
specifies that 'If the extent height and width are 0, null is returned.' This case will occur if the returned Graphics
array only has a single point in it, so you'll want to check for it.
应当注意的是,文档用于esri.graphicsExtent(...)
指定对“如果程度的高度和宽度都为0,则返回null”。如果返回的Graphics
数组中只有一个点,就会发生这种情况,因此您需要检查它。
Here's an example QueryTask
onComplete
callback that could be used to zoom the map to the extents of points returned by the query:
这是一个示例QueryTask
onComplete
回调,可用于将地图缩放到查询返回的点的范围:
function onQueryComplete(returnedPointFeatureSet){
var featureSet = returnedPointFeatureSet || {};
var features = featureSet.features || [];
var extent = esri.graphicsExtent(features);
if(!extent && features.length == 1) {
// esri.getExtent returns null for a single point, so we'll build the extent by hand by subtracting/adding 1 to create x and y min/max values
var point = features[0];
extent = new esri.geometry.Extent(point.x - 1, point.y - 1, point.x + 1, point.y + 1, point.spatialReference);
}
if(extent) {
// assumes the esri map object is stored in the globally-scoped variable 'map'
map.setExtent(extent)
}
}
回答by ADi3ek
I agree, map.setExtent(extent, true)
is the way to go here. Another observation: In case we have only a single point it's worth considering simply using map.centerAndZoom(point, ZOOM_LEVEL)
instead of creating an extent. Then, we could just have this:
我同意,这map.setExtent(extent, true)
是去这里的方式。另一个观察:如果我们只有一个点,值得考虑简单地使用map.centerAndZoom(point, ZOOM_LEVEL)
而不是创建一个范围。然后,我们可以这样:
function onQueryComplete(returnedPointFeatureSet){
var featureSet = returnedPointFeatureSet || {};
var features = featureSet.features || [];
var extent = esri.graphicsExtent(features);
if(!extent && features.length == 1) {
var point = features[0];
map.centerAndZoom(point, 12);
}
else {
map.setExtent(extent, true);
}
}
回答by Below the Radar
Not a good idea to create an extent from a point that way. If the units are in degrees you could get a huge extent. Instead, you could do a buffer around the point using the geometryEngine
从这样的角度创建范围不是一个好主意。如果单位以度为单位,您可以获得很大的范围。相反,您可以使用 geometryEngine 在该点周围做一个缓冲区
function onQueryComplete(featureSet){
if (featureSet.features.length) {
var extent = esri.graphicsUtils.graphicsExtent(featureSet.features);
if(!extent && featureSet.features.length == 1 && featureSet.features[0].geometry.type == "point") {
var point = featureSet.features[0];
var extent = esri.geometry.geometryEngine.buffer(point.geometry, 1, "meters").getExtent();
}
// assumes the esri map object is stored in the globally-scoped variable 'map'
map.setExtent(extent)
}
}