View / add a method to compute a valid zoom level

The `getValidZoomLevel` apply the current resolution constraint to return
a value that is guaranteed valid.

This is used for interactions & controls which need a target value to work:
the +/- buttons, the zoom clider, the dragbox zoom and the mouse wheel zoom.
This commit is contained in:
Olivier Guyot
2019-01-06 14:25:00 +01:00
parent 4e1ece16ed
commit 3c1e3779e2
7 changed files with 92 additions and 25 deletions

View File

@@ -1217,6 +1217,23 @@ class View extends BaseObject {
endInteraction() {
this.setHint(ViewHint.INTERACTING, -1);
}
/**
* Get a valid zoom level according to the current view constraints.
* @param {number|undefined} targetZoom Target resolution.
* @param {number=} opt_direction Direction. Default is `0`. Specify `-1` or `1` to return
* the available value respectively lower or greater than the target one. Leaving `0` will simply choose
* the nearest available value.
* @return {number|undefined} Valid zoom level.
* @api
*/
getValidZoomLevel(targetZoom, opt_direction) {
const direction = opt_direction || 0;
const currentRes = this.getResolution();
const currentZoom = this.getZoom();
return this.getZoomForResolution(
this.constraints_.resolution(currentRes, targetZoom - currentZoom, direction));
}
}