【发布时间】:2020-12-11 08:01:30
【问题描述】:
我正在尝试实现在 react native 中搜索和查明位置。我正在使用 react-native-maps 和 react-native-google-places-autocomplete 包,因为它们的明显用途。
首先,我在该州启动了区域:
constructor(){
this.state={
mapRegion: {
latitude: this.props.latitude ? this.props.latitude : 27.7172,
longitude: this.props.longitude ? this.props.longitude : 85.3240,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
},
}
}
我尝试在按下自动完成的搜索结果以及拖动标记时更新区域。但我得到一个错误:
Error: You attempted to set the key latitude with the value '27' on an object that is meant to be immutable and has been frozen.
我已将代码实现为:
<GooglePlacesAutocomplete
placeholder='Search'
fetchDetails={true}
onPress={(data, details = null) => {
let tempMapRegion = this.state.mapRegion;
tempMapRegion.latitude = details.geometry.location.lat;
tempMapRegion.longitude = details.geometry.location.lng;
this.setState({ mapRegion: tempMapRegion })
}}
query={{
key: AppSettings.googleMapApiKey,
language: 'en',
}}
/>
<MapView
initialRegion={{
latitude: this.props.latitude ? this.props.latitude : 27.7172,
longitude: this.props.longitude ? this.props.longitude : 85.3240,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
}}
region={this.state.mapRegion}
onRegionChange={(e) => { this.onRegionChange(e) }}
style={{ height: 300, width: 300 }}
>
<Marker draggable
coordinate={this.state.mapRegion}
onDragEnd={(e) => {
let tempMapRegion = this.state.mapRegion;
tempMapRegion.latitude = e.nativeEvent.coordinate.latitude
tempMapRegion.longitude = e.nativeEvent.coordinate.longitude
this.setState({ mapRegion: tempMapRegion })
}}
/>
</MapView>
MapView中的onRegionChange运行流畅,marker会自动拖到中心,但反之则出现上述错误。
是什么导致了这个错误,我该如何解决这个问题?
【问题讨论】:
标签: react-native google-maps react-native-maps