ArcGIS Javascript - Zoom to show all points

Viewed 15358

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.

3 Answers

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

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)
  }
}
Related