react-native-maps:如何在按下按钮时跟踪用户位置

kir*_*imi 5 javascript react-native react-native-maps

我正在使用react-native-maps并且效果很好。但我有两个问题。

  1. 我可以使用 自动跟踪用户位置,followsUserLocation但是当我移动地图时,它不断将我拖回用户位置。我怎么能解决这个问题?

  2. 我想要刷新按钮,当用户按下按钮时,我希望我的地图视图跟随用户位置。

    constructor() {
        super();
        this.state = {
            followsUserLocation: true,
        };
    }
    
    
    mapView() {
        return(
            <MapView style={styles.map}
            showsUserLocation
            followsUserLocation={this.state.followsUserLocation}
            initialRegion={{
            latitude: 37.523814,
            longitude:  126.927494,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421}}/>
       )
    }
    
    Run Code Online (Sandbox Code Playgroud)

任何意见或建议将不胜感激!提前致谢!

sat*_*mar 2

尝试 onUserLocationChange 回调并使用 setState 相应地设置您的区域

   state = {
    showsUserLocation: true,
    followsUserLocation : true,
    mapRegion: {
      latitude: 37.78825,
      longitude: -122.4324,
      latitudeDelta: 0.0922,
      longitudeDelta: 0.0421,
    },
  };

Run Code Online (Sandbox Code Playgroud)
    <MapView
            style={{ flex: 1 }}
            region={this.state.mapRegion}
            //use this callback to update the regions
            onUserLocationChange={event => console.log(event.nativeEvent)}
            showsUserLocation={this.state.showsUserLocation}
            followsUserLocation={this.state.followsUserLocation}
          />
Run Code Online (Sandbox Code Playgroud)