我怎样才能得到当前的传单地图缩放级别?

How can I get the current Leaflet map zoom level?

我正在尝试实时获取地图的缩放级别,以制作一个将缩放锁定为当前值的按钮。我试过使用 getMapZoom 和 getZoom 但都给了我一个未定义的值。我想我没有使用正确的参考文献,但我没能找到很多关于它的文档。这是代码:

<Map className="map-layer" 
          center={center} 
          onoverlayadd={this.overlayadd} 
          onoverlayremove={this.overlayremove}
          ondragend={this.zoomChange}
          onzoomend={console.log('Zoom: ' + this.mapRef.leafletElement.getMapZoom())}
          zoom={this.state.zoom}
          ref={this.mapRef}
          preferCanvas={false}
          animate={true}
          scrollWheelZoom={this.state.zoomLock ? false : true}
          doubleClickZoom={this.state.zoomLock ? false : true}
          touchZoom={this.state.zoomLock ? false : true}
          maxZoom={7}
          minZoom={7}

                    >

在纯传单中,如果您将地图定义为 const map = L.map("map", options),那么您只需调用 map.getZoom()

在构造函数中this.mapRef = React.createRef() 在地图元素中:

    ref={this.mapRef}
    onzoomend={() => console.log(this.mapRef.current.leafletElement.getZoom()}

在反应中你可以通过使用getZoom method()和useRef来获得zoomLevel。

1) const mapRef = useRef(null) 

2) const getMapZoom = () => {
 return mapRef && console.log("object", mapRef.current.getZoom());
 };

 3) <MapContainer
    className="markercluster-map"
    center={center}
    zoom={ZOOM_LEVEL}
    maxZoom={MAX_ZOOM}
    ref={mapRef}
    whenCreated={(mapInstance) => (mapRef.current = mapInstance)}
    whenReady={() => {}}
    >