map.setExtent where extent is common exten of array of features

2328
9
07-26-2012 04:34 AM
MatejSkerjanc
Occasional Contributor
i get array of features via a function (like query) and i create common extent from the array and then i use map.setExtent(arrayExtent, true);

The problem is the map doesnt create extent where all features would be visible in other words it sets too small extent.
Has anyone else seen this peculiar behavior?
0 Kudos
9 Replies
AlanRussian
New Contributor III
Hello, Matej,

Could you please post the code that creates the common extent from the array?

--Alan
0 Kudos
KenMorefield
Occasional Contributor
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
0 Kudos
AlanRussian
New Contributor III
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


And you said you've tried map.setExtent(unionExtent.expand(1.5), true);, correct?
0 Kudos
KenMorefield
Occasional Contributor
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
0 Kudos
AlanRussian
New Contributor III
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


Could you try the following and see if it works?
map.setExtent(unionExtent.expand(1.5), true);


From the documentation:

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)
0 Kudos
KenMorefield
Occasional Contributor
That seems to work exactly the same for me.  I verified that both ways zoom me to the exact same extent.  Is it not working for you?

Ken
0 Kudos
MatejSkerjanc
Occasional Contributor
allanrusian here's the code



var xmax, xmin, ymin, ymax;
var extent;


if(features[0].geometry.type != "point")
{
  //initial extent
  extent = features[0].geometry.getExtent();
  xmax = extent.xmax;
  xmin = extent.xmin;
  ymax = extent.ymax;
  ymin = extent.ymin;
 

  dojo.forEach(features, function(feature)
  {
   extent = feature.geometry.getExtent();
   //alert(extent.xmin + ',' + extent.ymin +  ',' + extent.xmax + ',' + extent.ymax);
   if(extent.xmax > xmax)
    xmax = extent.xmax;
   if(extent.xmin < xmin)
    xmin = extent.xmin;
   if(extent.ymax > ymax)
    ymax = extent.ymax;
   if(extent.xmin < ymin)
    ymin = extent.ymin;
  });
}
else
{
  //initial extent za pike

   xmin = features[0].geometry.x;
   ymin = features[0].geometry.y;
   xmax = features[0].geometry.x;
   ymax = features[0].geometry.y;
 
 

  dojo.forEach(features, function(feature)
  {
   if(feature.geometry.x > xmax)
    xmax = feature.geometry.x;
   if(feature.geometry.y > ymax)
    ymax = feature.geometry.y;
   
   if(feature.geometry.x < xmin)
    xmin = feature.geometry.x;
   if(feature.geometry.y < ymin)
    ymin = feature.geometry.y;
  });
 
 
}

//var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor, map.spatialReference)
  var xtent = new esri.geometry.Extent({
"xmin":xmin,
"ymin":ymin,
"xmax":xmax,
"ymax":ymax,  "spatialReference": { "wkid":map.spatialReference.wkid }
});


map.setExtent(xtent, true);
0 Kudos
DouglasHall
New Contributor III
Thanks Detroit -- needed the section that creates an extent for a set of points!
0 Kudos
MatejSkerjanc
Occasional Contributor
Thanks Detroit -- needed the section that creates an extent for a set of points!


You are most welcome, glad it helped someone.
0 Kudos