简体   繁体   English

react-leaflet v3 缩放监听器

[英]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在这种情况下类似React-Leaflet: 缩放标记后缩放

But react-leaflet in v3 doesn't have onZoomChange event for MapContainer.但是 v3 中的 react-leaflet 没有 MapContainer 的 onZoomChange 事件。 So i can't get zoom updates for scaling my DivIcon所以我无法获得缩放我的 DivIcon 的缩放更新

found the solution with useMapEvents hook provided in react-leaflet v3:使用 react-leaflet v3 中提供的 useMapEvents 钩子找到了解决方案:
https://react-leaflet.js.org/docs/api-map#usemapevents 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>
    )
}

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

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