Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No location provider available, permission denied in react native

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:

enter image description here

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.

like image 286
Tanmoy Sarker Avatar asked Jan 22 '19 10:01

Tanmoy Sarker


4 Answers

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)
  }
}
like image 55
Andrew Avatar answered Nov 10 '22 05:11

Andrew


Setting High Accuracy like this worked for me:

const location = await Location.getCurrentPositionAsync({
  accuracy:Location.Accuracy.High
});
like image 36
Aayush Patniya Avatar answered Nov 10 '22 05:11

Aayush Patniya


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

like image 37
Yehya Avatar answered Nov 10 '22 04:11

Yehya


please turn on the location on your device and try it out after that.

like image 24
Mafei Avatar answered Nov 10 '22 04:11

Mafei