[GIS] How to determine what Level a new Extent would be, without zooming to it

arcgis-javascript-api

In the ArcGIS Server JavaScript API 2.4, there are some related methods for getting and setting the map's extent.

  • map.getLevel() determine's the map's current level (when using a Tiled layer)
  • map.setExtent(extent) changes the map's extent to the new extent
  • map.setLevel(level) changes the map's level to the new level

Is it possible to determine what level the value of extent would equate to, before changing to that extent? That is, before I pass in map.setExtent(x1, y1, x2, y2) I want to know what level this would display, so that I can adjust the value as necessary.

I can set a listener on map.onExtentChange, verify the new level, and use map.setLevel(map.getLevel() + 1) to fix the extent if necessary – but this means the map may draw twice (once after map.setExtent then again after map.setLevel)

As far as I can see, there is no equivalent of extent.getLevel – is there a way to calculate this without actually changing the map level first?

Best Answer

You can call the internal _fixExtent method on the map object to get the new LOD. Main caveat is that it is an internal method, so it may or may not change in the future. If you're concerned about it changing in the future though, you could always look at its code and replicate it to your own liking.

Syntax:

var fixed = map._fixExtent(extent, fit);
var level = fixed.lod.level;