简体   繁体   中英

react-leaflet v3 zoom listener

I'm trying to subscribe on zoom change event, but can't get updates on each zoom change.

My goal is to reach markers scaling on zoom levels. Something like in this case React-Leaflet: Scale markers after zooming

But react-leaflet in v3 doesn't have onZoomChange event for MapContainer. So i can't get zoom updates for scaling my DivIcon

found the solution with useMapEvents hook provided in react-leaflet v3:
https://react-leaflet.js.org/docs/api-map#usemapevents

import {useMapEvents} from "react-leaflet";
import {useState} from "react";

function MyComponent() {
    const [zoomLevel, setZoomLevel] = useState(5); // initial zoom level provided for MapContainer
    
    const mapEvents = useMapEvents({
        zoomend: () => {
            setZoomLevel(mapEvents.getZoom());
        },
    });

    console.log(zoomLevel);

    return null
}

function MyMapComponent() {
    return (
        <MapContainer center={[50.5, 30.5]} zoom={5}>
            <MyComponent />
        </MapContainer>
    )
}

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