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);
}
});
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.
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.
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?
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.
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());
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
});