如何在React Naitve Maps中使用getMapBoundaries

问题描述

当前正在为公司创建应用程序。该应用程序的用户显示在地图上,并且他们的宠物也应该显示在地图上。为此,我需要地图的边界。我正在使用react-native-maps,并将其作为内置函数getMapBoundaries。使用该功能时,出现以下错误

function getMapBoundaries does not exist

最新的

Cannot set property map of undefined. 

这就是我所拥有的

const AppMapView = () => {
  
    const handleRegionChange = async region => {
      //I get an error for the statement below
      console.log(
        await this.map.getMapBoundaries()
          .then(response => {
            return response
          })
      )
    };
  
    handleRegionChange()
  
    return (
      <View style={styles.container}>
        <MapView
          ref={ref => {
            this.map = ref;
          }}
        >
          <Marker
            coordinate={...}
          >
            <Image {...} />
          </Marker>
        </MapView>
      </View>
    )
};
  
  export default AppMapView;

一个代码示例将不胜感激。

解决方法

此代码对我有用:

mapRef = null;
onRegionChangeComplete = async () => {
  console.log("onRegionChangeComplete",await this.mapRef.getMapBoundaries());
};

 <MapView
    ref={(ref) => {
       this.mapRef = ref;
    }}
    onRegionChangeComplete={this.onRegionChangeComplete}
    <Marker
       coordinate={...}
    >
    </Marker>
  </MapView>
,

如果有人仍在寻找答案,这很有效:

<MapView
    ref={mapView}
    onRegionChangeComplete={async (val) => {
       console.log(await mapRef.current.getMapBoundaries())
    }
>
    <Marker
       coordinate={...}
    >
    </Marker>
</MapView>

你可以从当前引用中获取它

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...