react-native-geolocation
react-native-geolocation copied to clipboard
WatchPosition - finding initial position takes forever
It takes quite long for watchPosition to return an initial position. I wonder if I am not following best practices for this matter.
I have the following useEffect which sets up the watchPosition:
useEffect(() => {
requestGeolocationPermission()
.then(r => {
if (r) {
Geolocation.watchPosition(
watchPosition,
watchPositionError,
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 500,
distanceFilter: 1,
},
);
}
});
return () => {
Geolocation.stopObserving();
};
}, []);
It often takes 10 - 30 seconds for the watchPosition success callback to receive the first location. After that, it works like it is supposed to. Am I not utilizing the watchPosition function properly? Any advice would be greatly appreciated!
Anything new?
Ran into the same issue where calling just watchPosition took forever on Android. I was able to work around by calling getCurrentPosition first and then starting watchPosition. Example below:
if (isAndroid) {
Geolocation.getCurrentPosition(
(p) => {
setLocationResponse(p);
},
(e) => {
setGeoLocationError(e);
},
);
}
const watchId = Geolocation.watchPosition(
(position) => {
setLocationResponse(position);
},
(e) => {
setGeoLocationError(e);
},
{ distanceFilter: 1, enableHighAccuracy: true, timeout: 10000 },
);
setWatchId(watchId);
I assume also calling watchPosition in the getCurrentPosition success handler works as well
I tried what @PetteriHaro did but nothing seems to work. Neither getCurrentPosition nor watchPosition seem to act fast enough that people start treating it as a bug. And it's worse if the device is offline.
I also noticed that this just happens in some devices, not all of them (only speaking Android side here). My Pixel 7 works in like less than 5 seconds while Samsung devices take forever.