我想将鼠标滚轮缩放级别限制为 View.resolutions 中指定的级别。
我能做些什么来实现它?
在视图选项中使用 constrainResolution: true
(c) 迈克
编辑:如果您只想将constrainResolution用于鼠标滚轮缩放,那么您应该在创建地图之前覆盖原型MouseWheelZoom中的函数handleWheelZoom_ :
import { clamp } from 'ol/math.js';
import { zoomByDelta } from 'ol/interaction/Interaction';
import { MouseWheelZoom } from "ol/interaction";
const mouseWheelZoomContrainResolution = true;
MouseWheelZoom.prototype.handleWheelZoom_ = function (map) {
var view = map.getView();
if (view.getAnimating()) {
view.cancelAnimations();
}
var delta = -clamp(this.totalDelta_, -this.maxDelta_ * this.deltaPerZoom_, this.maxDelta_ * this.deltaPerZoom_) / this.deltaPerZoom_;
if (view.getConstrainResolution() || mouseWheelZoomContrainResolution) {
// view has a zoom constraint, zoom by 1
delta = delta ? delta > 0 ? 1 : -1 : 0;
}
zoomByDelta(view, delta, this.lastAnchor_, this.duration_);
this.mode_ = undefined;
this.totalDelta_ = 0;
this.lastAnchor_ = null;
this.startTime_ = undefined;
this.timeoutId_ = undefined;
};
编辑2:
该视图有一个 setConstrainResolution 方法,因此您可以打开和关闭约束。您可以在手动设置缩放之前将其关闭,然后在对分辨率进行任何进一步更改时将其重新打开
(c) 迈克
请参阅下面的评论