React native 在Reactnative中自动获取当前位置的步骤
我有这个方法通过点击按钮获取当前位置,它将获取当前位置。我想在不点击该按钮的情况下获取位置React native 在Reactnative中自动获取当前位置的步骤,react-native,react-native-android,react-native-maps,React Native,React Native Android,React Native Maps,我有这个方法通过点击按钮获取当前位置,它将获取当前位置。我想在不点击该按钮的情况下获取位置 getLocationHandler = () =>{ navigator.geolocation.getCurrentPosition(pos =>{ const coordsEvent ={ nativeEvent :{ coordinate : { latitude : pos.coords.latitude,
getLocationHandler = () =>{
navigator.geolocation.getCurrentPosition(pos =>{
const coordsEvent ={
nativeEvent :{
coordinate : {
latitude : pos.coords.latitude,
longitude : pos.coords.longitude
}
}
};
this.pickLocationHandler(coordsEvent);
},
err => {
console.log(err);
alert("Fetching the position failed,Please pick one manually")
})
}
您需要将位置保留在状态上,并在安装组件时请求它。 使用
react native maps
,获取位置是一项异步任务,因此您需要以下解决方案:
constructor(props){
super(props);
state = {
myLocation: null
};
}
componentDidMount = () => {
this.getMyLocation();
};
getMyLocation = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
myLocation: 'Permission denied',
});
}
let location = await Location.getCurrentPositionAsync({});
this.setState({
myLocation: JSON.stringify(location)
});
};
哦,哦,你可以用谷歌搜索
反应本机映射获取当前位置
并发现此github问题:
顺便说一句,github问题的答案值得称赞。万岁 您需要将位置保留在状态上,并在安装组件时请求它。 使用
react native maps
,获取位置是一项异步任务,因此您需要以下解决方案:
constructor(props){
super(props);
state = {
myLocation: null
};
}
componentDidMount = () => {
this.getMyLocation();
};
getMyLocation = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
myLocation: 'Permission denied',
});
}
let location = await Location.getCurrentPositionAsync({});
this.setState({
myLocation: JSON.stringify(location)
});
};
哦,哦,你可以用谷歌搜索
反应本机映射获取当前位置
并发现此github问题:
顺便说一句,github问题的答案值得称赞。万岁 您想定期获取位置信息吗?特定事件后?我想在地图屏幕打开时显示当前位置您想定期获取位置吗?在特定事件之后?我想在地图屏幕打开时显示当前位置