[Expo Location] - GetCurrentPositionAsync randomly not working on Android

Please provide the following:

  1. SDK Version: 47
  2. Platforms(Android/iOS/web/all): Android Emulator - Pixel 4 - Api 31

Example of my code:

async function getGeolocationData(registeredAddress: AddressDataProps) {
  console.log("getGeolocationData - getCurrentPositionAsync");
  try {
    const location = await Location.getCurrentPositionAsync({
      accuracy: Location.Accuracy.High,
    });

    console.log("getGeolocationData - reverseGeocodeAsync");
    const reverseGeocodeText = await Location.reverseGeocodeAsync({
      latitude: location.coords.latitude,
      longitude: location.coords.longitude,
    });
  } catch (error) {
    console.log("error", error);
  }
}

Sometimes it works flawlessly, but every now and then I get stuck in the getCurrentPositionAsync( ), the other functions and console.logs never get called. Tried with low accuracy, with interval, but nothing seems to help with the inconsistencies.

I’m using this workaround to force a response when the method get stuck:

function handleGetCurrentPosition() {
    return new Promise<LocationObject>((resolve, reject) => {
      let shouldResolve = true;
      let timer: ReturnType<typeof setTimeout>;
      Location.getCurrentPositionAsync().then((res) => {
        if (shouldResolve) {
        //  If we get a response from the API, just return the resolved promise
          clearTimeout(timer);
          resolve(res);
        }
      });
      
      // If the response takes too long, we reject the promise to prevent being stuck forever
      timer = setTimeout(() => {
        shouldResolve = false;
        reject('error');
      }, 5000);
    });
  }

I wait 5 seconds to get some response from the API call and if I didn’t get one, I reject the promise.

Here’s some discussion in the expo github page regarding this issue: https://github.com/expo/expo/issues/10756

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.