Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.6k stars 291 forks source link

getCurrentPosition: cached location retrieved when enabling location in app background mode and moving back to app #307

Open sh-helen opened 2 years ago

sh-helen commented 2 years ago

Hi!

I have trouble with getting up-to-date location, when enabling location in background and going back to the app. I have screen that shows nearby location, the flow is:

  1. Set your current location to some point, i. e Paris
  2. Go to the app and enable location
  3. Quit app and disable location in settings
  4. Set your current location to different point, i. e London
  5. Go to the app again
  6. Since user has previously denied location, popup to enable location will be shown
  7. Go to settings and enable location (app is in background state at this step)
  8. Return to the app and for first 3-5 sec location returned is old one/cached one (i. e not London, but Paris set on the first step)
handleAppStateChange = (nextAppState: string): void => {
    if (
      this.state.appState.match(/inactive|background/) &&
      nextAppState === "active"
    ) {
      Geolocation.getCurrentPosition(
        (position) => {
            console.log(position)
        },
        (error) => {
           console.log(error)
        },
        {
          maximumAge: 0,
          timeout: isIOS ? 100 : 0
        }
      )
    }
 }

Tried both on iOS and Android, with/without maximumAge set to 0, different timeout options. watchPosition has the same issue.

As I see from the source code for iOS and Android it should force to retrieve the location.

However I can't get it work, it shows the cached location and only after some time a new correct one, is there any workaround for that case?

Agontuk commented 2 years ago

Thanks for providing detailed steps, I'll see if I can reproduce this on my end and provide a fix if possible. As a workaround, you can use watchPosition and get the latest location by checking the timestamp.

Agontuk commented 2 years ago

v5.3.0-beta.4 released, check if it solves your issue.