如何解决react nativie 使用 react-native-maps 获取当前位置
我想使用 react-native-maps 将用户的当前位置设置为初始屏幕。
我尝试了很多,但都没有达到我想要的效果。
试图填充 state 的 null 值是没有用的。
成功获取到用户的位置信息,但是没有反映在状态值中。
有什么解决办法吗?
import React from 'react';
import { StyleSheet,Image } from 'react-native';
import MapView,{ Marker } from 'react-native-maps';
import * as Location from 'expo-location';
export default class MapPage extends React.Component {
constructor(props) {
super(props);
this.state = {
latitude: null,longitude: null
};
}
componentDidMount = () => {
this.getCurrentLocation();
}
getCurrentLocation = async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
return;
}
let location = await Location.getCurrentPositionAsync({});
this.setState({
...this.state,latitude: location.coords.latitude,longitude: location.coords.longitude
});
//console.log(this.state.latitude);
}
render() {
return (
<MapView
style={styles.container}
initialRegion={{
latitude: this.state.latitude,longitude: this.state.longitude,latitudeDelta: 0.0922,longitudeDelta: 0.0421
}}>
</MapView>
);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。