Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current Leaflet map zoom level?

I'm trying to get the zoom level of a map in real time to make a button that locks the zoom with the current value. I have tried using getMapZoom and getZoom but both give me an undefined value. I think I'm not using the correct ref but I haven't been able to find much documentation about it. Here's the code:

<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}

                    >
like image 821
ODLana Avatar asked Jan 14 '20 12:01

ODLana


People also ask

Which method is used to get the current zoom level of the map?

The getZoom() method returns the current zoom level of the map.

What is zoom level in map?

A zoom level or scale is a number that defines how large or small the contents of a map appear in a map view . Scale is a ratio between measurements on a map view and measurements in the real-world.

How do you remove zoom control in leaflet?

You can remove the default zoom control by setting the zoomControl option of the map options to false.


1 Answers

In pure leaflet if you defined map as const map = L.map("map", options) than you just call map.getZoom().

In constructor this.mapRef = React.createRef() In Map element:

    ref={this.mapRef}
    onzoomend={() => console.log(this.mapRef.current.leafletElement.getZoom()}
like image 106
radulle Avatar answered Sep 18 '22 16:09

radulle