async location retrieval resolved on second call android react native expo

#1

I’m making an app with the react-native expo, and I have a MapView that is supposed to center on current location initially. On ios, the centering is done automatically (center called in component did mount), but on android, centering only occurs when I call the centerLocation method via a button after the component has been mounted. I’ve been able to narrow down the issue to the fact that getting the current location initially never resolves the first call, but I’m unsure why this happens, and why it works fine the second time I call the method. Permissions are all granted and check out fine. Any help would be appreciated :slight_smile:

My get current location method call: (either way, through Location or through the navigator, doesn’t resolve the first call. It’s worth noting that this works completely fine on iOS)

const getCurrentLocation = async () => {
  const position = Location.getCurrentPositionAsync({ enableHighAccuracy: true });
  return position;
  // return new Promise<Position>((resolve, reject) => {
  //   navigator.geolocation.getCurrentPosition(position => resolve(position), e => reject(e));
  // });
};

Position never resolves when centerLocation is called from componentDidMount. However, when I call it again via the button, it works fine.

centerLocation = () => {
    return getCurrentLocation().then((position) => {
      console.log(position);
      if (position) {
        this.setState({
          region: {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            latitudeDelta: 0.003,
            longitudeDelta: 0.003,
          },
        });
      }
    });
  }

I’m having similar issues for other screens/usages with the location as well. For example, I have a feed that tries to pull content from a certain location, but the initial location call never resolves, and only works once I manually refresh the feed. Only on android though. Thanks so much!

#2

Hey @shekarr,

Thanks for the very detailed post! If it’s not too much to ask, would you mind creating a snack.expo.io that reproduces the behavior you mentioned. Code snippets are great but a Snack really helps the debugging process.

Cheers,

Adam

#3

Hey @adamjnav,
I’ve created the snack here.

However, this snack works fine with the intended behavior, much to my surprise. A problem that I’m having with not only the code snippet above but others as well is that the Location.get… never completes when using async await. After running the snack, I’ve realized it may be a problem with the way components are mounting and dismounting (the screen that I described above is part of a tab navigator, and is not the default screen) and how lazy loading is handled. However, even with the landing screen of my application, from the componentDidMount, I call the expo Location.getCurrent location method, which never resolves. Any idea as to why this may be happening?

closed #4

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