function selectAnimal(item) {
    //apply a definition expression to the layer to only display trees of the selected species 
    var query = "Animal_ID LIKE'${name}'"; 
    homerangeLayer.setDefinitionExpression(esri.substitute({ 
        name: dojo.trim(item.value) 
    }, query));
 homerangeLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(results) {
  var unionExtent;
  for (var i = 0, il = results.length; i < il; i++) {
            if (unionExtent) {
                //if extent already initialized, union it with new feature extent
                unionExtent = unionExtent.union(new esri.geometry.Extent(results.geometry.getExtent()));
            }
            else {
                //if extent not initialized, set it to feature's extent
                unionExtent = new esri.geometry.Extent(new esri.geometry.Extent(results.geometry.getExtent()));
            }
   console.log(unionExtent);
        }
        //zoom to the extent of all polygons.
        map.setExtent(unionExtent.expand(1.5));
 });
}Hi Alan,
Here is a function I wrote which will apply a definition query on a featureLayer based upon the selected value in a combo box. I then select features within the featureLayer based on the definition query, and then union the extent of each selected feature.function selectAnimal(item) { //apply a definition expression to the layer to only display trees of the selected species var query = "Animal_ID LIKE'${name}'"; homerangeLayer.setDefinitionExpression(esri.substitute({ name: dojo.trim(item.value) }, query)); homerangeLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(results) { var unionExtent; for (var i = 0, il = results.length; i < il; i++) { if (unionExtent) { //if extent already initialized, union it with new feature extent unionExtent = unionExtent.union(new esri.geometry.Extent(results.geometry.getExtent())); } else { //if extent not initialized, set it to feature's extent unionExtent = new esri.geometry.Extent(new esri.geometry.Extent(results.geometry.getExtent())); } console.log(unionExtent); } //zoom to the extent of all polygons. map.setExtent(unionExtent.expand(1.5)); }); }
Hopefully this code makes sense to you!
Ken
I use -
map.setExtent(unionExtent.expand(1.5));
I don't use -
map.setExtent(unionExtent.expand(1.5), true);
The top one works perfectly for me...
And just to clarify, I'm using it with version 2.8 of the API.
Ken
map.setExtent(unionExtent.expand(1.5), true);
When true, for maps that contain tiled map service layers, you are guaranteed to have the input extent shown completely on the map. (As of v1.3)
Thanks Detroit -- needed the section that creates an extent for a set of points!