react-native-geolocation-service: getPosition() returning cached data instead of current location

I am running into an issue where cached geolocation data is being returned instead of the current geolocation data. It is my understanding that the previous geolocation should be wiped once the maximumAge has reached the end of its countdown. Currently, the maximumAge is set to 0, but I tried a few different variations of 1000-10000 to attempt to refresh the cache and remove the previous geolocation, is there a fix out there that I am overlooking for this or does this problem tend to occur in an iOS simulator.

const getPosition = () => {
    if (fetchingLocation === true) {
      return;
    } else {
      setFetchingLocation(true);
    }
    Geolocation.getCurrentPosition(
      (position) => {
        setGeoLocation(position?.coords);
        setFetchingLocation(false);
        console.log('setGeolocation', position?.coords);
      },
      (error) => {
        setGeoLocation(null);
        setFetchingLocation(false);
        console.log('geolocation error');
      },
      {enableHighAccuracy: true, timeout: 0, maximumAge: 0},
    );
  };```

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 23 (5 by maintainers)

Most upvoted comments

Can you check if this happens on a real device too ? Also is it only ios specific ?

I confirm that it happens on real device. When getCurrentPosition, it got cached location without update latest location, but if we open GoogleMap, then getCurrentPosition, it return newest location. So how we can update the location to latest before getCurrentPosition ?

I was having the same issue only on Android devices. I took a look at the code and notice that is using the “getLastLocation” method instead of the “getCurrentLocation” method from the FusedLocationProviderClient.

Please refer to: https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient

We’ve made an internal patch replacing by the new method and that solved the issue.

PS: We had to update the play-services-location to version 17.1.0.

Can you check if this happens on a real device too ? Also is it only ios specific ?

I confirm that it happens on real device. When getCurrentPosition, it got cached location without update latest location, but if we open GoogleMap, then getCurrentPosition, it return newest location. So how we can update the location to latest before getCurrentPosition ?

I have the same question, but I still don’t know how to solve it. Have you solved that?

Having the same problem.

Using getCurrentPosition returns the custom location in the simulator. If I change the location in the simulator between calls it still returns old data (timestamp is the same etc). Would expect a different location after 10 mins.

Putting the app in the background helps, sometimes. Getting called on my callback then, even though I removed the watch (which might be another problem but still it updates the location on entering foreground).

Simulator always provides a fixed static location unless you configure it from Features -> Location menu. There you can change location to use Bicycle Ride/Freeway Drive etc and then simulator will start providing different locations.

But it’s better to test this in a real device.