我正在开发一个跨平台的移动应用程序。
我正在 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)
我错过了什么?
分别在参数中添加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 | 塔布宁。
| 归档时间: |
|
| 查看次数: |
8839 次 |
| 最近记录: |