简体   繁体   English

在 OpenLayers 中捕获鼠标滚轮缩放

[英]Capturing mousewheel zoom in OpenLayers

I have a map in OpenLayers that has a number of layers with Markers.我在 OpenLayers 中有一张地图,其中有许多带有标记的图层。 Each time the user zooms the map I call a function that groups overlapping markers.每次用户缩放地图时,我都会调用一个对重叠标记进行分组的函数。 This works just fine when zooming using the normal zoom buttons, but I also want to call this function when a user zooms using the mouse wheel.这在使用普通缩放按钮缩放时效果很好,但我也想在用户使用鼠标滚轮缩放时调用此函数。

I guess I have to use the OpenLayers.Handler.MouseWheel to capture this event, but I don't know how.我想我必须使用OpenLayers.Handler.MouseWheel来捕获这个事件,但我不知道如何。 Does anyone have an example for this?有没有人有这方面的例子?

You should use map's zoomend event that fires every time user zooms in or out no matter how user did it (buttons, double click or mouse scroll).您应该使用地图的zoomend事件,无论用户如何操作(按钮、双击或鼠标滚动),每次用户放大或缩小时都会触发zoomend事件。

The code should look like this:代码应如下所示:

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

Using the latest version of Openlayers v6.5.0.使用最新版本的 Openlayers v6.5.0。 I got it worked我成功了

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,
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM