Location.getCurrentPositionAsync loop infinitly on One Plus 3


#1

Hi,
i tried to execute this Snack Code on One Plus 3 - A3003 with Oxygen 5.0.6 and Android 8.0.0 and its looping infinitely.

Its not working with different SDK too.

Other devices work perfectly.

Any idea? Its bug?

Thanks in advance


#2

Hi @cklinx, you could try the following, it breaks when timeout reached:

const GPS_TIMEOUT = 30 * 1000; // 30 secs

const getLocationAsync = async () => {
  let location;
  if (Platform.OS === 'android' && !Expo.Constants.isDevice) {
    if (__DEV__) {
      console.log('[Location] No location when running on virtual device');
    }
  } else {
    const { status } = await Expo.Permissions.askAsync(Expo.Permissions.LOCATION);
    const { locationServicesEnabled } = await Expo.Location.getProviderStatusAsync();
    if (status === 'granted' && locationServicesEnabled) {
      // resolves or rejects as soon as one
      await Promise.race([
        Expo.Location.getCurrentPositionAsync({
          enableHighAccuracy: true
        }),
        new Promise((resolve, reject) => {
          setTimeout(() => {
            reject(new Error('Promise execution timed out.'));
          }, GPS_TIMEOUT)
        })
      ]).then(result => {
        location = result;
        if (__DEV__) {
          console.log('[Location] Found', location);
        }
      }).catch(error => {
        if (__DEV__) {
          console.log('[Location] Timed out', GPS_TIMEOUT);
        }
      });
    } else {
      if (__DEV__) {
        console.log('[Location] Unavailable', status, locationServicesEnabled);
      }
    }
  }
  return location;
}

#3

Hi @outatime,
Thanks for your answer. Its a good snipped to stop the loop but it doesnt allow me to get che device’s location.

I need it cuz my app use all gps services.

Any help?


closed #4

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