OpenLayers OpenLayers

Ticket #1043: zoomer.patch

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

first run of patch. all tests seem to be passing ie6/ff. Need to write some new tests still though.

  • 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     *     Default is false. 
    766773     *  
    767774     * Returns: 
    768775     * {Integer} The index of the zoomLevel (entry in the resolutions array)  
    769776     *     that is the smallest resolution that is greater than the passed-in 
    770777     *     resolution. 
    771778     */ 
    772     getZoomForResolution: function(resolution) { 
    773          
    774         for(var i=1; i < this.resolutions.length; i++) { 
    775             if ( this.resolutions[i] < resolution) { 
    776                 break; 
     779    getZoomForResolution: function(resolution, closest) { 
     780        var diff; 
     781        var minDiff = Number.POSITIVE_INFINITY; 
     782        for(var i=0; i < this.resolutions.length; i++) {             
     783            if (closest) { 
     784                diff = Math.abs(this.resolutions[i] - resolution); 
     785                if (diff > minDiff) { 
     786                    break; 
     787                } 
     788                minDiff = diff; 
     789            } else { 
     790                if (this.resolutions[i] < resolution) { 
     791                    break; 
     792                } 
    777793            } 
    778794        } 
    779         return (i - 1); 
     795        return Math.max(0, i-1); 
    780796    }, 
    781797     
    782798    /** 
  • lib/OpenLayers/Map.js

    old new  
    707707                //redraw all layers 
    708708                var center = this.getCenter(); 
    709709                if (center != null) { 
    710                     if (oldExtent == null) { 
    711                         // simply set center but force zoom change 
    712                         this.setCenter( 
    713                             center, 
    714                             this.getZoomForResolution(this.resolution), 
    715                             false, true 
    716                         ); 
    717                     } else { 
    718                         // zoom to oldExtent *and* force zoom change 
    719                         this.setCenter(oldExtent.getCenterLonLat(),  
    720                                        this.getZoomForExtent(oldExtent), 
    721                                        false, true); 
    722                     } 
     710                    //either get the center from the old Extent or just from 
     711                    // the current center of the map.  
     712                    var newCenter = (oldExtent)  
     713                        ? oldExtent.getCenterLonLat() 
     714                        : center; 
     715 
     716                    //the new zoom will either come from the old Extent or  
     717                    // from the current resolution of the map                                                 
     718                    var newZoom = (oldExtent)  
     719                        ? this.getZoomForExtent(oldExtent, true) 
     720                        : this.getZoomForResolution(this.resolution, true); 
     721 
     722                    // zoom and force zoom change 
     723                    this.setCenter(newCenter, newZoom, false, true); 
    723724                } 
    724725 
    725726                this.events.triggerEvent("changebaselayer");