react-native-geolocation icon indicating copy to clipboard operation
react-native-geolocation copied to clipboard

WatchPosition - finding initial position takes forever

Open vgorte opened this issue 4 years ago • 3 comments
trafficstars

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!

vgorte avatar Dec 20 '20 11:12 vgorte

Anything new?

marcibk avatar May 27 '21 14:05 marcibk

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

PetteriHaro avatar Mar 11 '22 08:03 PetteriHaro

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.

johhansantana avatar Oct 05 '23 16:10 johhansantana