Merge pull request #835 from elemoine/setzoom
Add ol.View2D.prototype.setZoom
This commit is contained in:
@@ -11,33 +11,6 @@ goog.require('ol.array');
|
|||||||
ol.ResolutionConstraintType;
|
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.
|
* @param {Array.<number>} resolutions Resolutions.
|
||||||
* @return {ol.ResolutionConstraintType} Zoom function.
|
* @return {ol.ResolutionConstraintType} Zoom function.
|
||||||
|
|||||||
@@ -4,3 +4,4 @@
|
|||||||
@exportProperty ol.View2D.prototype.constrainRotation
|
@exportProperty ol.View2D.prototype.constrainRotation
|
||||||
@exportProperty ol.View2D.prototype.fitExtent
|
@exportProperty ol.View2D.prototype.fitExtent
|
||||||
@exportProperty ol.View2D.prototype.getView2D
|
@exportProperty ol.View2D.prototype.getView2D
|
||||||
|
@exportProperty ol.View2D.prototype.setZoom
|
||||||
|
|||||||
+20
-8
@@ -59,21 +59,22 @@ ol.View2D = function(opt_options) {
|
|||||||
values[ol.View2DProperty.PROJECTION] = ol.proj.createProjection(
|
values[ol.View2DProperty.PROJECTION] = ol.proj.createProjection(
|
||||||
options.projection, 'EPSG:3857');
|
options.projection, 'EPSG:3857');
|
||||||
|
|
||||||
var parts = ol.View2D.createResolutionConstraint_(options);
|
var resolutionConstraintInfo = ol.View2D.createResolutionConstraint_(
|
||||||
|
options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
this.maxResolution_ = parts[1];
|
this.maxResolution_ = resolutionConstraintInfo.maxResolution;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
this.minResolution_ = parts[2];
|
this.minResolution_ = resolutionConstraintInfo.minResolution;
|
||||||
|
|
||||||
var resolutionConstraint = parts[0];
|
var resolutionConstraint = resolutionConstraintInfo.constraint;
|
||||||
var rotationConstraint = ol.View2D.createRotationConstraint_(options);
|
var rotationConstraint = ol.View2D.createRotationConstraint_(options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,7 +87,7 @@ ol.View2D = function(opt_options) {
|
|||||||
if (goog.isDef(options.resolution)) {
|
if (goog.isDef(options.resolution)) {
|
||||||
values[ol.View2DProperty.RESOLUTION] = options.resolution;
|
values[ol.View2DProperty.RESOLUTION] = options.resolution;
|
||||||
} else if (goog.isDef(options.zoom)) {
|
} else if (goog.isDef(options.zoom)) {
|
||||||
values[ol.View2DProperty.RESOLUTION] = resolutionConstraint(
|
values[ol.View2DProperty.RESOLUTION] = this.constrainResolution(
|
||||||
this.maxResolution_, options.zoom);
|
this.maxResolution_, options.zoom);
|
||||||
}
|
}
|
||||||
values[ol.View2DProperty.ROTATION] =
|
values[ol.View2DProperty.ROTATION] =
|
||||||
@@ -398,11 +399,21 @@ goog.exportProperty(
|
|||||||
ol.View2D.prototype.setRotation);
|
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
|
* @private
|
||||||
* @param {ol.View2DOptions} options View2D options.
|
* @param {ol.View2DOptions} options View2D options.
|
||||||
* @return {Array} Array of three elements: the resolution constraint,
|
* @return {{constraint: ol.ResolutionConstraintType, maxResolution: number,
|
||||||
* maxResolution, and minResolution.
|
* minResolution: number}}
|
||||||
*/
|
*/
|
||||||
ol.View2D.createResolutionConstraint_ = function(options) {
|
ol.View2D.createResolutionConstraint_ = function(options) {
|
||||||
var resolutionConstraint;
|
var resolutionConstraint;
|
||||||
@@ -440,7 +451,8 @@ ol.View2D.createResolutionConstraint_ = function(options) {
|
|||||||
resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
|
resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
|
||||||
zoomFactor, maxResolution, maxZoom);
|
zoomFactor, maxResolution, maxZoom);
|
||||||
}
|
}
|
||||||
return [resolutionConstraint, maxResolution, minResolution];
|
return {constraint: resolutionConstraint, maxResolution: maxResolution,
|
||||||
|
minResolution: minResolution};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ describe('ol.View2D', function() {
|
|||||||
describe('with no options', function() {
|
describe('with no options', function() {
|
||||||
it('gives a correct resolution constraint function', function() {
|
it('gives a correct resolution constraint function', function() {
|
||||||
var options = {};
|
var options = {};
|
||||||
var fn = ol.View2D.createResolutionConstraint_(options)[0];
|
var fn = ol.View2D.createResolutionConstraint_(options).constraint;
|
||||||
expect(fn(156543.03392804097, 0, 0))
|
expect(fn(156543.03392804097, 0, 0))
|
||||||
.to.roughlyEqual(156543.03392804097, 1e-9);
|
.to.roughlyEqual(156543.03392804097, 1e-9);
|
||||||
expect(fn(78271.51696402048, 0, 0))
|
expect(fn(78271.51696402048, 0, 0))
|
||||||
@@ -24,12 +24,12 @@ describe('ol.View2D', function() {
|
|||||||
maxZoom: 3,
|
maxZoom: 3,
|
||||||
zoomFactor: 3
|
zoomFactor: 3
|
||||||
};
|
};
|
||||||
var parts = ol.View2D.createResolutionConstraint_(options);
|
var info = ol.View2D.createResolutionConstraint_(options);
|
||||||
var maxResolution = parts[1];
|
var maxResolution = info.maxResolution;
|
||||||
expect(maxResolution).to.eql(81);
|
expect(maxResolution).to.eql(81);
|
||||||
var minResolution = parts[2];
|
var minResolution = info.minResolution;
|
||||||
expect(minResolution).to.eql(3);
|
expect(minResolution).to.eql(3);
|
||||||
var fn = parts[0];
|
var fn = info.constraint;
|
||||||
expect(fn(82, 0, 0)).to.eql(81);
|
expect(fn(82, 0, 0)).to.eql(81);
|
||||||
expect(fn(81, 0, 0)).to.eql(81);
|
expect(fn(81, 0, 0)).to.eql(81);
|
||||||
expect(fn(27, 0, 0)).to.eql(27);
|
expect(fn(27, 0, 0)).to.eql(27);
|
||||||
@@ -44,12 +44,12 @@ describe('ol.View2D', function() {
|
|||||||
var options = {
|
var options = {
|
||||||
resolutions: [97, 76, 65, 54, 0.45]
|
resolutions: [97, 76, 65, 54, 0.45]
|
||||||
};
|
};
|
||||||
var parts = ol.View2D.createResolutionConstraint_(options);
|
var info = ol.View2D.createResolutionConstraint_(options);
|
||||||
var maxResolution = parts[1];
|
var maxResolution = info.maxResolution;
|
||||||
expect(maxResolution).to.eql(97);
|
expect(maxResolution).to.eql(97);
|
||||||
var minResolution = parts[2];
|
var minResolution = info.minResolution;
|
||||||
expect(minResolution).to.eql(0.45);
|
expect(minResolution).to.eql(0.45);
|
||||||
var fn = parts[0];
|
var fn = info.constraint;
|
||||||
expect(fn(97, 0, 0)).to.eql(97);
|
expect(fn(97, 0, 0)).to.eql(97);
|
||||||
expect(fn(76, 0, 0)).to.eql(76);
|
expect(fn(76, 0, 0)).to.eql(76);
|
||||||
expect(fn(65, 0, 0)).to.eql(65);
|
expect(fn(65, 0, 0)).to.eql(65);
|
||||||
|
|||||||
Reference in New Issue
Block a user