OpenLayers OpenLayers

Ticket #340: restrictedExtent.patch

File restrictedExtent.patch, 11.0 kB (added by tschaub, 10 months ago)

restrict panning

  • tests/test_Map.html

    old new  
    472472        t.ok( newNumControls == oldNumControls, "removing bad controlid doesnt crash or decrease control count") 
    473473    } 
    474474 
     475    function test_Map_restrictedExtent(t) { 
     476        t.plan(24); 
     477        var extent = new OpenLayers.Bounds(-180, -90, 180, 90); 
     478        var options = { 
     479            maxResolution: "auto" 
     480        }; 
     481        var map = new OpenLayers.Map("map", options); 
     482        var layer = new OpenLayers.Layer.WMS( 
     483            "test",  
     484            "http://octo.metacarta.com/cgi-bin/mapserv?", 
     485            {map: "/mapdata/vmap_wms.map", layers: "basic"} 
     486        ); 
     487        map.addLayer(layer); 
     488        map.zoomToMaxExtent(); 
     489        var nw = new OpenLayers.LonLat(extent.left, extent.top); 
     490        var ne = new OpenLayers.LonLat(extent.right, extent.top); 
     491        var sw = new OpenLayers.LonLat(extent.left, extent.bottom); 
     492        var se = new OpenLayers.LonLat(extent.right, extent.bottom); 
     493         
     494        // try panning to northwest corner 
     495        map.setOptions({restrictedExtent: extent}); 
     496        map.setCenter(nw, 0); 
     497        t.eq(map.getExtent().getCenterLonLat().toString(), 
     498             extent.getCenterLonLat().toString(), 
     499             "map extent properly restricted to northwest at zoom 0"); 
     500        t.eq(map.zoom, 0, "zoom not restricted for nw, 0"); 
     501        map.setCenter(nw, 5); 
     502        t.eq(map.getExtent().top, extent.top, 
     503             "map extent top properly restricted to northwest at zoom 5"); 
     504        t.eq(map.getExtent().left, extent.left, 
     505             "map extent left properly restricted to northwest at zoom 5"); 
     506        t.eq(map.zoom, 5, "zoom not restricted for nw, 5"); 
     507        map.setOptions({restrictedExtent: null}); 
     508        map.setCenter(nw, 0); 
     509        t.eq(map.getExtent().getCenterLonLat().toString(), 
     510             nw.toString(), 
     511             "map extent not restricted with null restrictedExtent for nw"); 
     512 
     513        // try panning to northeast corner 
     514        map.setOptions({restrictedExtent: extent}); 
     515        map.setCenter(ne, 0); 
     516        t.eq(map.getExtent().getCenterLonLat().toString(), 
     517             extent.getCenterLonLat().toString(), 
     518             "map extent properly restricted to northeast at zoom 0"); 
     519        t.eq(map.zoom, 0, "zoom not restricted for ne, 0"); 
     520        map.setCenter(ne, 5); 
     521        t.eq(map.getExtent().top, extent.top, 
     522             "map extent top properly restricted to northeast at zoom 5"); 
     523        t.eq(map.getExtent().right, extent.right, 
     524             "map extent right properly restricted to northeast at zoom 5"); 
     525        t.eq(map.zoom, 5, "zoom not restricted for ne, 5"); 
     526        map.setOptions({restrictedExtent: null}); 
     527        map.setCenter(ne, 0); 
     528        t.eq(map.getExtent().getCenterLonLat().toString(), 
     529             ne.toString(), 
     530             "map extent not restricted with null restrictedExtent for ne"); 
     531         
     532        // try panning to southwest corner 
     533        map.setOptions({restrictedExtent: extent}); 
     534        map.setCenter(sw, 0); 
     535        t.eq(map.getExtent().getCenterLonLat().toString(), 
     536             extent.getCenterLonLat().toString(), 
     537             "map extent properly restricted to southwest at zoom 0"); 
     538        t.eq(map.zoom, 0, "zoom not restricted for sw, 0"); 
     539        map.setCenter(sw, 5); 
     540        t.eq(map.getExtent().bottom, extent.bottom, 
     541             "map extent bottom properly restricted to southwest at zoom 5"); 
     542        t.eq(map.getExtent().left, extent.left, 
     543             "map extent left properly restricted to southwest at zoom 5"); 
     544        t.eq(map.zoom, 5, "zoom not restricted for sw, 5"); 
     545        map.setOptions({restrictedExtent: null}); 
     546        map.setCenter(sw, 0); 
     547        t.eq(map.getExtent().getCenterLonLat().toString(), 
     548             sw.toString(), 
     549             "map extent not restricted with null restrictedExtent for sw"); 
     550 
     551        // try panning to southeast corner 
     552        map.setOptions({restrictedExtent: extent}); 
     553        map.setCenter(se, 0); 
     554        t.eq(map.getExtent().getCenterLonLat().toString(), 
     555             extent.getCenterLonLat().toString(), 
     556             "map extent properly restricted to southeast at zoom 0"); 
     557        t.eq(map.zoom, 0, "zoom not restricted for se, 0"); 
     558        map.setCenter(se, 5); 
     559        t.eq(map.getExtent().bottom, extent.bottom, 
     560             "map extent bottom properly restricted to southeast at zoom 5"); 
     561        t.eq(map.getExtent().right, extent.right, 
     562             "map extent right properly restricted to southeast at zoom 5"); 
     563        t.eq(map.zoom, 5, "zoom not restricted for se, 5"); 
     564        map.setOptions({restrictedExtent: null}); 
     565        map.setCenter(se, 0); 
     566        t.eq(map.getExtent().getCenterLonLat().toString(), 
     567             se.toString(), 
     568             "map extent not restricted with null restrictedExtent for se"); 
     569    } 
     570 
    475571    function test_99_Map_destroy (t) { 
    476572        t.plan( 3 );     
    477573        map = new OpenLayers.Map('map'); 
     
    484580  </script> 
    485581</head> 
    486582<body> 
    487     <div id="map" style="width: 1080px; height: 600px;"/> 
     583    <div id="map" style="width: 600px; height: 300px;"/> 
    488584</body> 
    489585</html> 
  • lib/OpenLayers/Map.js

    old new  
    207207    minExtent: null, 
    208208     
    209209    /** 
     210     * APIProperty: restrictedExtent 
     211     * {<OpenLayers.Bounds>} Limit map navigation to this extent where possible. 
     212     *     If a non-null restrictedExtent is set, panning will be restricted 
     213     *     to the given bounds.  In addition, zooming to a resolution that 
     214     *     displays more than the restricted extent will center the map 
     215     *     on the restricted extent.  If you wish to limit the zoom level 
     216     *     or resolution, use maxResolution. 
     217     */ 
     218    restrictedExtent: null, 
     219 
     220    /** 
    210221     * APIProperty: numZoomLevels 
    211222     * {Integer} Number of zoom levels for the map.  Defaults to 16.  Set a 
    212223     *           different value in the map options if needed. 
     
    10361047     * TBD: reconsider forceZoomChange in 3.0 
    10371048     */ 
    10381049    setCenter: function (lonlat, zoom, dragging, forceZoomChange) { 
    1039          
     1050 
    10401051        if (!this.center && !this.isValidLonLat(lonlat)) { 
    10411052            lonlat = this.maxExtent.getCenterLonLat(); 
    10421053        } 
     1054 
     1055        if(this.restrictedExtent != null) { 
     1056            // In 3.0, decide if we want to change interpretation of maxExtent. 
     1057            if(lonlat == null) {  
     1058                lonlat = this.getCenter();  
     1059            } 
     1060            if(zoom == null) {  
     1061                zoom = this.getZoom();  
     1062            } 
     1063            var resolution = null; 
     1064            if(this.baseLayer != null) { 
     1065                resolution = this.baseLayer.resolutions[zoom]; 
     1066            } 
     1067            var extent = this.calculateBounds(lonlat, resolution);  
     1068            if(!this.restrictedExtent.containsBounds(extent)) { 
     1069                var maxCenter = this.restrictedExtent.getCenterLonLat();  
     1070                if(extent.getWidth() > this.restrictedExtent.getWidth()) {  
     1071                    lonlat = new OpenLayers.LonLat(maxCenter.lon, lonlat.lat);  
     1072                } else if(extent.left < this.restrictedExtent.left) { 
     1073                    lonlat = lonlat.add(this.restrictedExtent.left - 
     1074                                        extent.left, 0);  
     1075                } else if(extent.right > this.restrictedExtent.right) {  
     1076                    lonlat = lonlat.add(this.restrictedExtent.right - 
     1077                                        extent.right, 0);  
     1078                }  
     1079                if(extent.getHeight() > this.restrictedExtent.getHeight()) {  
     1080                    lonlat = new OpenLayers.LonLat(lonlat.lon, maxCenter.lat);  
     1081                } else if(extent.bottom < this.restrictedExtent.bottom) {  
     1082                    lonlat = lonlat.add(0, this.restrictedExtent.bottom - 
     1083                                        extent.bottom);  
     1084                }  
     1085                else if(extent.top > this.restrictedExtent.top) {  
     1086                    lonlat = lonlat.add(0, this.restrictedExtent.top - 
     1087                                        extent.top);  
     1088                }  
     1089            } 
     1090        } 
    10431091         
    10441092        var zoomChanged = forceZoomChange || ( 
    10451093                            (this.isValidZoomLevel(zoom)) &&  
  • examples/restricted-extent.html

    old new  
     1<html xmlns="http://www.w3.org/1999/xhtml"> 
     2  <head> 
     3    <style type="text/css"> 
     4        #map { 
     5            width: 512px; 
     6            height: 256px; 
     7            border: 1px solid #ccc; 
     8        } 
     9    </style> 
     10    <script src="../lib/Firebug/firebug.js"></script> 
     11    <script src="../lib/OpenLayers.js"></script> 
     12    <script type="text/javascript"> 
     13        var map = null; 
     14        var extent = new OpenLayers.Bounds(-180, -90, 180, 90); 
     15 
     16        function init() { 
     17            var options = { 
     18                restrictedExtent: extent 
     19            } 
     20            map = new OpenLayers.Map('map', options); 
     21 
     22            var wms = new OpenLayers.Layer.WMS( 
     23                "OpenLayers WMS",  
     24                "http://labs.metacarta.com/wms/vmap0?", 
     25                {layers: 'basic'} 
     26            );  
     27 
     28            map.addLayers([wms]); 
     29            map.setCenter(extent, 1); 
     30            document.getElementById("toggle").checked = true; 
     31        } 
     32         
     33        function toggleRestrictedExtent() { 
     34            if(map.restrictedExtent == null) { 
     35                map.setOptions({restrictedExtent: extent}); 
     36            } else { 
     37                map.setOptions({restrictedExtent: null}); 
     38            } 
     39        } 
     40    </script> 
     41  </head> 
     42  <body onload="init()"> 
     43    <h3>OpenLayers Restricted Extent Example</h3> 
     44    <div id="map"></div> 
     45    <p> 
     46        Map navigation is limited by a combination of map and layer properties. 
     47        The base layer resolutions array controls the resolutions (or zoom 
     48        levels) available.  The resolutions can be limited by setting a 
     49        maxResolution property or by explicitly specifying a resolutions 
     50        array. 
     51    </p> 
     52    <p> 
     53        Navigation limited by the maxExtent property.  A map cannot be panned 
     54        so that the center of the viewport is outside of the bounds specified 
     55        in maxExtent.  If you wish to further restrict panning, use the 
     56        restrictedExtent property.  With restrictedExtent set, the map cannot 
     57        be panned beyond the given bounds.  If the maxResolution allows the 
     58        map to be zoomed to a resolution that displays an area bigger than 
     59        the restrictedExtent, the viewport will remain centered on the 
     60        restrictedExtent. 
     61    </p> 
     62    <p> 
     63        <input type="checkbox" id="toggle" checked="checked" 
     64               onclick="toggleRestrictedExtent();" /> 
     65        <label for="toggle"> 
     66            Toggle restricted extent (to [-180, -90, 180, 90]). 
     67        </label> 
     68     
     69  </body> 
     70</html>