Refactoring to set the constraints to the map

This commit is contained in:
Éric Lemoine
2012-09-26 23:28:01 +02:00
parent 2991fa1178
commit b6bb05bf76
15 changed files with 164 additions and 178 deletions

View File

@@ -34,6 +34,7 @@ goog.require('ol.Pixel');
goog.require('ol.Projection');
goog.require('ol.Size');
goog.require('ol.TransformFunction');
goog.require('ol.interaction.Constraints');
goog.require('ol.interaction.Interaction');
goog.require('ol.renderer.Layer');
@@ -136,6 +137,13 @@ ol.Map = function(container, mapOptionsLiteral, opt_viewportSizeMonitor) {
*/
this.container_ = container;
/**
* @private
* FIXME change ol.interaction.Constraints -> ol.Constraints
* @type {ol.interaction.Constraints}
*/
this.constraints_ = mapOptions.constraints;
/**
* @private
* @type {Element}
@@ -218,7 +226,9 @@ ol.Map.prototype.canRotate = function() {
ol.Map.prototype.fitExtent = function(extent) {
this.withFrozenRendering(function() {
this.setCenter(extent.getCenter());
this.setResolution(this.getResolutionForExtent(extent));
var resolution = this.getResolutionForExtent(extent);
resolution = this.constraints_.resolution(resolution, 0);
this.setResolution(resolution);
if (this.canRotate()) {
this.setRotation(0);
}
@@ -614,6 +624,16 @@ ol.Map.prototype.renderFrame_ = function() {
};
/**
* @param {number|undefined} rotation Rotation.
* @param {number} delta Delta.
*/
ol.Map.prototype.rotate = function(rotation, delta) {
rotation = this.constraints_.rotation(rotation, delta);
this.setRotation(rotation);
};
/**
* @param {ol.Color} backgroundColor Background color.
*/
@@ -790,3 +810,62 @@ ol.Map.prototype.withFrozenRendering = function(f, opt_obj) {
this.unfreezeRendering();
}
};
/**
* @private
* @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
*/
ol.Map.prototype.zoom_ = function(resolution, opt_anchor) {
if (goog.isDefAndNotNull(resolution) && goog.isDefAndNotNull(opt_anchor)) {
var anchor = opt_anchor;
var oldCenter = /** @type {!ol.Coordinate} */ this.getCenter();
var oldResolution = this.getResolution();
var x = anchor.x - resolution * (anchor.x - oldCenter.x) / oldResolution;
var y = anchor.y - resolution * (anchor.y - oldCenter.y) / oldResolution;
var center = new ol.Coordinate(x, y);
this.withFrozenRendering(function() {
this.setCenter(center);
this.setResolution(resolution);
}, this);
} else {
this.setResolution(resolution);
}
};
/**
* @param {number} delta Delta from previous zoom level.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
*/
ol.Map.prototype.zoomByDelta = function(delta, opt_anchor) {
var resolution = this.constraints_.resolution(this.getResolution(), delta);
this.zoom_(resolution, opt_anchor);
};
/**
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
*/
ol.Map.prototype.zoomIn = function(opt_anchor) {
this.zoomByDelta(4, opt_anchor);
};
/**
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
*/
ol.Map.prototype.zoomOut = function(opt_anchor) {
this.zoomByDelta(-4, opt_anchor);
};
/**
* @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
*/
ol.Map.prototype.zoomToResolution = function(resolution, opt_anchor) {
resolution = this.constraints_.resolution(resolution, 0);
this.zoom_(resolution, opt_anchor);
};