OpenLayers OpenLayers

Ticket #1043: zoomer.TRUNK.3.patch

File zoomer.TRUNK.3.patch, 23.9 kB (added by euzuro, 1 year ago)

adding the latest change to the sandbox from cr5: incorporating 'closest' into getZoomForExtent() on the Map

  • tests/test_Layer.html

    old new  
    158158 
    159159    function test_06_Layer_getZoomForResolution(t) { 
    160160 
    161         t.plan(6); 
     161        t.plan(8); 
    162162 
    163163        var layer = new OpenLayers.Layer('Test Layer'); 
    164164             
     
    166166        layer.resolutions = [128, 64, 32, 16, 8, 4, 2]; 
    167167         
    168168        t.eq(layer.getZoomForResolution(200), 0, "zoom all the way out"); 
    169         t.eq(layer.getZoomForResolution(65), 1, "index closest to 65"); 
    170         t.eq(layer.getZoomForResolution(63), 1, "index closest to 63"); 
    171169        t.eq(layer.getZoomForResolution(25), 2, "zoom in middle"); 
    172170        t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in"); 
    173171        t.eq(layer.getZoomForResolution(1), 6, "zoom  all the way in"); 
    174172 
     173        t.eq(layer.getZoomForResolution(65), 0, "smallest containing res"); 
     174        t.eq(layer.getZoomForResolution(63), 1, "smallest containing res"); 
     175 
     176        t.eq(layer.getZoomForResolution(65, true), 1, "closest res"); 
     177        t.eq(layer.getZoomForResolution(63, true), 1, "closest res"); 
     178 
    175179    } 
    176180     
    177181    function test_07_Layer_redraw(t) { 
  • tests/Control/test_OverviewMap.html

    old new  
    4343        var overviewZoom = control.ovmap.getZoom(); 
    4444        t.eq(overviewCenter.lon, -71, "Overviewmap center lon correct"); 
    4545        t.eq(overviewCenter.lat, 42, "Overviewmap center lat correct"); 
    46         t.eq(overviewZoom, 9, "Overviewmap zoomcorrect"); 
     46        t.eq(overviewZoom, 8, "Overviewmap zoomcorrect"); 
    4747         
    4848        control.mapDivClick({'xy':new OpenLayers.Pixel(5,5)}); 
    4949         
    50         // There are box model issues that keep browsers from giving us 
    51         // identical results here.  Test the normalized difference against 
    52         // a tolerance instead of testing equality. 
    53         function normalizedDiff(a, b) { 
    54             return Math.abs((a - b) / (a + b)); 
    55         } 
    56         var tolerance = 1e-4; 
    57          
    5850        var cent = map.getCenter(); 
    59         t.ok(normalizedDiff(cent.lon, -71.00390625) < tolerance, 
    60              "Clicking on the Overview Map has the correct effect on map lon"); 
    61         t.ok(normalizedDiff(cent.lat, 42.00390625) < tolerance, 
    62              "Clicking on the Overview Map has the correct effect on map lat"); 
     51        t.eq(cent.lon, -71.3515625, "Clicking on the Overview Map has the correct effect on map lon"); 
     52        t.eq(cent.lat, 42.17578125, "Clicking on the Overview Map has the correct effect on map lat"); 
    6353 
    6454        control.rectMouseDown({'xy':new OpenLayers.Pixel(5,5), 'which':1}); 
    6555        control.rectMouseMove({'xy':new OpenLayers.Pixel(15,15), 'which':1}); 
    6656        control.rectMouseUp({'xy':new OpenLayers.Pixel(15,15), 'which':1}); 
    6757         
    6858        var cent = map.getCenter(); 
    69         t.ok(normalizedDiff(cent.lon, -70.83984375) < tolerance, 
    70              "Dragging on the Overview Map has the correct effect on map lon"); 
    71         t.ok(normalizedDiff(cent.lat, 41.84765625) < tolerance, 
    72              "Dragging on the Overview Map has the correct effect on map lat"); 
     59        t.eq(cent.lon, -71.2734375, "Dragging on the Overview Map has the correct effect on map lon"); 
     60        t.eq(cent.lat, 42.09765625, "Dragging on the Overview Map has the correct effect on map lat"); 
    7361         
    7462        map.setCenter(new OpenLayers.LonLat(0,0), 0); 
    7563        var overviewCenter = control.ovmap.getCenter(); 
  • tests/Layer/test_WrapDateLine.html

    old new  
    131131        var m = new OpenLayers.Map('map'); 
    132132        m.addLayer(layer); 
    133133        m.zoomToMaxExtent(); 
    134         t.eq(layer.grid[3][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C0%2C90%2C90&WIDTH=256&HEIGHT=256", "cell [3][0] is wrapped around the world."); 
    135         t.eq(layer.grid[0][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C270%2C90%2C360&WIDTH=256&HEIGHT=256", "cell [0][0] is wrapped around the world lon, but not lat"); 
    136         t.eq(layer.grid[0][3].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-90%2C270%2C0%2C360&WIDTH=256&HEIGHT=256", "cell [3][0] is not wrapped at all."); 
     134        t.eq(layer.grid[3][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C-90%2C180%2C90&WIDTH=256&HEIGHT=256", "cell [3][0] is wrapped around the world."); 
     135        t.eq(layer.grid[0][0].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C450%2C180%2C630&WIDTH=256&HEIGHT=256", "cell [0][0] is wrapped around the world lon, but not lat"); 
     136        t.eq(layer.grid[0][3].url, "http://octo.metacarta.com/cgi-bin/mapserv?MAP=%2Fmapdata%2Fvmap_wms.map&LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-180%2C450%2C0%2C630&WIDTH=256&HEIGHT=256", "cell [3][0] is not wrapped at all."); 
    137137 
    138138    } 
    139139    function test_Layer_WrapDateLine_KaMap (t) { 
     
    146146        var m = new OpenLayers.Map('map'); 
    147147        m.addLayer(layer); 
    148148        m.zoomToMaxExtent(); 
    149         t.eq(layer.grid[0][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=0&s=110735960.625", "grid[0][0] kamap is okay"); 
    150         t.eq(layer.grid[0][3].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=-256&s=110735960.625", "grid[0][3] kamap is okay"); 
    151         t.eq(layer.grid[3][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-512&l=0&s=110735960.625", "grid[3][0] is okay"); 
     149        t.eq(layer.grid[0][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=0&s=221471921.25", "grid[0][0] kamap is okay"); 
     150        t.eq(layer.grid[0][3].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-1280&l=-256&s=221471921.25", "grid[0][3] kamap is okay"); 
     151        t.eq(layer.grid[3][0].url, "http://www.openlayers.org/world/index.php?g=satellite&map=world&i=jpeg&t=-512&l=0&s=221471921.25", "grid[3][0] is okay"); 
    152152    } 
    153153    function test_Layer_WrapDateLine_WMS_Overlay (t) { 
    154154        t.plan( 3 ); 
     
    163163        var m = new OpenLayers.Map('map'); 
    164164        m.addLayers([baselayer,layer]); 
    165165        m.zoomToMaxExtent(); 
    166         t.eq(layer.grid[0][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C270%2C90%2C360&WIDTH=256&HEIGHT=256", "grid[0][0] wms overlay is okay"); 
    167         t.eq(layer.grid[0][3].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-90%2C270%2C0%2C360&WIDTH=256&HEIGHT=256", "grid[0][3] wms overlay is okay"); 
    168         t.eq(layer.grid[3][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C0%2C90%2C90&WIDTH=256&HEIGHT=256", "grid[3][0] wms overlay okay"); 
     166        t.eq(layer.grid[0][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C450%2C180%2C630&WIDTH=256&HEIGHT=256", "grid[0][0] wms overlay is okay"); 
     167        t.eq(layer.grid[0][3].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-180%2C450%2C0%2C630&WIDTH=256&HEIGHT=256", "grid[0][3] wms overlay is okay"); 
     168        t.eq(layer.grid[3][0].url, "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=bathymetry%2Cland_fn%2Cpark%2Cdrain_fn%2Cdrainage%2Cprov_bound%2Cfedlimit%2Crail%2Croad%2Cpopplace&TRANSPARENT=true&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=0%2C-90%2C180%2C90&WIDTH=256&HEIGHT=256", "grid[3][0] wms overlay okay"); 
    169169    } 
    170170 
    171171  </script> 
  • tests/Layer/test_TileCache.html

    old new  
    7878 
    7979        bounds = new OpenLayers.Bounds(10,10,12,12); 
    8080        zoom = layer.getZoomForExtent(bounds); 
    81         /** 
    82          * ideal resolution: 2 map units / 500px = 0.004 
    83          * layer.resolutions = [0.703125, 0.3515625, 0.17578125, 
    84          *                      0.087890625, 0.0439453125, 0.02197265625, 
    85          *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
    86          *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
    87          *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125, 
    88          *                      0.000021457672119140625] 
    89          * 
    90          * So, we expect a zoom of 8 because it is the closest resolution. 
    91          */ 
    92         t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
    9381 
     82        t.eq( zoom, 7, "getZoomForExtent() returns correct value"); 
     83 
    9484        bounds = new OpenLayers.Bounds(10,10,100,100); 
    9585        zoom = layer.getZoomForExtent(bounds); 
    96         /** 
    97          * ideal resolution: 90 map units / 500px = 0.18 
    98          * So, we expect a zoom of 2 because it is the closest. 
    99          */ 
    10086 
    101         t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
     87        t.eq( zoom, 1, "getZoomForExtent() returns correct value"); 
    10288    }    
    10389 
    10490 
  • tests/Layer/test_Grid.html

    old new  
    150150        bounds = new OpenLayers.Bounds(10,10,12,12); 
    151151        zoom = layer.getZoomForExtent(bounds); 
    152152 
    153         /** 
    154          * ideal resolution: 2 map units / 500px = 0.004 
    155          * layer.resolutions = [1.40625, 0.703125, 0.3515625, 0.17578125, 
    156          *                      0.087890625, 0.0439453125, 0.02197265625, 
    157          *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
    158          *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
    159          *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125] 
    160          * 
    161          * So, we expect a zoom of 9 because it is the closest resolution. 
    162          */ 
    163         t.eq( zoom, 9, "getZoomForExtent() returns correct value"); 
     153        t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
    164154 
    165155        bounds = new OpenLayers.Bounds(10,10,100,100); 
    166156        zoom = layer.getZoomForExtent(bounds); 
    167157 
    168         /** 
    169          * ideal resolution: 90 map units / 500px = 0.18 
    170          * So, we expect a zoom of 3 because it is the closest. 
    171          */ 
    172         t.eq( zoom, 3, "getZoomForExtent() returns correct value"); 
     158        t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
    173159    }    
    174160     
    175161    function test_07_Layer_Grid_moveTo(t) { 
     
    555541        map.zoomToMaxExtent(); 
    556542        map.zoomIn(); 
    557543        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    558         t.eq(bounds.toBBOX(), "-90,0,0,90", "get tile bounds returns correct bounds");  
     544        t.eq(bounds.toBBOX(), "-180,-90,0,90", "get tile bounds returns correct bounds");  
    559545        map.pan(200,0); 
    560546        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    561         t.eq(bounds.toBBOX(), "0,0,90,90", "get tile bounds returns correct bounds after pan");  
     547        t.eq(bounds.toBBOX(), "0,-90,180,90", "get tile bounds returns correct bounds after pan");  
    562548    } 
    563549 
    564550    function test_Layer_Grid_moveTo_buffer_calculation (t) { 
  • tests/Layer/test_KaMap.html

    old new  
    128128        bounds = new OpenLayers.Bounds(10,10,12,12); 
    129129        zoom = layer.getZoomForExtent(bounds); 
    130130 
    131         /** 
    132          * ideal resolution: 2 map units / 500px = 0.004 
    133          * layer.resolutions = [1.40625, 0.703125, 0.3515625, 0.17578125, 
    134          *                      0.087890625, 0.0439453125, 0.02197265625, 
    135          *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
    136          *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
    137          *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125] 
    138          * 
    139          * So, we expect a zoom of 9 because it is the closest resolution. 
    140          */ 
    141         t.eq( zoom, 9, "getZoomForExtent() returns correct value"); 
     131        t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
    142132 
    143133        bounds = new OpenLayers.Bounds(10,10,100,100); 
    144134        zoom = layer.getZoomForExtent(bounds); 
    145135 
    146         /** 
    147          * ideal resolution: 90 map units / 500px = 0.18 
    148          * So, we expect a zoom of 3 because it is the closest. 
    149          */ 
    150         t.eq( zoom, 3, "getZoomForExtent() returns correct value"); 
     136        t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
    151137    }    
    152138     
    153139    function test_06_Layer_kaMap_mergeNewParams (t) { 
     
    250236        map.zoomToMaxExtent(); 
    251237        map.zoomIn(); 
    252238        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    253         t.eq(bounds.toBBOX(), "-90,0,0,90", "get tile bounds returns correct bounds");  
     239        t.eq(bounds.toBBOX(), "-180,0,0,180", "get tile bounds returns correct bounds");  
    254240        map.pan(200,0); 
    255241        var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); 
    256         t.eq(bounds.toBBOX(), "0,0,90,90", "get tile bounds returns correct bounds after pan");  
     242        t.eq(bounds.toBBOX(), "0,0,180,180", "get tile bounds returns correct bounds after pan");  
    257243    } 
    258244 
    259245    function test_99_Layer_KaMap_destroy (t) { 
  • tests/Layer/test_TMS.html

    old new  
    7777 
    7878        bounds = new OpenLayers.Bounds(10,10,12,12); 
    7979        zoom = layer.getZoomForExtent(bounds); 
    80         /** 
    81          * ideal resolution: 2 map units / 500px = 0.004 
    82          * layer.resolutions = [1.40625, 0.703125, 0.3515625, 0.17578125, 
    83          *                      0.087890625, 0.0439453125, 0.02197265625, 
    84          *                      0.010986328125, 0.0054931640625, 0.00274658203125, 
    85          *                      0.001373291015625, 0.0006866455078125, 0.00034332275390625, 
    86          *                      0.000171661376953125, 0.0000858306884765625, 0.00004291534423828125] 
    87          * 
    88          * So, we expect a zoom of 9 because it is the closest resolution. 
    89          */ 
    90          
    91         t.eq( zoom, 9, "getZoomForExtent() returns correct value"); 
    9280 
     81        t.eq( zoom, 8, "getZoomForExtent() returns correct value"); 
     82 
    9383        bounds = new OpenLayers.Bounds(10,10,100,100); 
    9484        zoom = layer.getZoomForExtent(bounds); 
    95         /** 
    96          * ideal resolution: 90 map units / 500px = 0.18 
    97          * So, we expect a zoom of 3 because it is the closest. 
    98          */ 
    99         t.eq( zoom, 3, "getZoomForExtent() returns correct value"); 
     85 
     86        t.eq( zoom, 2, "getZoomForExtent() returns correct value"); 
    10087    }    
    10188 
    10289 
  • lib/OpenLayers/Layer.js

    old new  
    730730     *  
    731731     * Parameters: 
    732732     * bounds - {<OpenLayers.Bounds>} 
     733     * closest - {Boolean} Find the zoom level that corresponds to the absolute  
     734     *     closest for the given extent, which may result in a zoom that does  
     735     *     not exactly contain the entire extent. 
     736     *     Default is false. 
    733737     * 
    734738     * Returns: 
    735739     * {Integer} The index of the zoomLevel (entry in the resolutions array)  
    736      *     that still contains the passed-in extent. We do this by calculating 
    737      *     the ideal resolution for the given exteng (based on the map size) 
    738      *     and then find the closest resolution to this ideal resolution. 
     740     *     for the passed-in extent. We do this by calculating the ideal  
     741     *     resolution for the given extent (based on the map size) and then  
     742     *     calling getZoomForResolution(), passing along the 'closest' 
     743     *     parameter. 
    739744     */ 
    740     getZoomForExtent: function(extent) { 
     745    getZoomForExtent: function(extent, closest) { 
    741746        var viewSize = this.map.getSize(); 
    742747        var idealResolution = Math.max( extent.getWidth()  / viewSize.w, 
    743748                                        extent.getHeight() / viewSize.h ); 
    744749 
    745         return this.getZoomForResolution(idealResolution); 
     750        return this.getZoomForResolution(idealResolution, closest); 
    746751    }, 
    747752     
    748753    /**  
     
    759764 
    760765    /** 
    761766     * APIMethod: getZoomForResolution 
    762      * Get the index for the closest resolution in the layers resolutions array. 
    763767     *  
    764768     * Parameters: 
    765      * resolution - {Float} Map units per pixel. 
     769     * resolution - {Float} 
     770     * closest - {Boolean} Find the zoom level that corresponds to the absolute  
     771     *     closest resolution, which may result in a zoom whose corresponding 
     772     *     resolution is actually smaller than we would have desired (if this 
     773     *     is being called from a getZoomForExtent() call, then this means that 
     774     *     the returned zoom index might not actually contain the entire  
     775     *     extent specified... but it'll be close). 
     776     *     Default is false. 
    766777     *  
    767778     * Returns: 
    768779     * {Integer} The index of the zoomLevel (entry in the resolutions array)  
    769      *     that is the closest to the passed-in resolution. 
     780     *     that corresponds to the best fit resolution given the passed in  
     781     *     value and the 'closest' specification. 
    770782     */ 
    771     getZoomForResolution: function(resolution) { 
    772         var zoom, diff; 
     783    getZoomForResolution: function(resolution, closest) { 
     784        var diff; 
    773785        var minDiff = Number.POSITIVE_INFINITY; 
    774         for(var i=0; i < this.resolutions.length; i++) { 
    775             diff = Math.abs(this.resolutions[i] - resolution); 
    776             if(diff < minDiff) { 
    777                 zoom = i; 
     786        for(var i=0; i < this.resolutions.length; i++) {             
     787            if (closest) { 
     788                diff = Math.abs(this.resolutions[i] - resolution); 
     789                if (diff > minDiff) { 
     790                    break; 
     791                } 
    778792                minDiff = diff; 
    779             } else if(diff > minDiff) { 
    780                 break; 
     793            } else { 
     794                if (this.resolutions[i] < resolution) { 
     795                    break; 
     796                } 
    781797            } 
    782798        } 
    783         return zoom
     799        return Math.max(0, i-1)
    784800    }, 
    785801     
    786802    /** 
  • lib/OpenLayers/Map.js

    old new  
    711711                        // simply set center but force zoom change 
    712712                        this.setCenter( 
    713713                            center, 
    714                             this.getZoomForResolution(this.resolution), 
     714                            this.getZoomForResolution(this.resolution, true), 
    715715                            false, true 
    716716                        ); 
    717717                    } else { 
    718718                        // zoom to oldExtent *and* force zoom change 
    719719                        this.setCenter(oldExtent.getCenterLonLat(),  
    720                                        this.getZoomForExtent(oldExtent), 
     720                                       this.getZoomForExtent(oldExtent, true), 
    721721                                       false, true); 
    722722                    } 
    723723                } 
     
    13671367     *  
    13681368     * Parameters:  
    13691369     * bounds - {<OpenLayers.Bounds>} 
     1370     * closest - {Boolean} Find the zoom level that most closely fits the  
     1371     *     specified bounds. Note that this may result in a zoom that does  
     1372     *     not exactly contain the entire extent. 
     1373     *     Default is false. 
    13701374     *  
    13711375     * Returns: 
    13721376     * {Integer} A suitable zoom level for the specified bounds. 
    13731377     *           If no baselayer is set, returns null. 
    13741378     */ 
    1375     getZoomForExtent: function (bounds) { 
     1379    getZoomForExtent: function (bounds, closest) { 
    13761380        var zoom = null; 
    13771381        if (this.baseLayer != null) { 
    1378             zoom = this.baseLayer.getZoomForExtent(bounds); 
     1382            zoom = this.baseLayer.getZoomForExtent(bounds, closest); 
    13791383        } 
    13801384        return zoom; 
    13811385    }, 
     
    13851389     *  
    13861390     * Parameter: 
    13871391     * resolution - {Float} 
     1392     * closest - {Boolean} Find the zoom level that corresponds to the absolute  
     1393     *     closest resolution, which may result in a zoom whose corresponding 
     1394     *     resolution is actually smaller than we would have desired (if this 
     1395     *     is being called from a getZoomForExtent() call, then this means that 
     1396     *     the returned zoom index might not actually contain the entire  
     1397     *     extent specified... but it'll be close). 
     1398     *     Default is false. 
    13881399     *  
    13891400     * Returns: 
    13901401     * {Integer} A suitable zoom level for the specified resolution. 
    13911402     *           If no baselayer is set, returns null. 
    13921403     */ 
    1393     getZoomForResolution: function(resolution) { 
     1404    getZoomForResolution: function(resolution, closest) { 
    13941405        var zoom = null; 
    13951406        if (this.baseLayer != null) { 
    1396             zoom = this.baseLayer.getZoomForResolution(resolution); 
     1407            zoom = this.baseLayer.getZoomForResolution(resolution, closest); 
    13971408        } 
    13981409        return zoom; 
    13991410    },