React Native 如何使用Expo后台服务Location.startLocationUpdatesAsync()保持更新位置,即使对象没有移动,

zf2sa74q  于 2023-08-07  发布在  React
关注(0)|答案(2)|浏览(232)

请提供以下信息:1. SDK版本:37 2.平台(Android/iOS/web/all):安卓系统
我使用的是Expo SDK版本:37,在Android平台上,我想问,是否有一种方法可以让应用程序通知当前用户位置,即使用户不移动,我尝试每5分钟记录用户位置**,它使用Location.startLocationUpdatesAsync(见下面的代码),但如果用户长时间不移动,例如当用户坐着时,它不是更新的位置,我如何记录用户位置,虽然用户不移动,因为下面的代码startLocationUpdatesAsync将每10秒启动一次,但如果对象不移动,它不会生成新的位置数据(见const {latency,经度} = data.locations[0].coords)

  1. useEffect(() => {
  2. async function startWatching() {
  3. locationService.subscribe(onLocationUpdate)
  4. try {
  5. const { granted } = await Location.requestPermissionsAsync();
  6. if (!granted) {
  7. throw new Error('Location permission not granted');
  8. }
  9. let isRegistered = await TaskManager.isTaskRegisteredAsync('firstTask');
  10. if (isRegistered) {
  11. TaskManager.unregisterTaskAsync('firstTask')
  12. }
  13. await Location.startLocationUpdatesAsync('firstTask', {
  14. accuracy: Location.Accuracy.BestForNavigation,
  15. timeInterval: 10000,
  16. activityType: Location.ActivityType.AutomotiveNavigation,
  17. deferredUpdatesInterval: 15000
  18. });
  19. } catch (e) {
  20. setErr(e);
  21. }
  22. };
  23. startWatching()
  24. }, []);
  25. TaskManager.defineTask('firstTask', ({ data, error }) => {
  26. if (error) {
  27. // Error occurred - check `error.message` for more details.
  28. return;
  29. }
  30. if (data) {
  31. const { latitude, longitude } = data.locations[0].coords
  32. locationService.setLocation({latitude, longitude})
  33. // console.log('locations', locations);
  34. }
  35. });

字符串

mrzz3bfm

mrzz3bfm1#

由于需要每5分钟记录一次用户位置,我可以看到两个选项:
1.不要使用Location.startLocationUpdatesAsync侦听位置更改,而是设置一个间隔,每5分钟检索一次当前位置,例如:

  1. setInterval(() => {
  2. const location = await getCurrentLocation();
  3. doSomethingWithLocation(location);
  4. }, 300000)

字符串
1.继续监听位置变化,但也要设置一个时间间隔,每5分钟从位置服务中检索当前位置并使用它。如果位置在这段时间内没有改变,它将简单地发送以前的值。

yhxst69z

yhxst69z2#

您可以执行以下操作(其中CHANGE_FETCH是在位置发生更改时要更新的值,NO_CHANGE_FETCH是在位置未发生更改时的值):

  1. // starts the background location updates
  2. const startLocationUpdates = async () => {
  3. // start the task
  4. console.log('gets here');
  5. Location.startLocationUpdatesAsync(NO_CHANGE_FETCH, {
  6. accuracy: Location.Accuracy.Highest,
  7. distanceInterval: 0, // minimum change (in meters) betweens updates
  8. deferredUpdatesInterval: MILI * STAG_MAXTIME, // minimum interval (in milliseconds) between updates
  9. // foregroundService is how you get the task to be updated as often as would be if the app was open
  10. foregroundService: {
  11. notificationTitle: 'Using your location',
  12. notificationBody: 'To turn off, go back to the app and switch something off.',
  13. },
  14. });
  15. Location.startLocationUpdatesAsync(CHANGE_FETCH, {
  16. accuracy: Location.Accuracy.Highest,
  17. distanceInterval: MIN_DIST, // minimum change (in meters) betweens updates
  18. deferredUpdatesInterval: MILI * MOVING_MAXTIME, // minimum interval (in milliseconds) between updates
  19. // foregroundService is how you get the task to be updated as often as would be if the app was open
  20. foregroundService: {
  21. notificationTitle: 'Using your location',
  22. notificationBody: 'To turn off, go back to the app and switch something off.',
  23. },
  24. });
  25. };

字符串

展开查看全部

相关问题