'React native app : Not authorized to use location services

I'm currently working on a project that uses the geolocation service of my phone. I currently have a prolem with this service, it says that geolocation is not authorized.

I've tried to look on the internet and a few people had the same problem but I didn't manage to fix it...

  componentDidMount() {
    const { coordinate } = this.state;

    this.requestCameraPermission();

    this.watchID = navigator.geolocation.watchPosition(
      position => {
        const { routeCoordinates, distanceTravelled } = this.state;
        const { latitude, longitude } = position.coords;

        const newCoordinate = {
          latitude,
          longitude
        };
        console.log({ newCoordinate });

        coordinate.timing(newCoordinate).start();

        this.setState({
          latitude,
          longitude,
          routeCoordinates: routeCoordinates.concat([newCoordinate]),
          distanceTravelled:
            distanceTravelled + this.calcDistance(newCoordinate),
          prevLatLng: newCoordinate
        });
      },
      error => console.log(error),
      {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 1000,
        distanceFilter: 10
      }
    );
  }

Instead of appearing on my current spot, i'm in San Fransisco (which is the default location on maps). The function navigator.geolocation.watchPosition gets the error : "code": "E_LOCATION_UNAUTHORIZED", "message": "Not authorized to use location services", "watchId": 1,

My phone is a Samsung S9 and the location service is enabled... So I'm really curious about the problem I have right now.



Solution 1:[1]

Thank you for your answer, I managed to fix my problem, apparently expo wasn't allowed to use my location for some reason, so I just forced it with :

  Location.requestPermissionsAsync();

Solution 2:[2]

import * as Permissions from 'expo-permissions';

await Permissions.askAsync(Permissions.LOCATION);

Alternative method if requestPermissionsAsync(); is unable to work.

Solution 3:[3]

If you are using Expo client on your mobile then please make sure you have enabled the location permission.

If you already given the permission, then check your AndroidManifest.xml for <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> for these locations.

Solution 4:[4]

I've had the case when iOS worked fine without asking explicitly in the .ts code for any permissions. However, Android showed that warning Not authorized to use location services. The Permissions have slightly different syntax now, here is an example with Location.

if (Platform.OS !== "web") {
  const { status } = await Location.requestForegroundPermissionsAsync();
  
  if (status !== "granted") {
    Alert.alert(
      "Insufficient permissions!",
      "Sorry, we need location permissions to make this work!",
      [{ text: "Okay" }]
    );
    return;
  }
}

(in general) Don't mess up anything in the AndroidManifest.xml, like <uses-permission android:name=..., because whatever Expo needs will generate automatically there. That's the case for Location, citing official docs:

"Some Expo and React Native modules include permissions by default. If you use expo-location, for example, both the ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION are implied and added to your app's permissions automatically."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anthony Nguyen cong trang
Solution 2 Jonas
Solution 3 General Grievance
Solution 4 Daniel Danielecki