I have an app which requires the user to allow the location. The device I'm testing the app shows no problem to that. But whenever I use another device it shows the following error:
Here's the code i have:
componentDidMount() {
navigator.geolocation.getCurrentPosition((position) => {
let lat = parseFloat(position.coords.latitude)
let long = parseFloat(position.coords.longitude)
let initialRegion = {
latitude: lat,
longitude: long,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
this.setState({ initialPosition: initialRegion })
this.setState({ markerPosition: initialRegion })
},
(error) => alert(JSON.stringify(error)),
{ enableHighAccuracy: true, timeout: 2000, maximumAge: 3600000 }
)
}
I have tried { enableHighAccuracy: false, timeout: 2000 }
, but it shows the same error.
From the code you’ve given you haven’t shown that you are requesting authorisation to access the permissions.
You should be calling navigator.geolocation.requestAuthorization();
before you start requesting locations.
You should also make sure you have set the required permissions in the AndroidManifest.xml
and the Info.plist
You can check the documentation here https://facebook.github.io/react-native/docs/geolocation
Android API >= 23 Requires an additional step to check for, and request the ACCESS_FINE_LOCATION permission using the PermissionsAndroid API. Failure to do so may result in a hard crash.
You could do something like this.
import { PermissionsAndroid } from 'react-native';
async function requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Permission',
'message': 'This App needs access to your location ' +
'so we can know where you are.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use locations ")
} else {
console.log("Location permission denied")
}
} catch (err) {
console.warn(err)
}
}
Setting High Accuracy like this worked for me:
const location = await Location.getCurrentPositionAsync({
accuracy:Location.Accuracy.High
});
I also ran through this error. Took me a bit to find out that device Location method needs to be kept as High Accuracy in device settings.
Thankyou
please turn on the location on your device and try it out after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With