Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get zoom level in react native map

I'm trying to calculate the zoom level when user zoom in or out,
I find that react native maps has a onRegionChange which has longiture, latitude, longitude delta and latitude delta which (I guess) could be used to compute zoom level
from this response I tried this way:

 Math.round(Math.log(360 / this.state.longitudeDelta) / Math.LN2);

but it return a big number (which is not the zoom level).

like image 453
aName Avatar asked Dec 15 '25 18:12

aName


2 Answers

Write a function like below in the onRegionChangeComplete prop of react-native-map

changeRegion = debounce(async region => {
    console.log(
    Math.log2(360 * (metrics.screenWidth / 256 / region.longitudeDelta)) + 1
    );
  }, 1000);

or to be specific , Math.log2(360 * (metrics.screenWidth / 256 / region.longitudeDelta)) + 1); does the magic.

like image 84
FortuneCookie Avatar answered Dec 17 '25 07:12

FortuneCookie


     <MapView
          onRegionChangeComplete={async (region) => {
            const coords = await ref?.current?.getCamera();
            console.log('coords', coords.zoom);
          }}
          ref={ref} />
like image 26
Furkan Guler Avatar answered Dec 17 '25 09:12

Furkan Guler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!