question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getPosition() returning cached data instead of current location

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
redwindcommented, Mar 12, 2021

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 ?

2reactions
ricardofadinicommented, Nov 8, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react native geolocation for android returning cached position
The problem I am facing is that android is returning the cached position. I have removed the maximumAge parameter in my code but...
Read more >
Geolocation.getCurrentPosition() - Web APIs | MDN
A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return.
Read more >
Geolocation API - W3C
This section is non-normative. By default, the API always attempts to return a cached position so long as it has a previously acquired...
Read more >
Finding your position with Geolocation | HTML5 Doctor
The accuracy of the initial result from getCurrentPosition() or watchPostion() depend highly on the WiFi networks in the area. In my experience ...
Read more >
Geolocation - React Native
maximumAge (ms) - Is a positive value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found