[OpenLayers-Users] FW: [OpenLayers-Dev] OpenLayer Map restrictpanning and zooming

Worth Lutz wal3 at mindspring.com
Mon Mar 25 12:21:31 PDT 2013


I'm just learning OpenLayers also but I believe that the map settings you
are trying to change are for setting up the map.

 

See:  http://trac.osgeo.org/openlayers/wiki/SettingZoomLevels 

 

This may be off base but you might try is removing the resolutions from the
resolutions array which exceed your max_resolution. 

 

I think this might restrict the zooming.

 

This is just a guess from some things I've learned.

 

Hopefully someone with more experience will have a more definite answer.

 

 

Worth

 

  _____  

From: openlayers-users-bounces at lists.osgeo.org
[mailto:openlayers-users-bounces at lists.osgeo.org] On Behalf Of Bruce Mahfood
Sent: Monday, March 25, 2013 2:12 PM
To: openlayers-users at lists.osgeo.org
Subject: [OpenLayers-Users] FW: [OpenLayers-Dev] OpenLayer Map
restrictpanning and zooming

 

I am new to OpenLayers, and am trying to figure out how to restrict the
amount that a user can pan around and zoom outward. For this application,
the user chooses a local area in the map and clicks a button saying to use
that area, and we want them not to be able to pan outside of or zoom out
greater than that extent. When the button is clicked, I am setting the
maxRestrictedExtent to the current extent using map.GetExtent(), and I am
setting the maxResolution to the current resolution by calling
map.getResolution(). Panning works perfectly, but zooming doesn't. The user
is still able to zoom out passed the given resolution.

 

I have added code below which, every time the extent changes, prints out the
current extent and resolution. On the 6th extent change I set the three map
properties listed above and print out the settings. This allows me to zoom
in from full world view to a localized enough area for testing.  Please
note, I do not mean to leave this code in here, but am just catching this to
test and see what happens after the change at the 6th extent.

 

After the 6th extent change, I zoom out with my mouse wheel and see the
extent and resolution that were chosen. On zooming out with one click of my
mouse wheel, the next printout shows clearly that the resolution is indeed
larger than the setting for map.maxResolution, and I can see that the
viewable area is larger than the one chosen. I have tried a number of
things, including setting maxExtent, but nothing seems to cause the max
zoomable extent to be restricted. I can zoom all the way out to full world
view again if I like.

 

Below you can see the test code that I have written which instantiates a map
with a Navigation control and an OSM layer, and then registers for the map's
moveend event, creating a callback function that does what I have described,
and finally zooms to max extent.

 

Does anyone have any idea as to what I should be doing differently in order
to limit the ability to zoom out to the max extent chosen? I am hoping that
I will not have to manually adjust the extent back to within the max extent,
but if that's the case, at least knowing that would help.

 

P.S. - Unfortunately the whole html isn't shown, since the <html>, <head>,
<title> and <body> tags seem to be processed, along with their closing tags.
The only thing that is really missing though is that I call init() in the
onload event of the body.

 

P.S.S - I have already posted this on StackOverflow, and got an answer
saying that I needed to defined an array of resolutions, with a min and max
resolution set in the map constructor, and then it should work.  I have
added that code into my code below, but that is still not getting me there.
I know I am just around the corner from an answer, but haven't found it yet.
I'm hoping someone reading this list can help me with this.

 

Thanks...

Bruce

 

 

[code]

 

 

    <style type="text/css">

        html, body { width: 100%; height: 100%; margin: 0; }

        #map { width: 100%; height: 100%; margin: 0; }

    </style>

    <script src="http://www.openlayers.org/api/OpenLayers.js"></script>

    <script type="text/javascript">

 

    //Set up a global variable to hold our map object

    var map;

    var num;

 

    function init(){

        map = new OpenLayers.Map('map', {

            controls: [],

            minResolution: 0.074645535, // Added per StackOverflow
conversation

            maxResolution: 156543.0339 // Added per StackOverflow
conversation

        });

        map.addControl(new OpenLayers.Control.Navigation());       

        // Added the serverResolutions array below per StackOverflow
conversation

        map.addLayer(new OpenLayers.Layer.OSM({

            serverResolutions: [156543.0339, 78271.51695, 39135.75848,

                19567.87923828125, 9783.939619140625, 4891.9698095703125,
2445.9849047851562,

                1222.9924523925781, 611.4962261962891, 305.74811309814453,
152.87405654907226,

                76.43702827453613, 38.218514137268066, 19.109257068634033,
9.554628534317017,

                4.777314267158508, 2.388657133579254, 1.194328566789627,
0.5971642833948135,

                0.298582142, 0.149291071, 0.074645535

            ]

        }));

 

        // NOTE: added to trap changes in extent due to pan, zoom, or
window/frame resize

        //      for testing purposes.

        map.events.register("moveend", this, function (e) {

            if (num == 5)

            {

                // NOTE: added to restrict panning/zooming to extent bounds
chosen for the current scenario

                //   - setting restrictedExtent keeps panning within the
bounds, but setting maxResolution

                //     doesn't help keep the zooming from going out further
than the chosen zoom level /

                //     resolution.

                map.restrictedExtent = map.getExtent();

                map.maxResolution = map.getResolution();

                console.log(num++ + ": Setting maxExtent/restrictedExtent: "
+ map.maxExtent +

                                    "; resolution: " + map.maxResolution);

            }

            else

            {

                console.log(num++ + ": Changed to extent: " +
map.getExtent() + "; resolution: " + map.getResolution());

            }

        });

 

        num = 0;

        map.zoomToMaxExtent();

    }

    </script>

 

 

[/code]

 

_______________________________________________ Dev mailing list
Dev at lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/openlayers-dev

  _____  

No virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.2904 / Virus Database: 2641/6201 - Release Date: 03/24/13

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130325/7b35ae8d/attachment-0001.html>


More information about the Users mailing list