我希望我的位置坐标甚至应用程序关闭。我该怎么做呢 ?
我使用下面的代码每1秒获取我的位置坐标,但它总是给我相同的坐标。我不知道为什么它不是更新。
即使这是工作,我不能在应用程序关闭时获得协调。对此有何想法?
我这样做了:
componentDidMount() {
this.interval = setInterval(() => {
navigator.geolocation.getCurrentPosition((position) => {
var lat = parseFloat(position.coords.latitude);
var lng = parseFloat(position.coords.longitude);
console.log("lat :",lat);
console.log("lng :",lng);
this.setState({position1: lat, position2: lng});
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 30000, maximumAge: 1000 }
);
}, 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
如果应用程序在后台或带到前台,您可能想利用AppState api和AppState.addEventListener('change', this._handleAppStateChange);
查看位置更改。如果该应用程序已关闭,我不太确定如果该应用程序已关闭,本机是否具有api观看位置。但是你可以使用react-native-queue来做到这一点。你可能想看看https://github.com/billmalarky/react-native-queue#os-background-task-full-example。