[OpenLayers-Users] zoomToExtent
Graham Davis
gdavis at refractions.net
Fri Feb 2 19:03:22 EST 2007
I'm trying to zoom to an extent with map.zoomToExtent(bounds), but it
always zooms in 1 zoom level too far (so that the full extent is not
visible in the map viewport without having to pan). I've been looking
through the core code to see how zooming to an extent works, and it
basically gets the center point of the extent and jumps to that with a
zoom level that is determined by baseLayer.getZoomForExtent(extent).
Pasted below are the 2 functions that determine what zoom level is
returned from this function.
The logic looks right, although I'm not sure about this line:
var idealResolution = Math.min( extent.getWidth() / viewSize.w,
extent.getHeight() / viewSize.h );
If I change this to Math.max, jumping to my extents seems to work with
the correct zoom level. Although I'm not sure if this is coincidence or
if it's really the way the code should be working? I've gone through
this documentation: http://trac.openlayers.org/wiki/SettingZoomLevels
and setup my map and layer according to the instructions (by setting the
pre-determined scales, units, and a maxExtents of basically the whole
world). Everything else seems to be working correctly (zooming in and
out, jumping to a center point, etc). Should that Math.min really be a
Math.max? If not, any pointers of what else I can look into that could
be causing this problem? Thanks.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @param {OpenLayers.Bounds} bounds
*
* @returns 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 smallest resolution that
* is greater than this ideal resolution.
* @type int
*/
getZoomForExtent: function(extent) {
var viewSize = this.map.getSize();
var idealResolution = Math.min( extent.getWidth() / viewSize.w,
extent.getHeight() / viewSize.h );
return this.getZoomForResolution(idealResolution);
},
/**
* @param {float} resolution
*
* @returns The index of the zoomLevel (entry in the resolutions array)
* that is the smallest resolution that is greater than the
* passed-in resolution.
* @type int
*/
getZoomForResolution: function(resolution) {
for (var i=1; i < this.resolutions.length; i++) {
if ( this.resolutions[i] < resolution) {
break;
}
}
return (i - 1);
},
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--
Graham Davis
Refractions Research Inc.
gdavis at refractions.net
More information about the Users
mailing list