Select to view content in your preferred language

Setting a zoom level based on the size or shape of a feature

2023
6
Jump to solution
12-14-2011 11:16 AM
MikeOnzay
Frequent Contributor
I want to display a feature on my map to provide better context within the surrounding area than usinng setExtent or setExtent().expand().

In this example, Display Find Task in Grid, the map displays each parcel at an extent that I think is too close for the viewer to see it within the context of the surrounding area. This is especially true for really small parcels. For larger parcels you get a slightly better view. Even so, in many cases, part of the boundary is blocked by the window.

Would it be possible to evaluate the size/shape/area of each feature and then set a zoom level or extent that would provide a better view of the surrounding area? In other words, not be too close or too far away?

I saw esri.geometry.geodesicAreas(polygons, areaUnit) and also saw I would probably need to use esri.geometry.webMercatorToGeographic(geometry) first.

I tried adding a couple of lines in this function from the sample. I did not get any errors but I also did not see any numbers return in my console. Am I using this correctly? Overall, am I on the right track?

      function onRowClickHandler(evt){
        var clickedTaxLotId = grid.getItem(evt.rowIndex).PARCELID;
        var selectedTaxLot;

        dojo.forEach(map.graphics.graphics,function(graphic){
          if((graphic.attributes) && graphic.attributes.PARCELID === clickedTaxLotId){
            selectedTaxLot = graphic;
            return;
          }
        });
  var x = esri.geometry.webMercatorToGeographic(selectedTaxLot.graphic); 
  var areas = esri.geometry.geodesicAreas(x, esri.Units.ACRES);
  console.log(areas);
        var taxLotExtent = selectedTaxLot.geometry.getExtent();
        map.setExtent(taxLotExtent);
      }
0 Kudos
1 Solution

Accepted Solutions
MikeOnzay
Frequent Contributor
Thanks. The code you suggested provides the display I was looking for and helped me understand what I needed to do.  After fiddling with it I realized I still had to add
map.setExtent(taxLotExtent, true)
with the boolean parameter as you suggested in an earlier post to make sure it fit within the window.

View solution in original post

0 Kudos
6 Replies
MikeOnzay
Frequent Contributor
Okay, I should not have used esri.geometry.webMercatorToGeographic(geometry) in the example because that parcel layer is not in webMercator. I was thinking of my own layer which is in webMercator. That being said I would still like some thoughts on my original questions.
0 Kudos
by Anonymous User
Not applicable
I've done this in the past using map.setLevel() right after setting the extent to the feature extent.  It's kind of a quick and dirty way to do it, but I've found that just backing out one level of detail gives the desired effect. 

Of course, you have to be using a cached basemap for this to work.  Pseudo code would be something like this.

var curLevel = map.getLevel();
map.setLevel(curLevel + 1);

I never can remember whether you subtract 1 to zoom out or add 1.  You can test it out in your code and find out easy enough.

Jeff
0 Kudos
JeffPace
MVP Alum
just use expand
if you want to be about 25% more out, do

var taxLotExtent = selectedTaxLot.geometry.getExtent();
taxLotExtent = taxLotExtent.expand(1.25);
        map.setExtent(taxLotExtent, true);

not the boolean true on set extent, this will force your map to go out a level if even a bit of the extent is clipped.
0 Kudos
MikeOnzay
Frequent Contributor
Thanks for both of those answers.

Using expand with setExtent and the true option helps. But I have features that range in size from as big as the Gulf of Mexico to some dinky island in the middle of it. For the small area it would be helpful to see it within the context of nearby land or some other reference information from the Ocean basemap. That is why I was asking if there was a way to evaluate the size or shape of the area and then set a zoom level.
0 Kudos
JeffPace
MVP Alum
Thanks for both of those answers.

Using expand with setExtent and the true option helps. But I have features that range in size from as big as the Gulf of Mexico to some dinky island in the middle of it. For the small area it would be helpful to see it within the context of nearby land or some other reference information from the Ocean basemap. That is why I was asking if there was a way to evaluate the size or shape of the area and then set a zoom level.


It sounds like you want to have a maximum zoom level on your result.  Or you could

var taxLotExtent = selectedTaxLot.geometry.getExtent();
var height = taxLotExtent.ymax-taxLotExtent.ymin;
var width = taxLotExtent.xmax - taxLotExtent.xmin;
if (width < someSmallWidth or height < someSmallHeight){
taxLotExtent = taxLotExtent.expand(5);
}else{
taxLotExtent = taxLotExtent.expand(1.25);
}
0 Kudos
MikeOnzay
Frequent Contributor
Thanks. The code you suggested provides the display I was looking for and helped me understand what I needed to do.  After fiddling with it I realized I still had to add
map.setExtent(taxLotExtent, true)
with the boolean parameter as you suggested in an earlier post to make sure it fit within the window.
0 Kudos