getCurrentPositionAsync wait forever

#1

Hi, I’m having a problem trying to get GPS positions.
I’m using the following method, which causes to wait forever for the GPS fix.

Expo.Location.getCurrentPositionAsync({
  enableHighAccuracy: true
})

I’m testing on Android 6.0.1 and my device is a Samsung S5 without SIM, location services enabled, also I’m under roof.

I think the correct behavior should be to timeout if no GPS fix can be obtained after some amount of time.

I’m trying to implement a timeout myself, here’s the code:

import { Platform } from 'react-native';
import Expo from 'expo';
import Settings from '../config/settings'
import Sentry from 'sentry-expo';

export default {
  async get() {
    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, Settings.GPS_TIMEOUT);
          }),
        ]).then(result => {
          location = result;
          if (__DEV__) {
            console.log('[Location] Found', location);
          }
        })
        .catch(error => {
          if (__DEV__) {
            console.log('[Location] Timed out', Settings.GPS_TIMEOUT);
          }
          Sentry.captureMessage('Location timed out', {
            level: 'warning',
          });
        });
      } else {
        if (__DEV__) {
          console.log('[Location] Unavailable', status, locationServicesEnabled);
        }
        Sentry.captureMessage('Location unavailable', {
          level: 'warning',
          extra: {
            permission_status: status,
            location_services: locationServicesEnabled,
          }
        });
      }
    }
    return location;
  }
}
0 Likes

#2

Does your current code work as intended?

0 Likes

#3

Hi Edgar, the code is working as intended, but I think it would be nice that Expo.Location.getCurrentPositionAsync method could take a timeout parameter (or at least handle the case internally) to avoid to wait forever in case the GPS can’t provide a position.

0 Likes

#4

@outatime – there is actually a timeout parameter that is only used on Android (on iOS) it’s ignored. This isn’t documented because of the platform difference. This could work for you?

1 Like

#5

Hi @nikki, sorry for delay … it works for me but im leave the code as is for better handling in both os, thanks !!!

0 Likes

#6

[ANDROID]

Hi guys, is there any way to stop getCurrentPositionAsync while in progress?
I am using the timeout solution (Promise.race) given by OP, but unfortunately if position can’t be obtained it will look for it forever. Navigating away does not help.
The location marker symbol in the status bar will appear and stay there indefinitely (or at least until a fix is obtained).
I also tried the timeout parameter in options but it’s still the same - looking for a fix even though it times out.

Thanks in advance!

0 Likes