Merge pull request #835 from elemoine/setzoom

Add ol.View2D.prototype.setZoom
This commit is contained in:
Éric Lemoine
2013-07-05 12:31:34 -07:00
4 changed files with 30 additions and 44 deletions

View File

@@ -11,33 +11,6 @@ goog.require('ol.array');
ol.ResolutionConstraintType;
/**
* @param {number} power Power.
* @param {number} maxResolution Maximum resolution.
* @param {number=} opt_minResolution Minimum resolution.
* @return {ol.ResolutionConstraintType} Zoom function.
*/
ol.ResolutionConstraint.createContinuous =
function(power, maxResolution, opt_minResolution) {
var minResolution = opt_minResolution || 0;
return (
/**
* @param {number|undefined} resolution Resolution.
* @param {number} delta Delta.
* @param {number} direction Direction.
* @return {number|undefined} Resolution.
*/
function(resolution, delta, direction) {
if (goog.isDef(resolution)) {
resolution /= Math.pow(power, delta);
return goog.math.clamp(resolution, minResolution, maxResolution);
} else {
return undefined;
}
});
};
/**
* @param {Array.<number>} resolutions Resolutions.
* @return {ol.ResolutionConstraintType} Zoom function.

View File

@@ -4,3 +4,4 @@
@exportProperty ol.View2D.prototype.constrainRotation
@exportProperty ol.View2D.prototype.fitExtent
@exportProperty ol.View2D.prototype.getView2D
@exportProperty ol.View2D.prototype.setZoom

View File

@@ -59,21 +59,22 @@ ol.View2D = function(opt_options) {
values[ol.View2DProperty.PROJECTION] = ol.proj.createProjection(
options.projection, 'EPSG:3857');
var parts = ol.View2D.createResolutionConstraint_(options);
var resolutionConstraintInfo = ol.View2D.createResolutionConstraint_(
options);
/**
* @private
* @type {number}
*/
this.maxResolution_ = parts[1];
this.maxResolution_ = resolutionConstraintInfo.maxResolution;
/**
* @private
* @type {number}
*/
this.minResolution_ = parts[2];
this.minResolution_ = resolutionConstraintInfo.minResolution;
var resolutionConstraint = parts[0];
var resolutionConstraint = resolutionConstraintInfo.constraint;
var rotationConstraint = ol.View2D.createRotationConstraint_(options);
/**
@@ -86,7 +87,7 @@ ol.View2D = function(opt_options) {
if (goog.isDef(options.resolution)) {
values[ol.View2DProperty.RESOLUTION] = options.resolution;
} else if (goog.isDef(options.zoom)) {
values[ol.View2DProperty.RESOLUTION] = resolutionConstraint(
values[ol.View2DProperty.RESOLUTION] = this.constrainResolution(
this.maxResolution_, options.zoom);
}
values[ol.View2DProperty.ROTATION] =
@@ -398,11 +399,21 @@ goog.exportProperty(
ol.View2D.prototype.setRotation);
/**
* Zoom to a specific zoom level.
* @param {number} zoom Zoom level.
*/
ol.View2D.prototype.setZoom = function(zoom) {
var resolution = this.constrainResolution(this.maxResolution_, zoom, 0);
this.setResolution(resolution);
};
/**
* @private
* @param {ol.View2DOptions} options View2D options.
* @return {Array} Array of three elements: the resolution constraint,
* maxResolution, and minResolution.
* @return {{constraint: ol.ResolutionConstraintType, maxResolution: number,
* minResolution: number}}
*/
ol.View2D.createResolutionConstraint_ = function(options) {
var resolutionConstraint;
@@ -440,7 +451,8 @@ ol.View2D.createResolutionConstraint_ = function(options) {
resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
zoomFactor, maxResolution, maxZoom);
}
return [resolutionConstraint, maxResolution, minResolution];
return {constraint: resolutionConstraint, maxResolution: maxResolution,
minResolution: minResolution};
};

View File

@@ -8,7 +8,7 @@ describe('ol.View2D', function() {
describe('with no options', function() {
it('gives a correct resolution constraint function', function() {
var options = {};
var fn = ol.View2D.createResolutionConstraint_(options)[0];
var fn = ol.View2D.createResolutionConstraint_(options).constraint;
expect(fn(156543.03392804097, 0, 0))
.to.roughlyEqual(156543.03392804097, 1e-9);
expect(fn(78271.51696402048, 0, 0))
@@ -24,12 +24,12 @@ describe('ol.View2D', function() {
maxZoom: 3,
zoomFactor: 3
};
var parts = ol.View2D.createResolutionConstraint_(options);
var maxResolution = parts[1];
var info = ol.View2D.createResolutionConstraint_(options);
var maxResolution = info.maxResolution;
expect(maxResolution).to.eql(81);
var minResolution = parts[2];
var minResolution = info.minResolution;
expect(minResolution).to.eql(3);
var fn = parts[0];
var fn = info.constraint;
expect(fn(82, 0, 0)).to.eql(81);
expect(fn(81, 0, 0)).to.eql(81);
expect(fn(27, 0, 0)).to.eql(27);
@@ -44,12 +44,12 @@ describe('ol.View2D', function() {
var options = {
resolutions: [97, 76, 65, 54, 0.45]
};
var parts = ol.View2D.createResolutionConstraint_(options);
var maxResolution = parts[1];
var info = ol.View2D.createResolutionConstraint_(options);
var maxResolution = info.maxResolution;
expect(maxResolution).to.eql(97);
var minResolution = parts[2];
var minResolution = info.minResolution;
expect(minResolution).to.eql(0.45);
var fn = parts[0];
var fn = info.constraint;
expect(fn(97, 0, 0)).to.eql(97);
expect(fn(76, 0, 0)).to.eql(76);
expect(fn(65, 0, 0)).to.eql(65);