react-native - 如何使用本机反应每 x 秒监控或更新用户位置?
问题描述
如何使用本机反应每 x 秒监控或更新用户位置?
我的代码:
export default class app extends React.Component {
constructor(props){
super(props);
this.state = {
ready: false,
where: {lat:null, lng:null},
error: null,
time: String(new Date()),
};
}
componentDidMount(){
let geoOptions = {
enableHighAccuracy:false,
timeOut: 20000,
};
this.setState({ready:false, error: null });
Geolocation.getCurrentPosition( this.geoSuccess,
this.geoFailure,
geoOptions);
setInterval(() => {
Geolocation.getCurrentPosition( this.geoSuccess,
this.geoFailure,
geoOptions);
this.setState((oldState) => {
return {
time: String(new Date())
};
});
}, 1000);
}
geoSuccess = (position) => {
console.log(position.coords.latitude);
this.setState({
ready:true,
where: {lat: position.coords.latitude,lng:position.coords.longitude }
})
}
geoFailure = (err) => {
this.setState({error: err.message});
}
render() {
return (
<View style={styles.container}>
{ !this.state.ready && (
<Text style={styles.big}>Using Geolocation in React Native.</Text>
)}
{ this.state.error && (
<Text style={styles.big}>Error: {this.state.error}</Text>
)}
{ this.state.ready && (
<Text style={styles.big}>
Latitude: {this.state.where.lat}{'\n'}{'\n'}
Longitude: {this.state.where.lng} {'\n'}{'\n'}
{this.state.time}
</Text>
)}
</View>
);
}
}
我使用 setInterval 每 1 秒获取一次用户的位置更新。
当它第一次获得位置时,再次,每当它检测到新位置时
但最后不知道该怎么做???非常感谢
解决方案
推荐阅读
- azure-active-directory - Azure AD DS 和漫游配置文件
- reactjs - 未处理的拒绝(TypeError):this.props.dispatch(...).then 不是函数
- algorithm - 为保证唯一性但最多允许 3 个面相同的拼图块分配边缘值的算法?
- c - C中字符的频率(当我阅读字符时发出)
- split - 我们什么时候应该将数据集拆分为测试和训练?
- node.js - 如何清除nodejs中测试之间的全局变量?
- amazon-web-services - VPC 中的 Elastic Beanstalk 应用程序无法公开访问
- algorithm - 如果我有排序数组,有没有办法在合并排序算法中恢复两个排序的一半?
- python - 带有示例数据集但延迟下载的 Python 包?
- python - Pyglet 3-D 的渲染在图像中添加了额外的像素