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

View File

@@ -21,7 +21,13 @@ ol.ResolutionConstraint.createSnapToResolutions = function(resolutions) {
var z =
ol.array.linearFindNearest(resolutions, resolution, direction);
z = ol.math.clamp(z + delta, 0, resolutions.length - 1);
return resolutions[z];
var index = Math.floor(z);
if (z != index && index < resolutions.length - 1) {
var power = resolutions[index] / resolutions[index + 1];
return resolutions[index] / Math.pow(power, z - index);
} else {
return resolutions[index];
}
} else {
return undefined;
}
@@ -45,14 +51,7 @@ ol.ResolutionConstraint.createSnapToPower = function(power, maxResolution, opt_m
*/
function(resolution, delta, direction) {
if (resolution !== undefined) {
var offset;
if (direction > 0) {
offset = 0;
} else if (direction < 0) {
offset = 1;
} else {
offset = 0.5;
}
var offset = -direction / 2 + 0.5;
var oldLevel = Math.floor(
Math.log(maxResolution / resolution) / Math.log(power) + offset);
var newLevel = Math.max(oldLevel + delta, 0);

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};
};