Buw*_*era 0 react-native react-native-maps
在我的应用程序中,我有谷歌地图。它的实现如下所示。
<MapView style={[styles.mapStyle, { flex: 1, width: this.state.mapWidth }]}
initialRegion={{
latitude: this.state.latitude,
longitude: this.state.longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1,
}}
provider={PROVIDER_GOOGLE}
onPress={this.onMapPress.bind(this)}
showsUserLocation={true}
followsUserLocation={true}
showsMyLocationButton={true}
showsCompass={true}
showsTraffic={true}
toolbarEnabled={true}
onMapReady={() => this.setState({ width: width - 1 })}
>
<Marker draggable
coordinate={{
latitude: this.state.latitude,
longitude: this.state.longitude,
}}
onDragEnd={(e) => this.setState({ x: e.nativeEvent.coordinate })}
/>
<Circle
center={{
latitude: this.state.latitude,
longitude: this.state.longitude,
}}
radius={this.state.radius}
fillColor='rgba(253, 48, 4,0.5)'
strokeColor='rgba(253, 48, 4,1)'
/>
</MapView>
Run Code Online (Sandbox Code Playgroud)
当我单击一个按钮时,我会更改地图区域的状态值。
changeRegion(){
this.setState({ latitude: 6.86, longitude: 6.86 });
}
Run Code Online (Sandbox Code Playgroud)
这成功地改变了地图标记的位置。
我的问题是,地图没有移动到选定的位置。我怎样才能做到这一点?
为您的地图创建参考
const mapRef = React.createRef();
Run Code Online (Sandbox Code Playgroud)
将 ref 传递给您的 Map
<Map
ref={mapRef}
// Rest of your props
/>
Run Code Online (Sandbox Code Playgroud)
修改你的函数
changeRegion(){
const latitude = 6.86;
const longitude = 6.86;
this.setState({ latitude, longitude });
mapRef.current.animateToRegion({
latitude,
longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3161 次 |
| 最近记录: |