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

Created on 15 Sep 2020  路  11Comments  路  Source: Agontuk/react-native-geolocation-service

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}, ); };

Most helpful comment

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 ?

All 11 comments

I can confirm that I'm having the same issue in IOS. Setting distanceFilter:10 also doesn't help

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.

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).

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

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 ?

@redwind yes it is happenes in real device both android and ios
did you find any solution? i have the same problem

   "react": "^17.0.1",
    "react-native": "^0.63.4",
"react-native-geolocation-service": "^4.0.0"

this is my code but still i got cached location

async _getCurrentLocation() {
  if (Platform.OS == 'android') {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    );
    if (granted == 'granted') {
      await Geolocation.getCurrentPosition(
        (position) => {
          this.setState(
            {
              LATITUDE: position.coords.latitude,
              LONGITUDE: position.coords.longitude,
            },
            () => {
              if (position.coords.latitude) {
                setTimeout(() => {
                  //call api
                }, 500);
              }
            },
          );
        },
        (errorCallback) => {

        },
        {
          enableHighAccuracy: true,
          timeout: 15000,
          maximumAge: 0,
          forceRequestLocation: true,
        },
      );
    } 
  } else {
    await Geolocation.requestAuthorization();
    await Geolocation.setRNConfiguration({
      skipPermissionRequests: false,
      authorizationLevel: 'whenInUse',
    });

    await Geolocation.getCurrentPosition(
      (position) => {
        this.setState(
          {
            LATITUDE: position.coords.latitude,
            LONGITUDE: position.coords.longitude,
          },
          () => {
            if (position.coords.latitude) {
              setTimeout(() => {
                //call api
              }, 500);
            } else {}
          },
        );
      },
      (errorCallback) => { },
      {
        enableHighAccuracy: true,
        timeout: 15000,
        maximumAge: 0,
      },
    );
  }
}

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

it is happenes in real device both android and ios

I too have the same pronlem.

Having the same problem, react native on android device.

        "react": "16.13.1",
        "react-i18next": "^11.3.3",
        "react-native": "0.62.1",
        "react-native-geolocation-service": "^5.2.0",

usage:

    useEffect(() => {
        Geolocation.getCurrentPosition(
            (position) => {
                const tempInitialCamera = {
                    center: {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                    },
                    pitch: 0,
                    heading: 0,
                    altitude: 0,
                    zoom: configuration.defaultZoom,
                }
                if (!isScrollMap) {
                    setCurrentCamera({camera: tempInitialCamera, zoom: configuration.defaultZoom})
                }
                setCurrentPosition(position.coords)
            },
            (error) => {
                alert(JSON.stringify(error))
                console.log(error)
            },
            {enableHighAccuracy: true, timeout: 10000},
        )
        const watchID = Geolocation.watchPosition(
            (position) => {
                const tempInitialCamera = {
                    center: {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                    },
                    pitch: 0,
                    heading: 0,
                    altitude: 0,
                    zoom: configuration.defaultZoom,
                }
                if (!isScrollMap) {
                    setCurrentCamera({camera: tempInitialCamera, zoom: configuration.defaultZoom})
                }
                setCurrentPosition(position.coords)
                // if (!isScrollMap) {
                //     focusLocation(true)
                // }
            },
            (error) => console.log(error),
            {enableHighAccuracy: true, distanceFilter: 1, interval: 200, fastestInterval: 200},
        )
        return () => {
            Geolocation.clearWatch(watchID)
        }
    }, [])

appears to occur after a set amount of time.

Workaround (Android only):

_Ps.: the steps for IOs devices should be mostly the same, I didn't test it though, as I don't have one_
_Ps2: this was tested in Android API 30, the process remains the same but navigation may vary depending on your device's api version_

  1. As others have mentioned, make sure you have set the desired location in the emulator settings;
  2. Close your app instance;
  3. In the emulated device, navigate to it's "Settings" (usually the cog icon in the app menu);
  4. Navigate to "Apps & notifications", then select your app in the list of apps;
  5. Navigate to "Storage & cache";
  6. Click "Clear storage" as it'll also clear cached data;

That should do it, re-launch your application and your location/coordinates should now be updated. Just to make sure, I added the { maximumAge: 0 } option to my getCurrentPosition() call, but you can change that number to something more reasonable.

I my case the issue found only in simulator (API 30) not in the real phone (android 11).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jaraujo6 picture Jaraujo6  路  6Comments

Agontuk picture Agontuk  路  5Comments

chungweileong94 picture chungweileong94  路  5Comments

Nishit-Chauhan picture Nishit-Chauhan  路  7Comments

Ankur-MoneyKey picture Ankur-MoneyKey  路  11Comments