I am making an app which access user location using gps.That is why i added following lines to the AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
And below is my function which fetches location of user
componentWillMount(){
navigator.geolocation.getCurrentPosition(
position => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
}
But this produces error that app doesn't have permission to access location and i need to declare
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
in manifest file.I tried many ways and found a solution i.e to ask for run time permission. So I also added following lines in my code
try {
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Permission',
'message': 'This app needs access to your location',
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the location")
} else {
console.log("Location permission denied")
}
} catch (err) {
console.warn(err)
}
But problem doesn't go away.What should i do ?