Determining if geolocation enabled with react native
Asked Answered
I

3

19

Building an app with RN I use the following to receive user's location :

 navigator.geolocation.getCurrentPosition(
  (position) => {
   //do stuff with location
  },
  (error) => {
    //error or locaiton not allowed
  },
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

Which immediately invokes the inquiry for user to accept geolocation permissions. What I'm trying to achieve now is to have an explicit screen before prompting user to click on a button to accept those permissions. In order to get there, I need some way to check if he has accepted those permissions before or not. Caching it in storage would be a bad solution - it might go out of sync if user changes permissions in settings.

How can I check if user has accepted geolocation permissions without triggering the permission alert?

Iyre answered 14/7, 2016 at 10:37 Comment(3)
have u figure it out?Elsworth
@SibeliusSeraphini nope, I have moved back to Swift due to multitude of blockers. I can drop you a Swift snippet if you want to bridge it.Commissioner
maybe this package can help: github.com/yonahforst/react-native-permissionsElsworth
C
14

You can use this library https://github.com/yonahforst/react-native-permissions to check if user has accepted location permission. Like this:

Permissions.getPermissionStatus('location')
  .then(response => {
    this.setState({ locationPermission: response })
  })
Cristencristi answered 12/6, 2017 at 14:4 Comment(3)
Please don't just post some tool or library as an answer. At least demonstrate how it solves the problem in the answer itself.Medardas
^^ I think that was a really helpful answer. It's describing exactly how to solve the problem?!Brassica
Please edit your answer this is not updated anymoreSpoonful
S
9

In v2.x.x of react-native-permissions (https://github.com/react-native-community/react-native-permissions)

import { Platform } from "react-native";
import { PERMISSIONS, request } from "react-native-permissions";
import * as Geolocation from "@react-native-community/geolocation";

try {
request(
    Platform.select({
      android: PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
      ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
    })
  ).then(res => {
    if (res == "granted") {
      Geolocation.getCurrentPosition( // do location staffs);
      Geolocation.watchPosition( // do watch location staffs );
    } else {
      // console.log("Location is not enabled");
    }
  });
} catch (error) {
  console.log("location set error:", error);
}
Santosantonica answered 24/2, 2020 at 5:59 Comment(0)
S
4

Check this library, this would help you to check location is enabled or not.

https://www.npmjs.com/package/react-native-device-info#isLocationEnabled

Note : Above will not check permission enabled on not. It will just check location truned on or not

Smuts answered 7/5, 2022 at 1:39 Comment(1)
Looking for this for hours, thanks.Tree

© 2022 - 2024 — McMap. All rights reserved.