“ null不是对象(评估'this $ state $ region.latitude')”

时间:2019-11-25 20:07:50

标签: react-native-maps

enter image description here从'react'导入React;     从'react-native-maps'导入MapView;     从'react-native'导入{StyleSheet,View,Dimensions,Permissions,Location};     从'./components/Destinationbutton'导入Destinationbutton;     从'./components/CurrentLocationButton'导入CurrentLocationButton;     导出默认类应用扩展了React.Component {       构造函数(道具){         超级(道具);         this.state = {           地区:null,           }           this._getLocationAsync();           }           _getLocationAsync =异步()=> {             让{状态} =等待Permissions.askAsync(Permissions.LOCATION);             如果(状态!=='授予')             console.log('访问位置的权限被拒绝。');             let location = await Location.getCurrentPositionAsync({enableHighAccuracy:true});

        let region = {
          latitude :   location.coords.latitude,
          longitude : location.coords.longitude,
          latitudeDelta : 0.045,
          longitudeDelta : 0.045
        }
        this.setState({region: region}) 
      } 
    centerMap() {
        const { latitude,
             longitude,
             latitudeDelta,
             longitudeDelta } =  this.state.region ;

        this.map.animateToRegion({
            latitude,
            longitude,
            latitudeDelta,
            longitudeDelta
        })
    }      
  render() {
    return (
      <View style={styles.container}>
        <MapView
        initialRegion={this.state.region}
        showsUserLocation={true}
        showsCompass={true}
        ref={(map) =>{this.map = map }}
        rotateEnable={true}
        style={{flex:1,zIndex:0}}
      />
      <Destinationbutton />
      <CurrentLocationButton cb={() => { this.centerMap() }} />
      </View>
    );
  }}
const styles = StyleSheet.create({
  container: {
    flex:1,
    backgroundColor: '#fff',
    zIndex:0

  },
});

0 个答案:

没有答案