OpenLayers OpenLayers

Ticket #1043: zoomer.3.patch

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

zoomer now has a few simple tests.

  • tests/test_Layer.html

    old new  
    158158 
    159159    function test_06_Layer_getZoomForResolution(t) { 
    160160 
    161         t.plan(4); 
     161        t.plan(8); 
    162162 
    163163        var layer = new OpenLayers.Layer('Test Layer'); 
    164164             
     
    170170        t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in"); 
    171171        t.eq(layer.getZoomForResolution(1), 6, "zoom  all the way in"); 
    172172 
     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 
    173179    } 
    174180     
    175181    function test_07_Layer_redraw(t) { 
  • 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 whose  
     735     *     that does 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 smallest resolution that is greater than this 
    739      *     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
    740744     */ 
    741     getZoomForExtent: function(extent) { 
     745    getZoomForExtent: function(extent, closest) { 
    742746        var viewSize = this.map.getSize(); 
    743747        var idealResolution = Math.max( extent.getWidth()  / viewSize.w, 
    744748                                        extent.getHeight() / viewSize.h ); 
    745749 
    746         return this.getZoomForResolution(idealResolution); 
     750        return this.getZoomForResolution(idealResolution, closest); 
    747751    }, 
    748752     
    749753    /**  
     
    763767     *  
    764768     * Parameters: 
    765769     * 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 smallest resolution that is greater than the passed-in 
    770      *     resolution. 
     780     *     that corresponds to the best fit resolution given the passed in  
     781     *     value and the 'closest' specification. 
    771782     */ 
    772     getZoomForResolution: function(resolution) { 
    773          
    774         for(var i=1; i < this.resolutions.length; i++) { 
    775             if ( this.resolutions[i] < resolution) { 
    776                 break; 
     783    getZoomForResolution: function(resolution, closest) { 
     784        var diff; 
     785        var minDiff = Number.POSITIVE_INFINITY; 
     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                } 
     792                minDiff = diff; 
     793            } else { 
     794                if (this.resolutions[i] < resolution) { 
     795                    break; 
     796                } 
    777797            } 
    778798        } 
    779         return (i - 1); 
     799        return Math.max(0, i-1); 
    780800    }, 
    781801     
    782802    /** 
  • 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                }