React native 如何使用react native每x秒监控或更新用户位置?
如何使用react native每x秒监控或更新用户位置 我的代码:React native 如何使用react native每x秒监控或更新用户位置?,react-native,geolocation,setinterval,React Native,Geolocation,Setinterval,如何使用react native每x秒监控或更新用户位置 我的代码: export default class app extends React.Component { constructor(props){ super(props); this.state = { ready: false, where: {lat:null, lng:null}
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>
);
}
}
导出默认类app扩展React.Component{
构造器(道具){
超级(道具);
this.state={
就绪:错误,
其中:{lat:null,lng:null},
错误:null,
时间:字符串(新日期()),
};
}
componentDidMount(){
让geoOptions={
EnableHighAccurance:错误,
超时:20000,
};
this.setState({ready:false,error:null});
Geolocation.getCurrentPosition(this.geosucces,
这是一次失败,
地理选项);
设置间隔(()=>{
Geolocation.getCurrentPosition(this.geosucces,
这是一次失败,
地理选项);
this.setState((oldState)=>{
返回{
时间:字符串(新日期())
};
});
}, 1000);
}
地理成功=(位置)=>{
控制台.日志(位置.坐标.纬度);
这个.setState({
准备好了吗,
其中:{lat:position.coords.latitude,lng:position.coords.longitude}
})
}
geofilure=(err)=>{
this.setState({error:err.message});
}
render(){
报税表(
{!this.state.ready&&(
在React Native中使用地理定位。
)}
{this.state.error&&(
错误:{this.state.Error}
)}
{this.state.ready&&(
纬度:{this.state.where.lat}{'\n'}{'\n'}
经度:{this.state.where.lng}{'\n'}{'\n'}
{this.state.time}
)}
);
}
}
我使用setInterval每1秒更新一次用户的位置
当它第一次获得位置时,以及当它检测到一个新位置时
但最后还是不行了
知道怎么做吗???
多谢各位