请提供以下信息:1. SDK版本:37 2.平台(Android/iOS/web/all):安卓系统
我使用的是Expo SDK版本:37,在Android平台上,我想问,是否有一种方法可以让应用程序通知当前用户位置,即使用户不移动,我尝试每5分钟记录用户位置**,它使用Location.startLocationUpdatesAsync(见下面的代码),但如果用户长时间不移动,例如当用户坐着时,它不是更新的位置,我如何记录用户位置,虽然用户不移动,因为下面的代码startLocationUpdatesAsync将每10秒启动一次,但如果对象不移动,它不会生成新的位置数据(见const {latency,经度} = data.locations[0].coords)
useEffect(() => {
async function startWatching() {
locationService.subscribe(onLocationUpdate)
try {
const { granted } = await Location.requestPermissionsAsync();
if (!granted) {
throw new Error('Location permission not granted');
}
let isRegistered = await TaskManager.isTaskRegisteredAsync('firstTask');
if (isRegistered) {
TaskManager.unregisterTaskAsync('firstTask')
}
await Location.startLocationUpdatesAsync('firstTask', {
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 10000,
activityType: Location.ActivityType.AutomotiveNavigation,
deferredUpdatesInterval: 15000
});
} catch (e) {
setErr(e);
}
};
startWatching()
}, []);
TaskManager.defineTask('firstTask', ({ data, error }) => {
if (error) {
// Error occurred - check `error.message` for more details.
return;
}
if (data) {
const { latitude, longitude } = data.locations[0].coords
locationService.setLocation({latitude, longitude})
// console.log('locations', locations);
}
});
字符串
2条答案
按热度按时间mrzz3bfm1#
由于需要每5分钟记录一次用户位置,我可以看到两个选项:
1.不要使用
Location.startLocationUpdatesAsync
侦听位置更改,而是设置一个间隔,每5分钟检索一次当前位置,例如:字符串
1.继续监听位置变化,但也要设置一个时间间隔,每5分钟从位置服务中检索当前位置并使用它。如果位置在这段时间内没有改变,它将简单地发送以前的值。
yhxst69z2#
您可以执行以下操作(其中CHANGE_FETCH是在位置发生更改时要更新的值,NO_CHANGE_FETCH是在位置未发生更改时的值):
字符串