将 Region latitudeDelta / longitudeDelta 转换为近似的 ZoomLevel

问题描述 投票:0回答:2

我在

react-native-maps
应用程序上使用来自 Airbnb 的出色
react-native

我在 JSON 文件上得到了一个标记列表,其中每个标记都有一个属性

zoom
,它是标记应在地图上显示/隐藏的近似缩放级别的整数。

有没有一种方法基于

latitudeDelta
longitudeDelta
Region
来获得当前缩放级别的近似双倍/整数,就像我们在 Google 地图上一样(1 到 20)?

谢谢

react-native react-native-maps
2个回答
32
投票

好吧,我有一个方便的解决方案,我不知道我们是否可以做得更好。

我添加了

onRegionChange
事件来检索区域,然后我使用一些数学:

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

然后:

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

如果有人有更好的方法,欢迎评论!

谢谢。


4
投票

您可以使用 MapView 中的 onRegionChange 从 getCamera() 获取缩放级别

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>
© www.soinside.com 2019 - 2024. All rights reserved.