【发布时间】:2019-12-03 18:21:05
【问题描述】:
我正在尝试让地图在屏幕启动时显示当前位置。 当已授予位置权限时,该代码运行良好。 当请求权限时,地图会在更新状态之前从状态中获取默认值。 我在我的应用程序中使用 redux。
LocationScreen.js
Geolocation.getCurrentPosition(position => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
this.props.dispatch({type: 'UPDATE_LOCATION', location:{latitude:latitude,longitude:longitude}})
});
}
render() {
const location = this.props.location;
return (
<View style={styles.MainContainer}>
<StatusBar backgroundColor="#ffffff" barStyle="dark-content" />
<View pointerEvents="none" style={{zIndex:99 ,position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, alignItems: 'center', justifyContent: 'center', backgroundColor: 'transparent'}}>
<Image pointerEvents="none" style={{height:59,width:40, marginBottom:60}} source={require('../../../assets/marker1.png')}/>
</View>
<MapView
provider={PROVIDER_GOOGLE} // remove if not using Google Maps
style={{width:'100%',height:'100%'}}
initialRegion={{
latitude: location.latitude,
longitude: location.longitude,
latitudeDelta: 0.00005,
longitudeDelta: 0.0021,
}}
onRegionChange={(region) => {
this.props.dispatch({type: 'UPDATE_LOCATION', location:{latitude:region.latitude,longitude:region.longitude} });
}}
>
{/* <Marker
coordinate={{latitude:location.latitude,longitude:location.longitude}}
/> */}
</MapView>
<View style={styles.bottomView}>
<TouchableOpacity style={{ height: 40, width:'80%', backgroundColor:'#00665c', justifyContent:'center', alignItems:'center', marginHorizontal:'10%', marginBottom:20 }}
onPress={() => this.props.navigation.navigate('Details_one_reg')}
>
<Text style={{color:'white'}}>Next</Text>
</TouchableOpacity>
<Text style={{color:'gray',marginLeft:30,marginBottom:25}}>Drag and drop pin on the location of the Shop</Text>
<Text style={{fontWeight:'bold',fontSize:16, marginLeft:30, marginBottom:4}}>Location</Text>
</View>
</View>
);
}
}
const mapStateToProps = (state) =>{
return {
location : state.location,
}
}
export default connect(mapStateToProps)(LocationScreen);
状态树
export const stateTree = {
auth:{
mobile_no:7397440807,
otp:100000,
},
location:{
latitude:0,
longitude:0,
},
inventory : [
【问题讨论】:
标签: react-native redux react-native-maps