在OpenLayers中捕获鼠标滚轮缩放

时间:2012-08-15 10:25:39

标签: javascript javascript-events openlayers openstreetmap

我在OpenLayers中有一个地图,其中有许多带有标记的图层。每次用户缩放地图时,我都会调用一个对重叠标记进行分组的功能。使用普通缩放按钮进行缩放时,此功能很好,但我还想在用户使用鼠标滚轮进行缩放时调用此功能。

我想我必须使用OpenLayers.Handler.MouseWheel来捕获此事件,但我不知道如何。有没有人有这方面的例子?

2 个答案:

答案 0 :(得分:0)

你应该使用地图的zoomend事件,每当用户放大或缩小时都会触发,无论用户如何操作(按钮,双击或鼠标滚动)。

代码应如下所示:

map.events.on({ "zoomend": function(){
    //Do whatever you need to do here
}});

答案 1 :(得分:0)

使用最新版本的 Openlayers v6.5.0。 我成功了

this.map = new Map({
            controls: [],
            interactions: defaultInteractions({
                shiftDragZoom: false,
                doubleClickZoom: false,
                pinchRotate: false,
                mouseWheelZoom: false,
            }).extend([
                new MouseWheelZoom({
                    condition: platformModifierKeyOnly,
                    handleEvent: (event) => {
                        if (event.type !== "wheel") return;

                        if (event.originalEvent.deltaY <= -3) {
                            //mouse wheel up
                        } else {
                            //mouse wheel down
                        }
                    },
                }),
            ]),
            target: "map",
            layers,
        });