【发布时间】:2019-07-23 07:45:17
【问题描述】:
我正在开发这个MapScreen 以显示MapView 并设置initialRegion 以获取设备用户的坐标。但不幸的是,它没有按预期工作并且没有授予权限。
这是我目前尝试实现的:
export default function MapScreen() {
const [longitude, setLong] = React.useState();
const [latitude, setLat] = React.useState();
const [location, setLocation] = React.useState();
const [errorMessage, setErrorMessage] = React.useState();
const _getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
setErrorMessage('Permission to access location was denied');
}
let location = await Location.getCurrentPositionAsync({});
let geoLocal = Location.geocodeAsync(location);
setLocation({ location });
setLat(geoLocal.latitude);
setLong(geoLocal.longitude);
};
React.useEffect(()=>{
_getLocationAsync();
});
return (latitude && longitude) ? (
<MapView style={styles.map} initialRegion={{
latitude,
longitude,
latitudeDelta: 1,
longitudeDelta: 1
}} />
) : null // Displays a blank screen if no long or lat found
}
所以我实现的这段代码显示一个空白屏幕,表示没有设置经度或纬度并产生错误:[Unhandled promise rejection: Error: LOCATION permission is required to do this operation.]
【问题讨论】:
标签: reactjs react-native expo react-native-maps