OpenLayers OpenLayers

Changeset 4261

Show
Ignore:
Timestamp:
09/13/07 11:27:35 (10 months ago)
Author:
tschaub
Message:

Give the map a restrictedExtent property. Setting this property to some bounds causes map navigation to be limited to those bounds. Depending on the resolution settings, the viewport may still display area outside the restricted extent - though it will be centered on the restricted extent in that case. Using a combination of restrictedExtent and maxResolution, you can limit map navigation to the extent of your data (or any arbitrary extent). See the restricted-extent.html example (closes #340).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Map.js

    r4256 r4261  
    207207    minExtent: null, 
    208208     
     209    /** 
     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 
    209220    /** 
    210221     * APIProperty: numZoomLevels 
     
    10371048     */ 
    10381049    setCenter: function (lonlat, zoom, dragging, forceZoomChange) { 
    1039          
     1050 
    10401051        if (!this.center && !this.isValidLonLat(lonlat)) { 
    10411052            lonlat = this.maxExtent.getCenterLonLat(); 
     1053        } 
     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            } 
    10421090        } 
    10431091         
  • trunk/openlayers/tests/test_Map.html

    r4157 r4261  
    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 );     
     
    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>