diff --git a/src/ol/control/zoomcontrol.js b/src/ol/control/zoomcontrol.js index b3549a4f37..c4a7422f01 100644 --- a/src/ol/control/zoomcontrol.js +++ b/src/ol/control/zoomcontrol.js @@ -6,8 +6,10 @@ goog.require('goog.dom'); goog.require('goog.dom.TagName'); goog.require('goog.events'); goog.require('goog.events.EventType'); +goog.require('ol.animation'); goog.require('ol.control.Control'); goog.require('ol.css'); +goog.require('ol.easing'); /** @@ -74,8 +76,17 @@ ol.control.Zoom.prototype.handleIn_ = function(browserEvent) { var map = this.getMap(); map.requestRenderFrame(); // FIXME works for View2D only - map.getView().zoomByDelta(map, this.delta_, undefined, - ol.control.ZOOM_DURATION); + var view = map.getView().getView2D(); + var currentResolution = view.getResolution(); + if (goog.isDef(currentResolution)) { + map.addPreRenderFunction(ol.animation.zoom({ + resolution: currentResolution, + duration: ol.control.ZOOM_DURATION, + easing: ol.easing.easeOut + })); + } + var resolution = view.constrainResolution(currentResolution, this.delta_); + view.setResolution(resolution); }; @@ -87,8 +98,16 @@ ol.control.Zoom.prototype.handleOut_ = function(browserEvent) { // prevent #zoomOut anchor from getting appended to the url browserEvent.preventDefault(); var map = this.getMap(); - map.requestRenderFrame(); // FIXME works for View2D only - map.getView().zoomByDelta(map, -this.delta_, undefined, - ol.control.ZOOM_DURATION); + var view = map.getView().getView2D(); + var currentResolution = view.getResolution(); + if (goog.isDef(currentResolution)) { + map.addPreRenderFunction(ol.animation.zoom({ + resolution: currentResolution, + duration: ol.control.ZOOM_DURATION, + easing: ol.easing.easeOut + })); + } + var resolution = view.constrainResolution(currentResolution, -this.delta_); + view.setResolution(resolution); }; diff --git a/src/ol/interaction/dblclickzoominteraction.js b/src/ol/interaction/dblclickzoominteraction.js index 0b08c0ef58..24d28b84da 100644 --- a/src/ol/interaction/dblclickzoominteraction.js +++ b/src/ol/interaction/dblclickzoominteraction.js @@ -5,7 +5,6 @@ goog.provide('ol.interaction.DblClickZoom'); goog.require('goog.asserts'); goog.require('ol.MapBrowserEvent'); goog.require('ol.MapBrowserEvent.EventType'); -goog.require('ol.View2D'); goog.require('ol.interaction.Interaction'); @@ -46,9 +45,8 @@ ol.interaction.DblClickZoom.prototype.handleMapBrowserEvent = var delta = mapBrowserEvent.browserEvent.shiftKey ? -this.delta_ : this.delta_; // FIXME works for View2D only - var view = map.getView(); - goog.asserts.assertInstanceof(view, ol.View2D); - view.zoomByDelta(map, delta, anchor, + var view = map.getView().getView2D(); + ol.interaction.Interaction.zoomByDelta(map, view, delta, anchor, ol.interaction.DBLCLICKZOOM_ANIMATION_DURATION); mapBrowserEvent.preventDefault(); browserEvent.preventDefault(); diff --git a/src/ol/interaction/dragrotateandzoominteraction.js b/src/ol/interaction/dragrotateandzoominteraction.js index be44ef90a9..f7f64c0cc4 100644 --- a/src/ol/interaction/dragrotateandzoominteraction.js +++ b/src/ol/interaction/dragrotateandzoominteraction.js @@ -4,7 +4,6 @@ goog.provide('ol.interaction.DragRotateAndZoom'); goog.require('goog.asserts'); goog.require('goog.math.Vec2'); -goog.require('ol.View2D'); goog.require('ol.interaction.ConditionType'); goog.require('ol.interaction.Drag'); goog.require('ol.interaction.Interaction'); @@ -56,8 +55,7 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag = var theta = Math.atan2(delta.y, delta.x); var magnitude = delta.magnitude(); // FIXME works for View2D only - var view = map.getView(); - goog.asserts.assertInstanceof(view, ol.View2D); + var view = map.getView().getView2D(); map.requestRenderFrame(); // FIXME the calls to map.rotate and map.zoomToResolution should use // map.withFrozenRendering but an assertion fails :-( @@ -69,7 +67,7 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag = this.lastAngle_ = theta; if (goog.isDef(this.lastMagnitude_)) { var resolution = this.lastMagnitude_ * (view.getResolution() / magnitude); - view.zoom(map, resolution); + ol.interaction.Interaction.zoom(map, view, resolution); } this.lastMagnitude_ = magnitude; }; diff --git a/src/ol/interaction/interaction.js b/src/ol/interaction/interaction.js index af3ab89131..22d307ad99 100644 --- a/src/ol/interaction/interaction.js +++ b/src/ol/interaction/interaction.js @@ -99,3 +99,83 @@ ol.interaction.Interaction.rotateWithoutConstraints = } } }; + + +/** + * @param {ol.Map} map Map. + * @param {ol.View2D} view View. + * @param {number|undefined} resolution Resolution to go to. + * @param {ol.Coordinate=} opt_anchor Anchor coordinate. + * @param {number=} opt_duration Duration. + * @param {number=} opt_direction Zooming direction; > 0 indicates + * zooming out, in which case the constraints system will select + * the largest nearest resolution; < 0 indicates zooming in, in + * which case the constraints system will select the smallest + * nearest resolution; == 0 indicates that the zooming direction + * is unknown/not relevant, in which case the constraints system + * will select the nearest resolution. If not defined 0 is + * assumed. + */ +ol.interaction.Interaction.zoom = + function(map, view, resolution, opt_anchor, opt_duration, opt_direction) { + resolution = view.constrainResolution(resolution, 0, opt_direction); + ol.interaction.Interaction.zoomWithoutConstraints( + map, view, resolution, opt_anchor, opt_duration); +}; + + +/** + * @param {ol.Map} map Map. + * @param {ol.View2D} view View. + * @param {number} delta Delta from previous zoom level. + * @param {ol.Coordinate=} opt_anchor Anchor coordinate. + * @param {number=} opt_duration Duration. + */ +ol.interaction.Interaction.zoomByDelta = + function(map, view, delta, opt_anchor, opt_duration) { + var currentResolution = view.getResolution(); + var resolution = view.constrainResolution(currentResolution, delta, 0); + ol.interaction.Interaction.zoomWithoutConstraints( + map, view, resolution, opt_anchor, opt_duration); +}; + + +/** + * @param {ol.Map} map Map. + * @param {ol.View2D} view View. + * @param {number|undefined} resolution Resolution to go to. + * @param {ol.Coordinate=} opt_anchor Anchor coordinate. + * @param {number=} opt_duration Duration. + */ +ol.interaction.Interaction.zoomWithoutConstraints = + function(map, view, resolution, opt_anchor, opt_duration) { + if (goog.isDefAndNotNull(resolution)) { + var currentResolution = view.getResolution(); + var currentCenter = view.getCenter(); + if (goog.isDef(currentResolution) && goog.isDef(currentCenter) && + goog.isDef(opt_duration)) { + map.requestRenderFrame(); + map.addPreRenderFunction(ol.animation.zoom({ + resolution: currentResolution, + duration: opt_duration, + easing: ol.easing.easeOut + })); + if (goog.isDef(opt_anchor)) { + map.addPreRenderFunction(ol.animation.pan({ + source: currentCenter, + duration: opt_duration, + easing: ol.easing.easeOut + })); + } + } + if (goog.isDefAndNotNull(opt_anchor)) { + var center = view.calculateCenterZoom(resolution, opt_anchor); + map.withFrozenRendering(function() { + view.setCenter(center); + view.setResolution(resolution); + }); + } else { + view.setResolution(resolution); + } + } +}; diff --git a/src/ol/interaction/keyboardzoominteraction.js b/src/ol/interaction/keyboardzoominteraction.js index dc4c6d3578..bc3c41ca18 100644 --- a/src/ol/interaction/keyboardzoominteraction.js +++ b/src/ol/interaction/keyboardzoominteraction.js @@ -4,7 +4,6 @@ goog.provide('ol.interaction.KeyboardZoom'); goog.require('goog.asserts'); goog.require('goog.events.KeyHandler.EventType'); -goog.require('ol.View2D'); goog.require('ol.interaction.Interaction'); @@ -50,9 +49,8 @@ ol.interaction.KeyboardZoom.prototype.handleMapBrowserEvent = var delta = (charCode == '+'.charCodeAt(0)) ? this.delta_ : -this.delta_; map.requestRenderFrame(); // FIXME works for View2D only - var view = map.getView(); - goog.asserts.assertInstanceof(view, ol.View2D); - view.zoomByDelta(map, delta, undefined, + var view = map.getView().getView2D(); + ol.interaction.Interaction.zoomByDelta(map, view, delta, undefined, ol.interaction.KEYBOARD_ZOOM_DURATION); keyEvent.preventDefault(); mapBrowserEvent.preventDefault(); diff --git a/src/ol/interaction/mousewheelzoominteraction.js b/src/ol/interaction/mousewheelzoominteraction.js index c403944eb1..25d5bcbe42 100644 --- a/src/ol/interaction/mousewheelzoominteraction.js +++ b/src/ol/interaction/mousewheelzoominteraction.js @@ -7,7 +7,6 @@ goog.require('goog.events.MouseWheelEvent'); goog.require('goog.events.MouseWheelHandler.EventType'); goog.require('goog.math'); goog.require('ol.Coordinate'); -goog.require('ol.View2D'); goog.require('ol.interaction.Interaction'); @@ -108,11 +107,10 @@ ol.interaction.MouseWheelZoom.prototype.doZoom_ = function(map) { var delta = goog.math.clamp(this.delta_, -maxDelta, maxDelta); // FIXME works for View2D only - var view = map.getView(); - goog.asserts.assertInstanceof(view, ol.View2D); + var view = map.getView().getView2D(); map.requestRenderFrame(); - view.zoomByDelta(map, -delta, this.lastAnchor_, + ol.interaction.Interaction.zoomByDelta(map, view, -delta, this.lastAnchor_, ol.interaction.MOUSEWHEELZOOM_ANIMATION_DURATION); this.delta_ = 0; diff --git a/src/ol/interaction/touchzoominteraction.js b/src/ol/interaction/touchzoominteraction.js index 17409fe8ec..63558750a1 100644 --- a/src/ol/interaction/touchzoominteraction.js +++ b/src/ol/interaction/touchzoominteraction.js @@ -6,6 +6,7 @@ goog.require('goog.asserts'); goog.require('goog.style'); goog.require('ol.View'); goog.require('ol.ViewHint'); +goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Touch'); @@ -65,7 +66,7 @@ ol.interaction.TouchZoom.prototype.handleTouchMove = } var map = mapBrowserEvent.map; - var view = map.getView(); + var view = map.getView().getView2D(); // scale anchor point. var viewportPosition = goog.style.getClientPosition(map.getViewport()); @@ -75,7 +76,8 @@ ol.interaction.TouchZoom.prototype.handleTouchMove = var anchor = map.getCoordinateFromPixel(centroid); // scale, bypass the resolution constraint - view.zoomWithoutConstraints(map, view.getResolution() * scaleDelta, anchor); + ol.interaction.Interaction.zoomWithoutConstraints( + map, view, view.getResolution() * scaleDelta, anchor); }; @@ -87,12 +89,12 @@ ol.interaction.TouchZoom.prototype.handleTouchEnd = function(mapBrowserEvent) { if (this.targetTouches.length < 2) { var map = mapBrowserEvent.map; - var view = map.getView(); + var view = map.getView().getView2D(); // Zoom to final resolution, with an animation, and provide a // direction not to zoom out/in if user was pinching in/out. // Direction is > 0 if pinching out, and < 0 if pinching in. var direction = this.lastScaleDelta_ - 1; - view.zoom(map, view.getResolution(), undefined, + ol.interaction.Interaction.zoom(map, view, view.getResolution(), undefined, ol.interaction.TOUCHZOOM_ANIMATION_DURATION, direction); view.setHint(ol.ViewHint.INTERACTING, -1); return false; diff --git a/src/ol/view2d.js b/src/ol/view2d.js index 1990114237..e6a118a83f 100644 --- a/src/ol/view2d.js +++ b/src/ol/view2d.js @@ -15,9 +15,7 @@ goog.require('ol.RotationConstraint'); goog.require('ol.RotationConstraintType'); goog.require('ol.Size'); goog.require('ol.View'); -goog.require('ol.animation'); goog.require('ol.coordinate'); -goog.require('ol.easing'); goog.require('ol.projection'); @@ -376,81 +374,6 @@ goog.exportProperty( ol.View2D.prototype.setRotation); -/** - * @param {ol.Map} map Map. - * @param {number|undefined} resolution Resolution to go to. - * @param {ol.Coordinate=} opt_anchor Anchor coordinate. - * @param {number=} opt_duration Duration. - * @param {number=} opt_direction Zooming direction; > 0 indicates - * zooming out, in which case the constraints system will select - * the largest nearest resolution; < 0 indicates zooming in, in - * which case the constraints system will select the smallest - * nearest resolution; == 0 indicates that the zooming direction - * is unknown/not relevant, in which case the constraints system - * will select the nearest resolution. If not defined 0 is - * assumed. - */ -ol.View2D.prototype.zoom = - function(map, resolution, opt_anchor, opt_duration, opt_direction) { - resolution = this.constrainResolution(resolution, 0, opt_direction); - this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration); -}; - - -/** - * @param {ol.Map} map Map. - * @param {number} delta Delta from previous zoom level. - * @param {ol.Coordinate=} opt_anchor Anchor coordinate. - * @param {number=} opt_duration Duration. - */ -ol.View2D.prototype.zoomByDelta = - function(map, delta, opt_anchor, opt_duration) { - var currentResolution = this.getResolution(); - var resolution = this.constrainResolution(currentResolution, delta, 0); - this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration); -}; - - -/** - * @param {ol.Map} map Map. - * @param {number|undefined} resolution Resolution to go to. - * @param {ol.Coordinate=} opt_anchor Anchor coordinate. - * @param {number=} opt_duration Duration. - */ -ol.View2D.prototype.zoomWithoutConstraints = - function(map, resolution, opt_anchor, opt_duration) { - if (goog.isDefAndNotNull(resolution)) { - var currentResolution = this.getResolution(); - var currentCenter = this.getCenter(); - if (goog.isDef(currentResolution) && goog.isDef(currentCenter) && - goog.isDef(opt_duration)) { - map.requestRenderFrame(); - map.addPreRenderFunction(ol.animation.zoom({ - resolution: currentResolution, - duration: opt_duration, - easing: ol.easing.easeOut - })); - if (goog.isDef(opt_anchor)) { - map.addPreRenderFunction(ol.animation.pan({ - source: currentCenter, - duration: opt_duration, - easing: ol.easing.easeOut - })); - } - } - if (goog.isDefAndNotNull(opt_anchor)) { - var center = this.calculateCenterZoom(resolution, opt_anchor); - map.withFrozenRendering(function() { - this.setCenter(center); - this.setResolution(resolution); - }, this); - } else { - this.setResolution(resolution); - } - } -}; - - /** * @private * @param {ol.View2DOptions} options View2D options.