我在这里找不到位置的小问题是我的代码
export default class App extends React.Component{
state = {
location: null
};
findCoordinates = () => {
Geolocation.getCurrentPosition(
position => {
const location = JSON.stringify(position);
this.setState({ location });
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
};
render(){
return (
<View style={styles.container}>
<TouchableOpacity onPress={this.findCoordinates}>
<Text style={styles.welcome}>Find My Coords?</Text>
<Text>Location: {this.state.location}</Text>
</TouchableOpacity>
</View>
);
}
}
未授予他们显示位置的权限
首先获得用户的位置许可。使用此功能
import { Platform, PermissionsAndroid } from 'react-native';
import Geolocation from 'react-native-geolocation-service';
async function requestPermissions() {
if (Platform.OS === 'ios') {
Geolocation.requestAuthorization();
Geolocation.setRNConfiguration({
skipPermissionRequests: false,
authorizationLevel: 'whenInUse',
});
}
if (Platform.OS === 'android') {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
}
}