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 API to see how I could implement it but I’m having trouble figuring out what properties and/or methods to use to accomplish this.
Share
The
FeatureSetprovided to theQueryTask‘sonCompletecallback has the propertyfeaturesthat is an array ofGraphics.The javascript api provides the
esri.graphicsExtent(graphics)function that can accept that array ofGraphicsand 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 returnedGraphicsarray only has a single point in it, so you’ll want to check for it.Here’s an example
QueryTaskonCompletecallback that could be used to zoom the map to the extents of points returned by the query: