异步函数在android上不返回

时间:2023-01-27 13:28:06

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

我有一个问题,异步函数在android上运行时不返回,而在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:

这行代码在android中不返回,但在ios中返回。在android中,我试着在返回到getlocationasync之前记录位置的值,并记录一个已定义的正确的对象,我想知道为什么它不能返回它:

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

I'm using React Native 0.53

我用的是React Native 0.53

3 个解决方案

#1


1  

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

我认为Android不能定位是有原因的。

I'm using this location option, anh it works well on 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,
    );

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

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

#2


0  

try use it this way

试试用这种方法

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

The location is only tracked when the location track mode is in High accuracy or Battery saver

只有当位置跟踪模式在高准确度或电池保护时,位置才会被跟踪

#3


0  

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

也许这是一个权限问题,看看应用程序是否适用这个位置permisson。

#1


1  

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

我认为Android不能定位是有原因的。

I'm using this location option, anh it works well on 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,
    );

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

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

#2


0  

try use it this way

试试用这种方法

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

The location is only tracked when the location track mode is in High accuracy or Battery saver

只有当位置跟踪模式在高准确度或电池保护时,位置才会被跟踪

#3


0  

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

也许这是一个权限问题,看看应用程序是否适用这个位置permisson。