问题描述:
react native 中使用 expo-location 获取位置信息时,按照官方文档执行案例,获取不到位置结果。Location.requestForegroundPermissionsAsync() 执行后可以获取授权状态,但是执行到 Location.getCurrentPositionAsync() 时不会打印结果。
解决方法:
给 Location.getCurrentPositionAsync() 添加参数,如下
let location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
maximumAge: 10000,
});
console.log("Location:", location);
官方文档:
- 安装:
npx expo install expo-location
- 使用:
import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import Device from 'expo-device';
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
useEffect(() => {
(async () => {
if (Platform.OS === 'android' && !Device.isDevice) {
setErrorMsg(
'Oops, this will not work on Snack in an Android Emulator. Try it on your device!'
);
return;
}
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{text}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 20,
},
paragraph: {
fontSize: 18,
textAlign: 'center',
},
});
标签:getCurrentPositionAsync,text,expo,Location,import,location From: https://www.cnblogs.com/lpkshuai/p/17743517.html官方案例中 getCurrentPositionAsync 方法传参为空对象 {} ,这是打印不出结果的原因。
添加参数后便可以获取结果数据。