expo-location (React-Native) :Location.getCurrentPositionAsync() 永远不会返回任何内容

Vla*_*sov 9 react-native expo

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

我正在 Android Studio 模拟器(google Pixel 5,api 级别 30)上测试我的代码,我使用的是 expo 版本:~43.0.2 和 expo-location 版本:~13.0.4

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

        console.log("there")
        const userLocation = await Location.getCurrentPositionAsync()
        console.log("here")
Run Code Online (Sandbox Code Playgroud)

事实上,函数 Location.getCurrentPositionAsync() 似乎被锁定

根据以下链接,过去已经知道类似的问题:

React Native Expo-Location 返回位置服务在初始 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

。下面是整个应用程序类:


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


Run Code Online (Sandbox Code Playgroud)

我错过了什么?

kar*_*rel 4

分别在参数中添加accuracy和 ,如下所示:maximumAgeLocation.Accuracy.Highest10000

JavaScript:

const userLocation = await Location.getCurrentPositionAsync({accuracy: Location.Accuracy.Highest, maximumAge: 10000});
Run Code Online (Sandbox Code Playgroud)

解决方案来自How to use getCurrentPositionAsync function in expo-location | 塔布宁

  • 2023年了还是不行……,位置很随意。它有效,但有时它只是在无限等待中被阻塞。现在是 timeInterval(仅限 Android)设置下次 GPS 获取新坐标的时间。最大年龄不可用。对于我的应用程序的用户来说,这非常令人沮丧。我被迫将位置保存在存储中,下次它不起作用时,我可以使其与存储值一起使用(但这意味着用户必须等待很长时间才能获得结果,因为我需要等待这个expo函数响应才能切换到存储) (3认同)
  • 参数“maximumAge”对于“getCurrentPostionAsync”不再有效(“timeout”也不再有效) (3认同)