Setting max and min zoom level for map

14044
18
03-07-2014 07:20 AM
vinayb
by
New Contributor III
HI All,

I wan to set zoom level , that is i want to define maximum level upto which user zoom-out and min level to which user can zoom in .
0 Kudos
18 Replies
JeffPace
MVP Alum
Ahh, this only works on tiled maps

Unfortunately there is no min/max extent yet.

So your only option is to write an on(extent-change) event that checks the extent, sees if it is beyond where you want to be, and reverts.

sorry for the confusion.
0 Kudos
vinayb
by
New Contributor III
How can i set extent of map  to revert back  ( using map.setExtent  i think this is deprected ) i am using 3.6 api ?
0 Kudos
LloydBronn
Occasional Contributor II

Reviving this old thread. I'm trying to set max and min zooms in my map. I want the initial (max) extent to be zoom level 3, and I don't want to be able to zoom in any further than level 15 or so (min extent). I found a snippet of JS that works for the first part, and I'm trying to adapt it to work for the second. I think my issue is how I'm trying to set the minimum extent. I only want a lat/lon center point for the maximum (initial) extent. Here is the code snippet. The first if statement works but the else if does not. 

map = new Map("map", {
            basemap: "streets",
               center: [-105, 45],
            zoom: 3
          });
            
            //set max extent to inital extent
        dojo.connect(map, "onLoad", function(){
         maxExtent = map.extent;
         minExtent = ("map", {
            zoom: 15
          });
        });

        //check to see if map is within max extent when its extent changes.  If not, roll back to the max
        //extent that we set above
        dojo.connect(map, "onExtentChange", function(extent){
          if((map.extent.xmin < maxExtent.xmin) ||
            (map.extent.ymin < maxExtent.ymin)  ||
            (map.extent.xmax > maxExtent.xmax) ||
            (map.extent.ymax > maxExtent.ymax) 
            ) {
            map.setExtent(maxExtent);
          }
            else if((map.extent.xmin > minExtent.xmin) ||
            (map.extent.ymin > minExtent.ymin)  ||
            (map.extent.xmax < minExtent.xmax) ||
            (map.extent.ymax < minExtent.ymax) 
            ) {
            map.setExtent(minExtent);
          }
        
      });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   A couple of things. Number 1 do not get in the habit of use the depreciated dojo.connect. What you should use is on

map.on("load", function(){

and

map.on("extent-change", function(extent){

Next if you are using an tiled/cached basemap then you get set the LODs you want to use to limit the zoom in and zoom out like discussed about. If you are not using a tiled/cached basemap then use your method.

0 Kudos
LloydBronn
Occasional Contributor II

Thanks Robert. I realized that I won't be able to use this method because it's returning to the default center in addition to the zoom. If there is a feature located outside of the default extent, I can't navigate to it. 

How would I use a cached basemap if I'm hosting on an ArcGIS server? I can't upload basemaps as part of a map service. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   Sorry your question is not making any sense to me:

How would I use a cached basemap if I'm hosting on an ArcGIS server? I can't upload basemaps as part of a map service. 

Can you clarify what you are wanting to do?

0 Kudos
LloydBronn
Occasional Contributor II

What I'm saying is that my map is not tiled/cached. What I'm trying to do is just restrict the minimum and maximum zoom levels. Earlier in this thread someone said the API does not have this functionality. I tried using this method to revert to the initial extent if someone tries to zoom out too far, but I didn't take into account that this would also send them back to the default center point. If there are any features outside of that level (to the east or west), they can't be accessed. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

  OK now I see. So ignore all the LOD stuff mentioned then and lets get back to your minExtent var.

So you are using:

minExtent = ("map", {
  zoom: 15
});

and as you mentioned earlier that you thought this was the issue, well it absolutely is. You can not get a valid extent with that code. You need to build a true extent class and set the minExtent to that.

So the issue of the extent re-centering at the initial value can be over come using the Extent classes centerAt method:

Extent | API Reference | ArcGIS API for JavaScript 3.18 | centerAt

map.setExtent(maxExtent.centerAt(map.extent.getCenter());
0 Kudos
LloydBronn
Occasional Contributor II

I just figured out how to restrict only the zoom levels. I only need the centerpoint for the initial extent.   Map | API Reference | ArcGIS API for JavaScript 3.18 

map = new Map("map", {
            basemap: "streets",
               center: [-96.8, 38.5],
               maxZoom: 15,
               minZoom: 4
          });‍‍‍‍‍‍