transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.66k stars 426 forks source link

maximumAge is not getting the last fetched location - getCurrentPosition bug #2142

Closed sainjay closed 1 month ago

sainjay commented 2 months ago

While using the getCurrentPosition method and setting maximumAge in the config, the location is not getting the last fetched location and always getting a new location.

Your Environment


  expo-env-info 1.2.0 environment info:
    System:
      OS: macOS 14.6.1
      Shell: 5.9 - /bin/zsh
    Binaries:
      Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
      Yarn: 3.6.1 - ~/.nvm/versions/node/v18.16.0/bin/yarn
      npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
      Watchman: 2024.05.06.00 - /opt/homebrew/bin/watchman
    Managers:
      CocoaPods: 1.15.2 - /opt/homebrew/bin/pod
    SDKs:
      iOS SDK:
        Platforms: DriverKit 23.2, iOS 17.2, macOS 14.2, tvOS 17.2, visionOS 1.0, watchOS 10.2
      Android SDK:
        API Levels: 28, 30, 31, 32, 33, 34
        Build Tools: 30.0.3, 33.0.0, 33.0.1, 34.0.0
        System Images: android-30 | Google Play ARM 64 v8a, android-31 | Google APIs ARM 64 v8a, android-34 | Google APIs ARM 64 v8a
    IDEs:
      Android Studio: 2024.1 AI-241.15989.150.2411.11948838
      Xcode: 15.2/15C500b - /usr/bin/xcodebuild
    npmPackages:
      expo: ^51.0.11 => 51.0.26 
      expo-router: ~3.5.23 => 3.5.23 
      react: 18.2.0 => 18.2.0 
      react-dom: 18.2.0 => 18.2.0 
      react-native: 0.74.5 => 0.74.5 
      react-native-web: ~0.19.6 => 0.19.12 
    Expo Workflow: bare

## Expected Behavior
<!--- Tell us what should happen -->
`maximumAge` should be taking the previously fetched location.

## Actual Behavior
<!--- Tell us what happens instead -->
Even `maximumAge` set in the config, `getCurrentPosition` is always fetching a new location.

## Steps to Reproduce
<!--- reproduce this issue; include code to reproduce, if relevant -->

let startTime = performance.now(); const location = await BackgroundGeolocation.getCurrentPosition({ ...appLocationConfig, }); let endTime = performance.now();

console.log(`Current Location from Background Geolocation: ${location}`);

console.log(`Location took ${endTime - startTime}ms`);

## Context
<!--- What were you trying to do? -->
Trying to reuse the last location fetched if within the `maximumAge` timelimit

## Debug logs
<!-- include iOS / Android logs
- ios XCode logs,
- use #getLog #emailLog methods (@see docs)
- Android: $ adb logcat -s TSLocationManager
-->
<details><summary>Logs</summary>

