Change to getZoomForResolution() (and those who call it) so that it does not choose the closest fit by default. Doing so was causing us problems, because user would drag a zoom box and then the map's new zoom would not contain said zoombox. Not good. Default is back to how it was before, but now there's an option 'closest' for those times when what we really want is the closest. Right now, the only time that's true is when we're switching baselayers. This is based on the work from sandbox/euzuro/zoomToResolution, which is started by reverting r4318. (Closes #1043)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@4792 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -730,19 +730,24 @@ OpenLayers.Layer = OpenLayers.Class({
|
||||
*
|
||||
* Parameters:
|
||||
* bounds - {<OpenLayers.Bounds>}
|
||||
* closest - {Boolean} Find the zoom level that most closely fits the
|
||||
* specified bounds. Note that this may result in a zoom that does
|
||||
* not exactly contain the entire extent.
|
||||
* Default is false.
|
||||
*
|
||||
* Returns:
|
||||
* {Integer} The index of the zoomLevel (entry in the resolutions array)
|
||||
* that still contains the passed-in extent. We do this by calculating
|
||||
* the ideal resolution for the given exteng (based on the map size)
|
||||
* and then find the closest resolution to this ideal resolution.
|
||||
* for the passed-in extent. We do this by calculating the ideal
|
||||
* resolution for the given extent (based on the map size) and then
|
||||
* calling getZoomForResolution(), passing along the 'closest'
|
||||
* parameter.
|
||||
*/
|
||||
getZoomForExtent: function(extent) {
|
||||
getZoomForExtent: function(extent, closest) {
|
||||
var viewSize = this.map.getSize();
|
||||
var idealResolution = Math.max( extent.getWidth() / viewSize.w,
|
||||
extent.getHeight() / viewSize.h );
|
||||
|
||||
return this.getZoomForResolution(idealResolution);
|
||||
return this.getZoomForResolution(idealResolution, closest);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -759,28 +764,39 @@ OpenLayers.Layer = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* APIMethod: getZoomForResolution
|
||||
* Get the index for the closest resolution in the layers resolutions array.
|
||||
*
|
||||
* Parameters:
|
||||
* resolution - {Float} Map units per pixel.
|
||||
* resolution - {Float}
|
||||
* closest - {Boolean} Find the zoom level that corresponds to the absolute
|
||||
* closest resolution, which may result in a zoom whose corresponding
|
||||
* resolution is actually smaller than we would have desired (if this
|
||||
* is being called from a getZoomForExtent() call, then this means that
|
||||
* the returned zoom index might not actually contain the entire
|
||||
* extent specified... but it'll be close).
|
||||
* Default is false.
|
||||
*
|
||||
* Returns:
|
||||
* {Integer} The index of the zoomLevel (entry in the resolutions array)
|
||||
* that is the closest to the passed-in resolution.
|
||||
* that corresponds to the best fit resolution given the passed in
|
||||
* value and the 'closest' specification.
|
||||
*/
|
||||
getZoomForResolution: function(resolution) {
|
||||
var zoom, diff;
|
||||
getZoomForResolution: function(resolution, closest) {
|
||||
var diff;
|
||||
var minDiff = Number.POSITIVE_INFINITY;
|
||||
for(var i=0; i < this.resolutions.length; i++) {
|
||||
diff = Math.abs(this.resolutions[i] - resolution);
|
||||
if(diff < minDiff) {
|
||||
zoom = i;
|
||||
for(var i=0; i < this.resolutions.length; i++) {
|
||||
if (closest) {
|
||||
diff = Math.abs(this.resolutions[i] - resolution);
|
||||
if (diff > minDiff) {
|
||||
break;
|
||||
}
|
||||
minDiff = diff;
|
||||
} else if(diff > minDiff) {
|
||||
break;
|
||||
} else {
|
||||
if (this.resolutions[i] < resolution) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return zoom;
|
||||
return Math.max(0, i-1);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user