Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Region latitudeDelta / longitudeDelta into an approximate zoomLevel

I use the great react-native-maps from Airbnb on a react-native app.

I got a list of markers on a JSON file where each of these markers have a property zoom which is a integer of an approximate zoom level where the marker should display / hide on the map.

Is there a way based on the latitudeDelta and longitudeDelta of a Region to get an approximate double/integer of the current zoom level as we have on Google Maps (1 to 20) ?

Thanks

like image 507
Thmsct Avatar asked Oct 04 '17 15:10

Thmsct


2 Answers

Ok I come with an handy solution, I don't know if we can do better.

I added the onRegionChange event to retrieve the region, then I use some math :

<MapView
    style={styles.map}
    initialRegion={this.state.region}
    onRegionChange={region => {
        clearTimeout(this.timerForMap)
        this.timerForMap = setTimeout(() => {
            this.showMarkers(region)
        }, 100)
    }}>
    ...

Then :

showMarkers(region) {
    let zoom = Math.round(Math.log(360 / region.longitudeDelta) / Math.LN2)
    ...
}

If someone have a better way to do it, feel free to comment !

Thanks.

like image 122
Thmsct Avatar answered Nov 05 '22 12:11

Thmsct


you can get the zoom level from getCamera() using onRegionChange in MapView

const [zoom, setZoom] = useState(''); //initiates variable zoom

const getZoom = async () => {
  const coords = await mapRef.getCamera();
  setZoom(coords.center.zoom); // sets variable zoom the value under coords.center.zoom
}

<MapView>
ref = {(ref) => mapRef = ref}
 onRegionChange = {() => {getZoom();}}
</MapView>
like image 4
devashish desai Avatar answered Nov 05 '22 12:11

devashish desai