10

I'm having a problem with an async function not returning when running on android whereas it returns normally when run on iOS.

This is the function:

_getLocationAsync = async () => {
    let {status} = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
        this.setState({
            errorMessage: 'Permission to access location was denied',
        });
    }

    let location = await Location.getCurrentPositionAsync({});

    this.setState({location});
    return location;
};

and I'm using it in another function here:

async fetchMarkers(settings ){
    console.log("fetchMarkers");
    // console.log(settings);
    this.setState({listLoading: true});


    let location = await this._getLocationAsync();
    console.log("location is ", location);
    ....
    ....
}

This line is not returning in android, but it returns in ios. In android I tried logging the value of location just before returning it in _getLocationAsync and it logs a defined and correct object, I'm wondering why it's failing to return it then:

let location = await Location.getCurrentPositionAsync({});

I'm using React Native 0.53

A. Wali
  • 454
  • 3
  • 22
  • 3
    nothing to do with your question, but do you realise if status is not granted, you'll still try to getCurrentPositionAsync? – Jaromanda X May 07 '18 at 22:53
  • good catch! I'll fix that as well – A. Wali May 07 '18 at 23:01
  • 1
    Weird that it returns on iOS device but not Android. However, since you're setting the location state after a successful fetch, why not access it via the state? `await Location.getCurrentPositionAsync({}); let location = this.state.location` – K.Wu May 08 '18 at 01:40
  • @K.Wu my reasoning was that since setState is async, I can’t guarantee that location will be set before i try to access it in the second function, but if I await the result I can guarantee that the value returned is the latest one – A. Wali May 08 '18 at 08:44
  • @A.Wali Then you can also `await this.setState({ location })` in `_getLocationAsync` function – K.Wu May 08 '18 at 12:06
  • @A.Wali can you create an example on https://snack.expo.io? – Roy Wang May 10 '18 at 16:07

2 Answers2

1

I think there are some reasons that Android can't get location.

I'm using this location option, anh it works well on android

// how accuracy should get location be
    const GET_LOCATION_OPTIONS = {
     enableHighAccuracy: false,
     timeout: 20000,
     maximumAge: 1000,
    };

    navigator.geolocation.getCurrentPosition(
      (position) => {
        const location = {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        };
        if (callback) { callback(location); }
      },
      (err) => {
        if (callback) { callback(null); }
      },
      GET_LOCATION_OPTIONS,
    );

Ref: https://facebook.github.io/react-native/docs/geolocation.html

tuledev
  • 10,177
  • 4
  • 29
  • 49
0

maybe it's a permission problem, check whether the app does apply the position permisson