dojo.connect(map, "onExtentChange", function (){ var extent = map.extent.getCenter(); if(initialExtent.contains(extent)){} else{map.setExtent(initialExtent)} });
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 <BR />//far away from the initial extent.<BR />function limitMapExtent(map) {<BR /> var initialExtent = map.extent;<BR /> map.on('extent-change', function(event) {<BR /> //If the map has moved to the point where it's center is <BR /> //outside the initial boundaries, then move it back to the <BR /> //edge where it moved out<BR /> var currentCenter = map.extent.getCenter();<BR /> if (!initialExtent.contains(currentCenter) && <BR /> event.delta.x !== 0 && event.delta.y !== 0) {<BR /><BR /> var newCenter = map.extent.getCenter();<BR /><BR /> //check each side of the initial extent and if the <BR /> //current center is outside that extent, <BR /> //set the new center to be on the edge that it went out on<BR /> if (currentCenter.x < initialExtent.xmin) {<BR /> newCenter.x = initialExtent.xmin;<BR /> }<BR /> if (currentCenter.x > initialExtent.xmax) {<BR /> newCenter.x = initialExtent.xmax;<BR /> }<BR /> if (currentCenter.y < initialExtent.ymin) {<BR /> newCenter.y = initialExtent.ymin;<BR /> }<BR /> if (currentCenter.y > initialExtent.ymax) {<BR /> newCenter.y = initialExtent.ymax;<BR /> }<BR /> map.centerAt(newCenter);<BR /> }<BR /> });<BR />}
http://arcscripts.esri.com/
This sample shows how to set up custom LODs when creating the map: Specify LODs (levels of detail) | ArcGIS API for JavaScript
This link is broken.
// 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); } });
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.
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.