代码之家  ›  专栏  ›  技术社区  ›  Thmsct

将区域latitudeDelta/longitudeDelta转换为近似zoomLevel

  •  12
  • Thmsct  · 技术社区  · 7 年前

    我用伟大的 react-native-maps react-native 应用程序。

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

    latitudeDelta longitudeDelta Region

    谢谢

    2 回复  |  直到 7 年前
        1
  •  30
  •   Thmsct    7 年前

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

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

    如果有人有更好的方法,请随时发表评论!

        2
  •  4
  •   devashish desai    3 年前

    可以使用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>