react-native-geolocation-service icon indicating copy to clipboard operation
react-native-geolocation-service copied to clipboard

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

Open sh-helen opened this issue 4 years ago • 2 comments

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?

sh-helen avatar Aug 31 '21 13:08 sh-helen

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 avatar Sep 04 '21 11:09 Agontuk

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

Agontuk avatar Dec 08 '21 18:12 Agontuk