react-native-maps:如何按下按钮时的用户位置

问题描述 投票:2回答:1

我正在使用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}}/> ) }

任何意见或建议将非常感谢!提前致谢!

javascript react-native react-native-maps
1个回答
0
投票

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

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

    <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}
          />
© www.soinside.com 2019 - 2025. All rights reserved.