Add and use ol.Constraints
This commit is contained in:
28
src/ol/control/constraints.js
Normal file
28
src/ol/control/constraints.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// FIXME add rotation constraint
|
||||
|
||||
goog.provide('ol.control.Constraints');
|
||||
|
||||
goog.require('ol.control.CenterConstraintType');
|
||||
goog.require('ol.control.ResolutionConstraintType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {ol.control.CenterConstraintType} centerConstraint Center constraint.
|
||||
* @param {ol.control.ResolutionConstraintType} resolutionConstraint
|
||||
* Resolution constraint.
|
||||
*/
|
||||
ol.control.Constraints = function(centerConstraint, resolutionConstraint) {
|
||||
|
||||
/**
|
||||
* @type {ol.control.CenterConstraintType}
|
||||
*/
|
||||
this.center = centerConstraint;
|
||||
|
||||
/**
|
||||
* @type {ol.control.ResolutionConstraintType}
|
||||
*/
|
||||
this.resolution = resolutionConstraint;
|
||||
|
||||
};
|
||||
@@ -3,13 +3,22 @@
|
||||
goog.provide('ol.Control');
|
||||
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.Control = function() {
|
||||
ol.Control = function(constraints) {
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @type {ol.control.Constraints}
|
||||
*/
|
||||
this.constraints = constraints;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -17,3 +26,68 @@ ol.Control = function() {
|
||||
* @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
|
||||
*/
|
||||
ol.Control.prototype.handleMapBrowserEvent = goog.abstractMethod;
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
*/
|
||||
ol.Control.prototype.fitExtent = function(map, extent) {
|
||||
var resolution = map.getResolutionForExtent(extent);
|
||||
resolution = this.constraints.resolution(resolution, 0);
|
||||
var center = extent.getCenter();
|
||||
center = this.constraints.center(center, resolution, ol.Coordinate.ZERO);
|
||||
map.withFrozenRendering(function() {
|
||||
map.setCenter(center);
|
||||
map.setResolution(resolution);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {ol.Coordinate} delta Delta.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor.
|
||||
*/
|
||||
ol.Control.prototype.pan = function(map, delta, opt_anchor) {
|
||||
var center = opt_anchor ? opt_anchor : map.getCenter();
|
||||
var resolution = map.getResolution();
|
||||
center = this.constraints.center(center, resolution, delta);
|
||||
map.setCenter(center);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {number|undefined} resolution Resolution.
|
||||
*/
|
||||
ol.Control.prototype.setResolution = function(map, resolution) {
|
||||
resolution = this.constraints.resolution(resolution, 0);
|
||||
map.setResolution(resolution);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {number} rotation Rotation.
|
||||
*/
|
||||
ol.Control.prototype.setRotation = function(map, rotation) {
|
||||
// FIXME use a constraint
|
||||
map.setRotation(rotation);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {number} delta Delta.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor.
|
||||
*/
|
||||
ol.Control.prototype.zoom = function(map, delta, opt_anchor) {
|
||||
//if (false && goog.isDef(opt_anchor)) {
|
||||
// FIXME
|
||||
//} else {
|
||||
var resolution = map.getResolution();
|
||||
resolution = this.constraints.resolution(resolution, delta);
|
||||
map.setResolution(resolution);
|
||||
//}
|
||||
};
|
||||
|
||||
@@ -3,26 +3,17 @@ goog.provide('ol.control.DblClickZoom');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.Control');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.ResolutionConstraintType');
|
||||
goog.require('ol.control.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Control}
|
||||
* @param {ol.control.ResolutionConstraintType} resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.DblClickZoom = function(resolutionConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.ResolutionConstraintType}
|
||||
*/
|
||||
this.resolutionConstraint_ = resolutionConstraint;
|
||||
|
||||
ol.control.DblClickZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.DblClickZoom, ol.Control);
|
||||
|
||||
@@ -34,14 +25,9 @@ ol.control.DblClickZoom.prototype.handleMapBrowserEvent =
|
||||
function(mapBrowserEvent) {
|
||||
if (mapBrowserEvent.type == goog.events.EventType.DBLCLICK) {
|
||||
var map = mapBrowserEvent.map;
|
||||
map.withFrozenRendering(function() {
|
||||
// FIXME compute correct center for zoom
|
||||
map.setCenter(mapBrowserEvent.getCoordinate());
|
||||
var browserEvent = mapBrowserEvent.browserEvent;
|
||||
var delta = browserEvent.shiftKey ? -1 : 1;
|
||||
var resolution = this.resolutionConstraint_(map.getResolution(), delta);
|
||||
map.setResolution(resolution);
|
||||
}, this);
|
||||
var delta = mapBrowserEvent.browserEvent.shiftKey ? -1 : 1;
|
||||
var anchor = mapBrowserEvent.getCoordinate();
|
||||
this.zoom(map, delta, anchor);
|
||||
mapBrowserEvent.preventDefault();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,16 +7,18 @@ goog.require('goog.functions');
|
||||
goog.require('ol.Control');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Control}
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.Drag = function() {
|
||||
ol.control.Drag = function(constraints) {
|
||||
|
||||
goog.base(this);
|
||||
goog.base(this, constraints);
|
||||
|
||||
/**
|
||||
* @private
|
||||
|
||||
@@ -4,8 +4,7 @@ goog.provide('ol.control.DragPan');
|
||||
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.CenterConstraint');
|
||||
goog.require('ol.control.CenterConstraintType');
|
||||
goog.require('ol.control.Constraints');
|
||||
goog.require('ol.control.Drag');
|
||||
|
||||
|
||||
@@ -13,20 +12,10 @@ goog.require('ol.control.Drag');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.control.Drag}
|
||||
* @param {ol.control.CenterConstraintType=} opt_centerConstraint
|
||||
* Center constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.DragPan = function(opt_centerConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.CenterConstraintType|undefined}
|
||||
*/
|
||||
this.centerConstraint_ =
|
||||
opt_centerConstraint || ol.control.CenterConstraint.none;
|
||||
|
||||
ol.control.DragPan = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.DragPan, ol.control.Drag);
|
||||
|
||||
@@ -39,8 +28,7 @@ ol.control.DragPan.prototype.handleDrag = function(mapBrowserEvent) {
|
||||
var resolution = map.getResolution();
|
||||
var delta =
|
||||
new ol.Coordinate(-resolution * this.deltaX, resolution * this.deltaY);
|
||||
var center = this.centerConstraint_(this.startCenter, resolution, delta);
|
||||
map.setCenter(center);
|
||||
this.pan(map, delta, this.startCenter);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -3,28 +3,17 @@ goog.provide('ol.control.KeyboardPan');
|
||||
goog.require('goog.events.KeyCodes');
|
||||
goog.require('goog.events.KeyHandler.EventType');
|
||||
goog.require('ol.Control');
|
||||
goog.require('ol.control.CenterConstraint');
|
||||
goog.require('ol.control.CenterConstraintType');
|
||||
goog.require('ol.control.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Control}
|
||||
* @param {ol.control.CenterConstraintType=} opt_centerConstraint
|
||||
* Center constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.KeyboardPan = function(opt_centerConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.CenterConstraintType}
|
||||
*/
|
||||
this.centerConstraint_ = opt_centerConstraint ||
|
||||
ol.control.CenterConstraint.none;
|
||||
|
||||
ol.control.KeyboardPan = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.KeyboardPan, ol.Control);
|
||||
|
||||
@@ -43,7 +32,6 @@ ol.control.KeyboardPan.prototype.handleMapBrowserEvent =
|
||||
keyCode == goog.events.KeyCodes.RIGHT ||
|
||||
keyCode == goog.events.KeyCodes.UP) {
|
||||
var map = mapBrowserEvent.map;
|
||||
var center = map.getCenter().clone();
|
||||
var resolution = map.getResolution();
|
||||
var delta;
|
||||
if (keyCode == goog.events.KeyCodes.DOWN) {
|
||||
@@ -56,8 +44,7 @@ ol.control.KeyboardPan.prototype.handleMapBrowserEvent =
|
||||
goog.asserts.assert(keyCode == goog.events.KeyCodes.UP);
|
||||
delta = new ol.Coordinate(0, 16 * resolution);
|
||||
}
|
||||
center = this.centerConstraint_(center, resolution, delta);
|
||||
map.setCenter(center);
|
||||
this.pan(map, delta);
|
||||
keyEvent.preventDefault();
|
||||
mapBrowserEvent.preventDefault();
|
||||
}
|
||||
|
||||
@@ -10,19 +10,10 @@ goog.require('ol.control.ResolutionConstraintType');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Control}
|
||||
* @param {ol.control.ResolutionConstraintType} resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.KeyboardZoom = function(resolutionConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.ResolutionConstraintType}
|
||||
*/
|
||||
this.resolutionConstraint_ = resolutionConstraint;
|
||||
|
||||
ol.control.KeyboardZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.KeyboardZoom, ol.Control);
|
||||
|
||||
@@ -39,8 +30,7 @@ ol.control.KeyboardZoom.prototype.handleMapBrowserEvent =
|
||||
if (charCode == '+'.charCodeAt(0) || charCode == '-'.charCodeAt(0)) {
|
||||
var map = mapBrowserEvent.map;
|
||||
var delta = charCode == '+'.charCodeAt(0) ? 1 : -1;
|
||||
var resolution = this.resolutionConstraint_(map.getResolution(), delta);
|
||||
map.setResolution(resolution);
|
||||
this.zoom(map, delta);
|
||||
keyEvent.preventDefault();
|
||||
mapBrowserEvent.preventDefault();
|
||||
}
|
||||
|
||||
@@ -3,26 +3,17 @@ goog.provide('ol.control.MouseWheelZoom');
|
||||
goog.require('goog.events.MouseWheelEvent');
|
||||
goog.require('goog.events.MouseWheelHandler.EventType');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.ResolutionConstraintType');
|
||||
goog.require('ol.control.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Control}
|
||||
* @param {ol.control.ResolutionConstraintType} resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.MouseWheelZoom = function(resolutionConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.ResolutionConstraintType}
|
||||
*/
|
||||
this.resolutionConstraint_ = resolutionConstraint;
|
||||
|
||||
ol.control.MouseWheelZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.MouseWheelZoom, ol.Control);
|
||||
|
||||
@@ -39,13 +30,9 @@ ol.control.MouseWheelZoom.prototype.handleMapBrowserEvent =
|
||||
mapBrowserEvent.browserEvent;
|
||||
goog.asserts.assert(mouseWheelEvent instanceof goog.events.MouseWheelEvent);
|
||||
if (mouseWheelEvent.deltaY !== 0) {
|
||||
map.withFrozenRendering(function() {
|
||||
// FIXME compute correct center for zoom
|
||||
map.setCenter(mapBrowserEvent.getCoordinate());
|
||||
var delta = mouseWheelEvent.deltaY < 0 ? 1 : -1;
|
||||
var resolution = this.resolutionConstraint_(map.getResolution(), delta);
|
||||
map.setResolution(resolution);
|
||||
}, this);
|
||||
var delta = mouseWheelEvent.deltaY < 0 ? 1 : -1;
|
||||
var anchor = mapBrowserEvent.getCoordinate();
|
||||
this.zoom(map, delta, anchor);
|
||||
mapBrowserEvent.preventDefault();
|
||||
mouseWheelEvent.preventDefault();
|
||||
}
|
||||
|
||||
@@ -19,10 +19,9 @@ ol.control.ResolutionConstraint.createSnapToResolutions =
|
||||
function(resolutions) {
|
||||
return function(resolution, delta) {
|
||||
if (goog.isDef(resolution)) {
|
||||
var oldLevel = ol.array.linearFindNearest(resolutions, resolution);
|
||||
var newLevel = goog.math.clamp(
|
||||
oldLevel + delta, 0, resolutions.length - 1);
|
||||
return resolutions[newLevel];
|
||||
var z = ol.array.linearFindNearest(resolutions, resolution);
|
||||
z = goog.math.clamp(z + delta, 0, resolutions.length - 1);
|
||||
return resolutions[z];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -8,9 +8,10 @@ goog.require('ol.control.Drag');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.control.Drag}
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.ShiftDragRotate = function() {
|
||||
goog.base(this);
|
||||
ol.control.ShiftDragRotate = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.ShiftDragRotate, ol.control.Drag);
|
||||
|
||||
|
||||
@@ -2,27 +2,18 @@ goog.provide('ol.control.ShiftDragRotateAndZoom');
|
||||
|
||||
goog.require('goog.math.Vec2');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.Constraints');
|
||||
goog.require('ol.control.Drag');
|
||||
goog.require('ol.control.ResolutionConstraintType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.control.Drag}
|
||||
* @param {ol.control.ResolutionConstraintType=} opt_resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.ShiftDragRotateAndZoom = function(opt_resolutionConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.ResolutionConstraintType|undefined}
|
||||
*/
|
||||
this.resolutionConstraint_ = opt_resolutionConstraint;
|
||||
|
||||
ol.control.ShiftDragRotateAndZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.ShiftDragRotateAndZoom, ol.control.Drag);
|
||||
|
||||
@@ -54,12 +45,9 @@ ol.control.ShiftDragRotateAndZoom.prototype.handleDrag =
|
||||
size.height / 2 - browserEvent.offsetY);
|
||||
var theta = Math.atan2(delta.y, delta.x);
|
||||
// FIXME this should use map.withFrozenRendering but an assertion fails :-(
|
||||
map.setRotation(this.startRotation_ - theta);
|
||||
this.setRotation(map, this.startRotation_ - theta);
|
||||
var resolution = this.startRatio_ * delta.magnitude();
|
||||
if (goog.isDef(this.resolutionConstraint_)) {
|
||||
resolution = this.resolutionConstraint_(resolution, 0);
|
||||
}
|
||||
map.setResolution(resolution);
|
||||
this.setResolution(map, resolution);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ goog.provide('ol.control.ShiftDragZoom');
|
||||
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.Constraints');
|
||||
goog.require('ol.control.Drag');
|
||||
goog.require('ol.control.ResolutionConstraintType');
|
||||
|
||||
|
||||
/**
|
||||
@@ -25,19 +25,10 @@ ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS_SQUARED =
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.control.Drag}
|
||||
* @param {ol.control.ResolutionConstraintType=} opt_resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.control.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.control.ShiftDragZoom = function(opt_resolutionConstraint) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.control.ResolutionConstraintType|undefined}
|
||||
*/
|
||||
this.resolutionConstraint_ = opt_resolutionConstraint;
|
||||
|
||||
ol.control.ShiftDragZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
};
|
||||
goog.inherits(ol.control.ShiftDragZoom, ol.control.Drag);
|
||||
|
||||
@@ -52,14 +43,7 @@ ol.control.ShiftDragZoom.prototype.handleDragEnd = function(mapBrowserEvent) {
|
||||
var extent = ol.Extent.boundingExtent(
|
||||
this.startCoordinate,
|
||||
mapBrowserEvent.getCoordinate());
|
||||
var resolution = map.getResolutionForExtent(extent);
|
||||
if (goog.isDef(this.resolutionConstraint_)) {
|
||||
resolution = this.resolutionConstraint_(resolution, 0);
|
||||
}
|
||||
map.withFrozenRendering(function() {
|
||||
map.setCenter(extent.getCenter());
|
||||
map.setResolution(resolution);
|
||||
});
|
||||
this.fitExtent(map, extent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user