所以我更改了地理位置,我现在使用 'react-native-geolocation-service'
中的地理位置。所以我现在的问题是不确定我的 PermissionsAndroid 代码是否正确,因为没有显示位置权限对话框。每次我按下按钮时,console.log 中都会显示“未授予位置权限”,因此这意味着未授予权限。正如我所说,没有显示“权限”对话框。
我希望你们检查我的代码是否有错误。 这些是我的代码:
hasLocationPermission = () => {
try{
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission',
message: 'You must to accept this to make it work.'
}
)
if(granted === PermissionsAndroid.RESULTS.GRANTED){
console.log('Location permission accepted.')
}else{
console.log("Location permission denied")
}
} catch (err) {
console.warn(err)
}
}
getUserLocationHandler = () => {
if(this.hasLocationPermission){
Geolocation.getCurrentPosition(
position => {
this.setState({
userLocation: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.0622,
longitudeDelta: 0.0421,
},
});
},
err => console.log(err),
{
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000
}
);
}
}
render() {
return (
<View style={styles.container}>
<FetchLocation onGetLocation={this.getUserLocationHandler}/>
<UsersMap userLocation={this.state.userLocation}/>
</View>
);
}
"FetchLocation" is the Button and the "UsersMap" is the map.
If you guys know how to show up the Permission Dialog before the app open like the Grab app, that would be better. Thank you!
If you want to see something else in my codes or in my build.gradles, feel free to ask. Thank you!
最佳答案
您是否在 AndroidManifest 文件中添加了权限?就像documentation说。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
关于javascript - PermissionsAndroid 未显示在 React Native 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52848830/