异步函数没有在android上返回

时间:2018-05-07 22:18:47

标签: javascript react-native async-await react-native-android

我遇到了在Android上运行时没有返回异步函数的问题,而在iOS上运行时它会正常返回。

这是功能:

_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;
};

我在其他功能中使用它:

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


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

此行未在android中返回,但它以ios返回。在android中我尝试在_getLocationAsync中返回之前记录location的值,并记录一个已定义且正确的对象,我想知道它为什么没有返回它:

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

我使用的是React Native 0.53

2 个答案:

答案 0 :(得分:1)

我认为有些原因导致Android无法获取位置。

我正在使用这个位置选项,是吧 在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,
    );

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

答案 1 :(得分:0)

也许这是一个许可问题, 检查应用程序是否确实应用了职位权限