expo-location (React-Native) : Location.getCurrentPositionAsync() 从不 returns 任何东西

expo-location (React-Native) : Location.getCurrentPositionAsync() never returns anything

我正在开发跨平台移动应用程序。

我正在 Android Studio 模拟器(google 像素 5,api 级别 30)上测试我的代码,我使用的是 expo 版本:~43.0.2 和世博会地点版本:~13.0.4

我已经请求了位置权限,它有效。但是当我调用以下代码时,我会记录“那里”但从不记录“这里”:

        console.log("there")
        const userLocation = await Location.getCurrentPositionAsync()
        console.log("here")

确实,函数 Location.getCurrentPositionAsync() 似乎已锁定

过去根据这些链接知道了一个类似的问题:

React Native Expo-Location returns Location service unavailable during initial useEffect https://forums.expo.dev/t/getcurrentpositionasync-doesnt-return-any-value-infinity-loading/23643

但这也是 Expo 文档中的代码:

https://snack.expo.dev/@charliecruzan/expo-map-and-location-example

。下面是整个应用 class :


import { StatusBar } from 'expo-status-bar';
import React from 'react';
import {Text, TextInput, Pressable, View, Alert} from 'react-native';
import * as Location from "expo-location"


export default class App extends React.Component{
    state = {
        errorMessage: "",
        location: {}
    }

    getAddress(){
        return this.state.address
    }

    _getLocation = async ()=>{
        const {status} = await Location.requestForegroundPermissionsAsync();
        if (status !== "granted"){
            console.log("PERMISSION LACK!")
            this.setState({
                errorMessage:"PERMISSION NOT GRANTED"
            });
        }
        console.log("there")
        const userLocation = await Location.getCurrentPositionAsync();
        console.log("here")
        console.log(JSON.stringify(userLocation))
        this.setState({
            location: userLocation
        })
    }

    render(){
        this._getLocation()

        return (
            <View>
                 <Text>Salut</Text>
            </View>
        );
    }
}


我错过了什么?

Location.Accuracy.Highest10000参数中分别添加accuracymaximumAge如下图:

JavaScript:

const userLocation = await Location.getCurrentPositionAsync({accuracy: Location.Accuracy.Highest, maximumAge: 10000});

解决方案来自How to use getCurrentPositionAsync 世博会的功能 |塔宁.

如此 reddit post 中所述,如果您登录 google 帐户,定位服务仅在模拟器中可用。 https://www.reddit.com/r/reactnative/comments/s2ja6v/do_location_services_work_in_expo_for_android/