ArcGIS Javascript - Zoom to show all points
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:
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);
}
}
The FeatureSet
provided to the QueryTask
's onComplete
callback has the property features
that is an array of 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.
It should be noted that the documentation for 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.
Here's an example QueryTask
onComplete
callback that could be used to zoom the map to the extents of points returned by the query:
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)
}
}