``` <!-- Syntax highlighting:  DO NOT REMOVE -->
                                                                                                    ╔═════════════════════════════════════════════
                                                                                                    ║ getCurrentPosition LocationResult: 5 (2886ms old)
                                                                                                    ╠═════════════════════════════════════════════
                                                                                                    ╟─ 📍  Location[fused 43.64****,-79.38**** hAcc=18.907 et=+1h42m14s722ms alt=54.099998474121094 vAcc=33.02328 {Bundle[{}]}], time: 1726520333731
2024-09-16 16:58:56.631 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService b] 
                                                                                                      🎾  STOP [LocationRequestService startId: 10, eventCount: 2]
2024-09-16 16:58:56.631 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      ⚙️︎   FINISH [LocationRequestService startId: 10, eventCount: 1, sticky: false]
2024-09-16 16:58:56.671 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.TSLocationManager onSingleLocationResult] 
                                                                                                      🔵  Acquired current position
2024-09-16 16:58:56.671 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.l.TSLocationManager a] Median accuracy: 18.477
2024-09-16 16:58:56.675 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      ⚙️︎   FINISH [LocationRequestService startId: 9, eventCount: 0, sticky: false]
2024-09-16 16:58:56.880 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService f] 
                                                                                                      ⚙️︎  LocationRequestService.stopSelfResult(10): true
2024-09-16 16:58:56.881 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService onDestroy] 
                                                                                                      🔴  LocationRequestService stopped
2024-09-16 16:59:05.082 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          W  [c.t.l.l.SingleLocationRequest finish] 
                                                                                                      ⚠️  📍  SingleLocationRequest TIMEOUT: 6
2024-09-16 16:59:05.083 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.SingleLocationRequest finish] 
                                                                                                      🔵  Found previous sample
2024-09-16 16:59:05.084 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.TSLocationManager a] 
                                                                                                    ╔═════════════════════════════════════════════
                                                                                                    ║ getCurrentPosition LocationResult: 6 (39722ms old)
                                                                                                    ╠═════════════════════════════════════════════
                                                                                                    ╟─ 📍  Location[fused 43.64****,-79.38**** hAcc=19.869 et=+1h41m46s352ms alt=54.099998474121094 vAcc=29.646803 vel=1.2690876 sAcc=1.5 bear=212.37807 bAcc=45.0 {Bundle[{persist=false}]}], time: 1726520305361
2024-09-16 16:59:05.137 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.TSLocationManager onSingleLocationResult] 
                                                                                                      🔵  Acquired current position
2024-09-16 16:59:05.165 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.TSLocationManager a] 
                                                                                                    ╔═════════════════════════════════════════════
                                                                                                    ║ getCurrentPosition LocationResult: 7 (11433ms old)
                                                                                                    ╠═════════════════════════════════════════════
                                                                                                    ╟─ 📍  Location[fused 43.64****,-79.38**** hAcc=18.907 et=+1h42m14s722ms alt=54.099998474121094 vAcc=33.02328 {Bundle[{}]}], time: 1726520333731
2024-09-16 16:59:05.165 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.l.TSLocationManager a] Median accuracy: 19.869
2024-09-16 16:59:05.169 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.u.LocationAuthorization withPermission] 
                                                                                                      ℹ️  LocationAuthorization: Permission granted
2024-09-16 16:59:05.204 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      🎾  start [LocationRequestService  startId: 1, eventCount: 1]
2024-09-16 16:59:05.204 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.SingleLocationRequest startUpdatingLocation] 
                                                                                                      🔵  [SingleLocationRequest start, action: 2, requestId: 7]
2024-09-16 16:59:05.205 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      ⚙️︎   FINISH [LocationRequestService startId: 1, eventCount: 0, sticky: true]
2024-09-16 16:59:12.283 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      🎾  2:7 [LocationRequestService  startId: 2, eventCount: 1]
2024-09-16 16:59:12.286 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.s.LocationRequestService b] 
                                                                                                      ℹ️  Location availability: false
2024-09-16 16:59:12.286 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      ⚙️︎   FINISH [LocationRequestService startId: 2, eventCount: 0, sticky: true]
2024-09-16 16:59:15.148 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          W  [c.t.l.l.SingleLocationRequest finish] 
                                                                                                      ⚠️  📍  SingleLocationRequest TIMEOUT: 7
2024-09-16 16:59:15.148 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.SingleLocationRequest finish] 
                                                                                                      🔵  Found previous sample
2024-09-16 16:59:15.150 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.TSLocationManager a] 
                                                                                                    ╔═════════════════════════════════════════════
                                                                                                    ║ getCurrentPosition LocationResult: 7 (21418ms old)
                                                                                                    ╠═════════════════════════════════════════════
                                                                                                    ╟─ 📍  Location[fused 43.64****,-79.38**** hAcc=18.907 et=+1h42m14s722ms alt=54.099998474121094 vAcc=33.02328 {Bundle[{persist=false}]}], time: 1726520333731
2024-09-16 16:59:15.164 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService b] 
                                                                                                      🎾  STOP [LocationRequestService startId: 3, eventCount: 1]
2024-09-16 16:59:15.165 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService a] 
                                                                                                      ⚙️︎   FINISH [LocationRequestService startId: 3, eventCount: 0, sticky: false]
2024-09-16 16:59:15.214 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          I  [c.t.l.l.TSLocationManager onSingleLocationResult] 
                                                                                                      🔵  Acquired current position
2024-09-16 16:59:15.214 26621-26854 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.l.TSLocationManager a] Median accuracy: 18.907
2024-09-16 16:59:15.366 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService f] 
                                                                                                      ⚙️︎  LocationRequestService.stopSelfResult(3): true
2024-09-16 16:59:15.366 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.service.AbstractService onDestroy] 
                                                                                                      🔴  LocationRequestService stopped
2024-09-16 17:03:49.908 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.l.LifecycleManager onPause] ☯️  onPause
2024-09-16 17:03:49.908 26621-26621 TSLocationManager       com.incapsulate.ltcprovider          D  [c.t.l.l.LifecycleManager onStop] ☯️  onStop

sainjay commented 2 months ago

Just checked, doesn't seem to be happening in iOS. Updated my libraries to use the latest version of both the libaries:

"react-native-background-fetch": "^4.2.5", "react-native-background-geolocation": "^4.17.1",

christocracy commented 1 month ago

The problem is your desiredAccuracy: 1 provided to .getCurrentPosition.

The options.desiredAccuracy for .getCurrentPosition doesn't have the same meaning as that supplied to .ready(config). When you provide desiredAccuracy: 1 to .getCurrentPosition, you're saying "I desire a location with a minimum accuracy of 1 meter". (NOTE .getCurrentPosition always turns on location-services to request the highest possible accuracy).

Of course, a location with accuracy: 1 is impossible since the best accuracy you'll ever get is 5 meters. The plugin looks at its last-known-location and says "That last location cannot satisfy your desiredAccuracy: 1 -- I will turn on location-services to fetch a new location and return the best possible".

Solution: increase your options.desiredAccuracy (eg: 100).

sainjay commented 1 month ago

yes, working as expected after increasing the desiredAccuracy to 150 and initial config accuracy is 100. Thanks!