Reputation: 904
So I change the geolocation, I use Geolocation from 'react-native-geolocation-service'
now. So my problem now is not sure if my codes of PermissionsAndroid is correct because there is no Permission of location dialog is showing. Everytime I press the button "Location permission is not granted" is showing up in the console.log so it means permission is not granted. As I say there is not Permission dialog is showing up.
I want you guys check my codes if where are my mistakes. These are my codes:
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!
Upvotes: 5
Views: 8857
Reputation: 656
check whether your app already has the permission, if permission is granted it won't show any popup
Upvotes: 1
Reputation: 13608
Your userdefined Text to request Permission you've added:
title: 'Location Permission',
message: 'You must to accept this to make it work.'
won't show for the first Permission-Reuquest. But if the user click "deny" and you request again for the Permission in your app, then your Text appears.
I've also struggled at this, but the docs describe that behavior:
If a user has previously turned off a permission that you prompt for, the OS will advise your app to show a rationale for needing the permission. The optional rationale argument will show a dialog prompt only if necessary - otherwise the normal permission prompt will appear.
So if you wan't to have a notice for the User what for do you need the permission, it seems you need to use an alert in front of first Permission-Request.
If anybody has another way, I would be glad to hear.
Upvotes: 2
Reputation: 159
did you add permission in your AndroidManifest file? like what the documentation said.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Upvotes: 6