Support fractional zoom levels in ol.View#getZoom and #setZoom

This commit is contained in:
Andreas Hocevar
2016-08-02 22:52:57 +02:00
parent 3d57ea45cb
commit 98fbfe4b67
3 changed files with 67 additions and 29 deletions
+27 -16
View File
@@ -134,6 +134,12 @@ ol.View = function(opt_options) {
*/
this.minResolution_ = resolutionConstraintInfo.minResolution;
/**
* @private
* @type {number}
*/
this.zoomFactor_ = resolutionConstraintInfo.zoomFactor;
/**
* @private
* @type {Array.<number>|undefined}
@@ -453,27 +459,32 @@ ol.View.prototype.getState = function() {
/**
* Get the current zoom level. Return undefined if the current
* resolution is undefined or not a "constrained resolution".
* resolution is undefined or not within the "resolution constraints".
* @return {number|undefined} Zoom.
* @api stable
*/
ol.View.prototype.getZoom = function() {
var offset;
var zoom;
var resolution = this.getResolution();
if (resolution !== undefined) {
var res, z = 0;
do {
res = this.constrainResolution(this.maxResolution_, z);
if (res == resolution) {
offset = z;
break;
if (resolution !== undefined &&
resolution >= this.minResolution_ && resolution <= this.maxResolution_) {
var offset = this.minZoom_ || 0;
var max, zoomFactor;
if (this.resolutions_) {
var nearest = ol.array.linearFindNearest(this.resolutions_, resolution, 1);
offset += nearest;
if (nearest == this.resolutions_.length - 1) {
return offset;
}
++z;
} while (res > this.minResolution_);
max = this.resolutions_[nearest];
zoomFactor = max / this.resolutions_[nearest + 1];
} else {
max = this.maxResolution_;
zoomFactor = this.zoomFactor_;
}
zoom = offset + Math.log(max / resolution) / Math.log(zoomFactor);
}
return offset !== undefined ? this.minZoom_ + offset : offset;
return zoom;
};
@@ -689,7 +700,7 @@ ol.View.createCenterConstraint_ = function(options) {
* @private
* @param {olx.ViewOptions} options View options.
* @return {{constraint: ol.ResolutionConstraintType, maxResolution: number,
* minResolution: number}} The constraint.
* minResolution: number, zoomFactor: number}} The constraint.
*/
ol.View.createResolutionConstraint_ = function(options) {
var resolutionConstraint;
@@ -763,7 +774,7 @@ ol.View.createResolutionConstraint_ = function(options) {
zoomFactor, maxResolution, maxZoom - minZoom);
}
return {constraint: resolutionConstraint, maxResolution: maxResolution,
minResolution: minResolution, minZoom: minZoom};
minResolution: minResolution, minZoom: minZoom, zoomFactor: zoomFactor};
};