expo: Location.getCurrentPositionAsync hangs indefinetly

Summary

The Location.getCurrentPositionAsync just hangs indefinetly. I copied the code from the docs (https://docs.expo.dev/versions/latest/sdk/location/) and have a file called MapView.js with the following code:

import React, { useState, useEffect } from 'react';
import { Text, View } from 'react-native';
import * as Location from 'expo-location';

export default function App() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      //Doesn't continue after this line
      setLocation(location);
    })();
  }, []);

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  } else if (location) {
    text = JSON.stringify(location);
  }

  return (
    <View>
      <Text>{text}</Text>
    </View>
  );
}

When I run the code in the app it’s stuck on “Waiting…” and I’m getting no errors in the emulator nor the console. The issue is both on the emulator but also physical devices, I have both Nokia 4.2 and a Samsung A5.

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

bare

What platform(s) does this occur on?

Android

SDK Version (managed workflow only)

No response

Environment

Expo CLI 4.13.0 environment info: System: OS: Linux 5.15 Ubuntu 21.10 21.10 (Impish Indri) Shell: 5.1.8 - /bin/bash Binaries: Node: 16.13.1 - /snap/bin/node Yarn: 1.22.15 - /snap/bin/yarn npm: 8.1.2 - /snap/bin/npm npmPackages: expo: ^43.0.0 => 43.0.0 react: 17.0.1 => 17.0.1 react-native: ^0.64.3 => 0.64.3 react-navigation: ^4.4.4 => 4.4.4 Expo Workflow: bare

Reproducible demo

The code is identical to the code on the docs: https://docs.expo.dev/versions/latest/sdk/location/

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 19 (4 by maintainers)

Most upvoted comments

Having the same issue on Android 13. We are using expo@49.0.13 and expo-location@16.1.0. Usually the freeze happens on the first call of getCurrentPositionAsync() after app start. We came up with a second retry after 10 seconds.

async function getLocationWithRetry(retries = 2): Promise<Location.LocationObject> {
    const timeout = new Promise<never>((_, reject) =>
        setTimeout(() => reject(new Error('Timeout exceeded')), 10000)
    );
    try {
        return await Promise.race([Location.getCurrentPositionAsync({}), timeout]) as Location.LocationObject;
    } catch (error) {
        if (retries > 0) {
            return getLocationWithRetry(retries - 1);
        } else {
            throw error;
        }
    }
}

const userLocation = await getLocationWithRetry();

Its working for us but its not a fine solution.

I am also getting this in a real android device

Please re-open this. This is the reproducible example. It is as minimum as it gets. This code is directly taken from the documentation and is supposed to work as is. I have not just copying in code, this code is in my app.

With the code from the documentation, Location.getCurrentPositionAsync hangs indefinitely.

I am experiencing this as well; either the documentation is incorrect (which is a docs bug) or there’s an issue with the method, but concur that the code in the docs is the minimal reproducible example.

Like @shahzaib-cmyk, this issue started occurring in our app (on real devices running the production app build) only after upgrading the following:

  • React Native from 0.70.6 to 0.72.4
  • expo@47.0.6 to expo@49.0.8
  • expo-location@15.0.1 to expo-location@16.1.0

After we added 10-second timeouts, our error logging has picked the issue up on several versions of Android (10, 12, 13) and at least iOS 16.6.1 (though I think the iOS one might be a red herring).

We’ve started working around it by implementing a fallback to fetch low accuracy location - the low accuracy fallback seems to work very reliably and I think we’ve only seen that fail once, while we’ve seen hundreds of timeouts for high-accuracy or ‘last known location’.

I would expect timeouts to be built into the getCurrentPositionAsync API, with a sensible default, as it doesn’t make sense for it to hang indefinitely.

I’m curious to know what’s caused this to happen only now, and why it wasn’t an issue before. Maybe it was automatically falling back to a low accuracy value previously, and the new behaviour is more correct (albeit unexpected, from my point of view)?

This is still and issue and is impacting real Android devices.

I am still having this issue and I am on Expo managed app SDK 47. getCurrentPositionAsync hangs indefinitely with no response. Event using lowest accuracy for android and ios.

I am encountering the same issue. However, as @bradjones1 said, this only appears to affect Android emulators while it works fine on real devices. I have made a simple snack that reproduces the hanging behavior available at:

https://snack.expo.dev/@andrea-marino/issue_location_getcurrentlocationasync

If you run this on an emulated device (e.g. using the device ID) you can see how the ‘Loading’ text never changes to ‘Done’ and the location is never displayed.

EDIT: I was able to solve this issue. As explained in this reddit post, the location service only works in emulators if you are logged into a google account.