getPosition() returning cached data instead of current location
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:9
- Comments:23 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.