diff --git a/src/ol/View.js b/src/ol/View.js index 8c82d91ba7..2bfe5d9592 100644 --- a/src/ol/View.js +++ b/src/ol/View.js @@ -1069,7 +1069,7 @@ class View extends BaseObject { if (options.duration !== undefined) { this.animate({ resolution: resolution, - center: center, + center: this.getValidCenter(center, resolution), duration: options.duration, easing: options.easing }, callback); @@ -1312,6 +1312,19 @@ class View extends BaseObject { this.resolveConstraints_(opt_duration, opt_resolutionDirection); } + /** + * Get a valid position for the view center according to the current constraints. + * @param {import("./coordinate.js").Coordinate|undefined} targetCenter Target center position. + * @param {number=} opt_targetResolution Target resolution. If not supplied, the current one will be used. + * This is useful to guess a valid center position at a different zoom level. + * @return {import("./coordinate.js").Coordinate|undefined} Valid center position. + * @api + */ + getValidCenter(targetCenter, opt_targetResolution) { + const size = this.getSizeFromViewport_(this.getRotation()); + return this.constraints_.center(targetCenter, opt_targetResolution || this.getResolution(), size); + } + /** * Get a valid zoom level according to the current view constraints. * @param {number|undefined} targetZoom Target zoom. diff --git a/src/ol/interaction/DragPan.js b/src/ol/interaction/DragPan.js index 1058867947..ae31b38003 100644 --- a/src/ol/interaction/DragPan.js +++ b/src/ol/interaction/DragPan.js @@ -116,7 +116,7 @@ class DragPan extends PointerInteraction { centerpx[1] - distance * Math.sin(angle) ]); view.animate({ - center: dest, + center: view.getValidCenter(dest), duration: 500, easing: easeOut }); diff --git a/src/ol/interaction/DragZoom.js b/src/ol/interaction/DragZoom.js index 393db36660..2dcd12bdd5 100644 --- a/src/ol/interaction/DragZoom.js +++ b/src/ol/interaction/DragZoom.js @@ -80,13 +80,11 @@ function onBoxEnd() { extent = mapExtent; } - const resolution = view.getResolutionForExtent(extent, size); - const zoom = view.getValidZoomLevel(view.getZoomForResolution(resolution)); - - const center = getCenter(extent); + const resolution = view.getValidResolution(view.getResolutionForExtent(extent, size)); + const center = view.getValidCenter(getCenter(extent), resolution); view.animate({ - zoom: zoom, + resolution: resolution, center: center, duration: this.duration_, easing: easeOut diff --git a/src/ol/interaction/Interaction.js b/src/ol/interaction/Interaction.js index c48921d7ca..5834c5900b 100644 --- a/src/ol/interaction/Interaction.js +++ b/src/ol/interaction/Interaction.js @@ -114,7 +114,7 @@ export function pan(view, delta, opt_duration) { view.animate({ duration: opt_duration !== undefined ? opt_duration : 250, easing: linear, - center: center + center: view.getValidCenter(center) }); } }