为什么 getCurrentPositionAsync 从不 returns 任何东西?
Why does getCurrentPositionAsync never returns anything?
感谢您访问此 post。
我在使用 React Native 获取用户位置时遇到问题。
我找了好久,也看了别人关于这个功能的post。
看来这个功能有几个问题。
而且 React Native Expo 文档似乎已过时。
https://docs.expo.dev/versions/latest/sdk/location/
我在使用部分使用此处的代码。
import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState("");
useEffect(async () => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
console.log(status);
if (status !== 'granted') {
console.log("denied")
setErrorMsg('Permission to access location was denied');
return;
}
console.log("status", status); // <=== always says "granted"
let location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
timeout: 5000
});
console.log({ location }) // <== never reach here.
setLocation(location);
setErrorMsg('No ERROR');
})();
}, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
return (
<View>
<Text>{text}</Text>
</View>
);
}
我看到 post 中的一个说我必须将 accuracy, maximumAge
的参数传递给 getCurrentPositionAsync
,而不是 expo 文档中提供的 {} empty object
.
但还是不行。由于 getCurrentPositionAsync
挂起,屏幕继续显示 waiting...
。
当然,我相信 android 模拟器设置正确,因为我确实看到了状态日志,它说 "granted"
。
非常感谢您的帮助和阅读我的post。
"expo": "~43.0.2",
"expo-location": "~13.0.4",
"expo-status-bar": "~1.1.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-web": "0.17.1"
您不能将 async 与 useEffect 一起使用,因此不能等待。
你试过相反的方法吗?
const [position, setPosition] = useState(false)
// useEffect
Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
timeout: 5000
})
.then(res => setPosition(res))
.catch(e => console.log(e)
我也有更多机会没有非脂肪箭头 IIFE,你应该试试
(function () {
let { status } = await Location.requestForegroundPermissionsAsync();
console.log(status);
if (status !== 'granted') {
console.log("denied")
setErrorMsg('Permission to access location was denied');
return;
}
console.log("status", status); // <=== always says "granted"
let location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
timeout: 5000
});
console.log({ location }) // <== never reach here.
setLocation(location);
setErrorMsg('No ERROR');
})();
感谢您访问此 post。 我在使用 React Native 获取用户位置时遇到问题。
我找了好久,也看了别人关于这个功能的post。 看来这个功能有几个问题。
而且 React Native Expo 文档似乎已过时。 https://docs.expo.dev/versions/latest/sdk/location/ 我在使用部分使用此处的代码。
import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState("");
useEffect(async () => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
console.log(status);
if (status !== 'granted') {
console.log("denied")
setErrorMsg('Permission to access location was denied');
return;
}
console.log("status", status); // <=== always says "granted"
let location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
timeout: 5000
});
console.log({ location }) // <== never reach here.
setLocation(location);
setErrorMsg('No ERROR');
})();
}, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
return (
<View>
<Text>{text}</Text>
</View>
);
}
我看到 post 中的一个说我必须将 accuracy, maximumAge
的参数传递给 getCurrentPositionAsync
,而不是 expo 文档中提供的 {} empty object
.
但还是不行。由于 getCurrentPositionAsync
挂起,屏幕继续显示 waiting...
。
当然,我相信 android 模拟器设置正确,因为我确实看到了状态日志,它说 "granted"
。
非常感谢您的帮助和阅读我的post。
"expo": "~43.0.2",
"expo-location": "~13.0.4",
"expo-status-bar": "~1.1.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-web": "0.17.1"
您不能将 async 与 useEffect 一起使用,因此不能等待。
你试过相反的方法吗?
const [position, setPosition] = useState(false)
// useEffect
Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
timeout: 5000
})
.then(res => setPosition(res))
.catch(e => console.log(e)
我也有更多机会没有非脂肪箭头 IIFE,你应该试试
(function () {
let { status } = await Location.requestForegroundPermissionsAsync();
console.log(status);
if (status !== 'granted') {
console.log("denied")
setErrorMsg('Permission to access location was denied');
return;
}
console.log("status", status); // <=== always says "granted"
let location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
timeout: 5000
});
console.log({ location }) // <== never reach here.
setLocation(location);
setErrorMsg('No ERROR');
})();