简体   繁体   中英

Capturing mousewheel zoom in OpenLayers

I have a map in OpenLayers that has a number of layers with Markers. 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. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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