如何解决:Undefined Promise Rejection (id:0) type error with user location map?
How to solve: Undefined Promise Rejection (id:0) type error with user location map?
My position is in Vietnam but the device doesn't display the permission asking when I navigate to this screen
Here's the error
我尝试使用 react-native-maps 和导入权限,来自 expo-permissions + expo-location 的位置
我是否必须捕获错误以及如何捕获错误?
我也在用我的真实设备
这是代码
import React from 'react';
import {
View,
Text,
StyleSheet,
PermissionsAndroid,
} from 'react-native'
import MapView from 'react-native-maps';
import {Permissions} from 'expo-permissions';
import {Location} from 'expo-location'
class MapViewScreen extends React.Component {
constructor(props){
super(props);
this.state={
region:{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.922,
longitudeDelta: 0.0421,
}
}
this._requestUserLocationPermission();
}
_requestUserLocationPermission = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if(status !== 'granted'){
alert('Please enable your location!')
}
let location = await Location.getCurrentPositionAsunc({enabledHighAccuracy: true})
let region = {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.045,
longitudeDelta: 0.045
}
}
render(){
return(
<View style={styles.container}>
<MapView
initialRegion={this.state.region}
showsCompass={true}
showsUserLocation={true}
rotateEnabled={false}
style={{flex: 1}}
/>
</View>
);
}
}
export default MapViewScreen;
const styles = StyleSheet.create({
container:{
flex: 1,
backgroundColor: '#fff',
}
})
我已经成功解决了这个问题。通过将 react-native-maps 中的 initialRegion
属性更改为 region
,屏幕现在能够在我进入地图屏幕后立即显示我的设备位置。感谢大家的帮助
My position is in Vietnam but the device doesn't display the permission asking when I navigate to this screen
Here's the error 我尝试使用 react-native-maps 和导入权限,来自 expo-permissions + expo-location 的位置 我是否必须捕获错误以及如何捕获错误? 我也在用我的真实设备
这是代码
import React from 'react';
import {
View,
Text,
StyleSheet,
PermissionsAndroid,
} from 'react-native'
import MapView from 'react-native-maps';
import {Permissions} from 'expo-permissions';
import {Location} from 'expo-location'
class MapViewScreen extends React.Component {
constructor(props){
super(props);
this.state={
region:{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.922,
longitudeDelta: 0.0421,
}
}
this._requestUserLocationPermission();
}
_requestUserLocationPermission = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if(status !== 'granted'){
alert('Please enable your location!')
}
let location = await Location.getCurrentPositionAsunc({enabledHighAccuracy: true})
let region = {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.045,
longitudeDelta: 0.045
}
}
render(){
return(
<View style={styles.container}>
<MapView
initialRegion={this.state.region}
showsCompass={true}
showsUserLocation={true}
rotateEnabled={false}
style={{flex: 1}}
/>
</View>
);
}
}
export default MapViewScreen;
const styles = StyleSheet.create({
container:{
flex: 1,
backgroundColor: '#fff',
}
})
我已经成功解决了这个问题。通过将 react-native-maps 中的 initialRegion
属性更改为 region
,屏幕现在能够在我进入地图屏幕后立即显示我的设备位置。感谢大家的帮助