Solved! Go to Solution.
dojo.connect(map, "onExtentChange", function (){ var extent = map.extent.getCenter(); if(initialExtent.contains(extent)){} else{map.setExtent(initialExtent)} });
This link is broken.
This sample shows how to set up custom LODs when creating the map: Specify LODs (levels of detail) | ArcGIS API for JavaScript
Has anyone been able to constrain map extent to the initial extent of the map? I am wanting to limit where the user can pan the map. I want to keep the user inside of a giving county or state. Is this possible with the JavaScript API? Any and all help is appreciated.
dojo.connect(map, "onExtentChange", function (){ var extent = map.extent.getCenter(); if(initialExtent.contains(extent)){} else{map.setExtent(initialExtent)} });
// Get the current zoom level on initialization, then every time the map is zoomed. var currentZoom = map.getZoom(); dojo.connect( map, "onZoomEnd", function(){ currentZoom = map.getZoom(); }); // Re-center the map at the current zoom level when the user pans outside of the initial map extent. dojo.connect( map, "onPanEnd", function() { var currCenter = map.extent.getCenter(); if (initialExtent.contains( currCenter)) { } else { map.centerAndZoom( center, currentZoom); } });
I came up with my own version based on what was posted above and things I found elsewhere on the web. Here's a jsFiddle example: Esri Map Limit Extent - JSFiddle
And here's the relevant code:
//This function limits the extent of the map to prevent users from scrolling
//far away from the initial extent.
function limitMapExtent(map) {
var initialExtent = map.extent;
map.on('extent-change', function(event) {
//If the map has moved to the point where it's center is
//outside the initial boundaries, then move it back to the
//edge where it moved out
var currentCenter = map.extent.getCenter();
if (!initialExtent.contains(currentCenter) &&
event.delta.x !== 0 && event.delta.y !== 0) {
var newCenter = map.extent.getCenter();
//check each side of the initial extent and if the
//current center is outside that extent,
//set the new center to be on the edge that it went out on
if (currentCenter.x < initialExtent.xmin) {
newCenter.x = initialExtent.xmin;
}
if (currentCenter.x > initialExtent.xmax) {
newCenter.x = initialExtent.xmax;
}
if (currentCenter.y < initialExtent.ymin) {
newCenter.y = initialExtent.ymin;
}
if (currentCenter.y > initialExtent.ymax) {
newCenter.y = initialExtent.ymax;
}
map.centerAt(newCenter);
}
});
}