请提供以下内容: 1. SDK版本: 37 2.平台( Android /iOS/web/all):Android
我正在使用Expo版本: 37,在平台Android中,我想问,有没有办法让应用程序通知当前用户的位置--即使用户不移动,我尝试每5分钟在中记录用户位置,它与Location.startLocationUpdatesAsync一起工作(见下面的代码),但是如果用户坐着的时候没有移动很长时间,它就没有更新位置,是如何记录用户位置的,尽管用户没有移动,因为下面的代码会每10秒启动一次,但是如果对象没有移动,它不生成新的位置数据(请参见const {纬度,经度}= data.locations.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);
}
});
发布于 2020-04-11 07:41:02
由于您需要每5分钟记录一次用户位置,我可以看到两个选项:
Location.startLocationUpdatesAsync
侦听位置更改,而是设置一个间隔,每5分钟检索一次当前位置,例如:。
setInterval(() => {
const location = await getCurrentLocation();
doSomethingWithLocation(location);
}, 300000)
。
https://stackoverflow.com/questions/61153035
复制相似问题