簡體   English   中英

異步函數沒有在android上返回

[英]Async function not returning on 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

我認為有一些原因導致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

也許這是一個權限問題,檢查應用程序是否確實應用了職位權限

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM