Add zoom to/from anchor

This commit is contained in:
Tom Payne
2012-08-04 16:45:11 +02:00
parent 97eabc8693
commit b7e7a9b3ba
4 changed files with 33 additions and 9 deletions

View File

@@ -80,15 +80,36 @@ ol.Control.prototype.setResolution = function(map, resolution) {
/**
* @param {ol.Map} map Map.
* @param {number|undefined} resolution Resolution.
* @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();
ol.Control.prototype.zoom = function(map, resolution, delta, opt_anchor) {
if (goog.isDefAndNotNull(opt_anchor)) {
var mapCenter = /** @type {!ol.Coordinate} */ map.getCenter();
var mapResolution = map.getResolution();
resolution = this.constraints.resolution(resolution, delta);
var center;
if (resolution < mapResolution) {
center = opt_anchor.clone();
center.subtract(mapCenter);
center.scale(resolution / mapResolution);
center.add(mapCenter);
} else if (resolution == mapResolution) {
center = mapCenter;
} else {
center = mapCenter.clone();
center.subtract(opt_anchor);
center.scale(resolution / mapResolution);
center.add(opt_anchor);
}
center = this.constraints.center(center, resolution, ol.Coordinate.ZERO);
map.withFrozenRendering(function() {
map.setCenter(center);
map.setResolution(resolution);
});
} else {
resolution = this.constraints.resolution(resolution, delta);
map.setResolution(resolution);
//}
}
};

View File

@@ -25,9 +25,10 @@ ol.control.DblClickZoom.prototype.handleMapBrowserEvent =
function(mapBrowserEvent) {
if (mapBrowserEvent.type == goog.events.EventType.DBLCLICK) {
var map = mapBrowserEvent.map;
var resolution = map.getResolution();
var delta = mapBrowserEvent.browserEvent.shiftKey ? -1 : 1;
var anchor = mapBrowserEvent.getCoordinate();
this.zoom(map, delta, anchor);
this.zoom(map, resolution, delta, anchor);
mapBrowserEvent.preventDefault();
}
};

View File

@@ -29,8 +29,9 @@ ol.control.KeyboardZoom.prototype.handleMapBrowserEvent =
var charCode = keyEvent.charCode;
if (charCode == '+'.charCodeAt(0) || charCode == '-'.charCodeAt(0)) {
var map = mapBrowserEvent.map;
var resolution = map.getResolution();
var delta = charCode == '+'.charCodeAt(0) ? 1 : -1;
this.zoom(map, delta);
this.zoom(map, resolution, delta);
keyEvent.preventDefault();
mapBrowserEvent.preventDefault();
}

View File

@@ -31,8 +31,9 @@ ol.control.MouseWheelZoom.prototype.handleMapBrowserEvent =
goog.asserts.assert(mouseWheelEvent instanceof goog.events.MouseWheelEvent);
if (mouseWheelEvent.deltaY !== 0) {
var delta = mouseWheelEvent.deltaY < 0 ? 1 : -1;
var resolution = map.getResolution();
var anchor = mapBrowserEvent.getCoordinate();
this.zoom(map, delta, anchor);
this.zoom(map, resolution, delta, anchor);
mapBrowserEvent.preventDefault();
mouseWheelEvent.preventDefault();
}