反应原生地图 animateToRegion 不起作用

时间:2021-06-24 10:28:39

标签: react-native react-native-maps

我一直在尝试在 React Native 地图中使用 animateToRegion 函数,但它不起作用,我不断收到此错误消息:“TypeError: undefined is not an object (evaluating 'this.mapView')”

这是我的代码:

animate(){
    let r = {
        latitude: 42.5,
        longitude: 15.2,
        latitudeDelta: 7.5,
        longitudeDelta: 7.5,
    };
   
    this.mapView.animateToRegion(r, 2000);
}
render() {
    return (
        <View style={styles.container}>
          <MapView
            region={this.state.region}
            style={styles.map}
            showsUserLocation={true}
            zoomEnabled={true}
            showsBuildings={true}
            ref = {(ref)=>this.mapView=ref}
            >
          </MapView>
          <TouchableOpacity
            onPress={this.animate}
            style={{...styles.buttonAdd, marginVertical: 0}}>
            <Text style={styles.buttonText}>Animate</Text>
          </TouchableOpacity>
        </View>
    );
  }
}

0 个答案:

没有答案