Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANDROID Geolocation not working for ACCESS_COARSE_LOCATION #205

Closed
sabinayakc opened this issue Oct 15, 2022 · 7 comments · Fixed by #207
Closed

ANDROID Geolocation not working for ACCESS_COARSE_LOCATION #205

sabinayakc opened this issue Oct 15, 2022 · 7 comments · Fixed by #207
Labels
bug Something isn't working

Comments

@sabinayakc
Copy link

sabinayakc commented Oct 15, 2022

Environment

System:
OS: macOS 12.6
CPU: (10) arm64 Apple M1 Pro
Memory: 83.22 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.10.0 - /opt/homebrew/bin/node
Yarn: Not Found
npm: 8.19.2 - /opt/homebrew/bin/npm
Watchman: 2022.10.03.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.11.3 - /Users/***/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.4, iOS 16.0, macOS 12.3, tvOS 16.0, watchOS 9.0
Android SDK: Not Found //Local.properties
IDEs:
Android Studio: 2020.3 AI-203.7717.56.2031.7935034
Xcode: 14.0.1/14A400 - /usr/bin/xcodebuild
Languages:
Java: 11.0.16.1 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.1.0 => 18.1.0
react-native: 0.70.2 => 0.70.2
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Platforms

ANDROID

Versions

  • Android: API Level 32
  • iOS: 16
  • react-native-geolocation: latest
  • react-native: 0.70.2
  • react: 18.1.0

Description

Geolocation returning {"PERMISSION_DENIED": 1, "POSITION_UNAVAILABLE": 2, "TIMEOUT": 3, "code": 1, "message": "Location permission was not granted."} when using ACCESS_COARSE_LOCATION.

Works fine with ACCESS_FINE_LOCATION. But if the user changes from precise to approximate it throws the above error.

Reproducible Demo

Pardon my lengthy code here, haven't refactored it.

--ANDROID MANIFEST

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

-- Checking Permission

 if (Platform.OS == 'android') {
      const permissions = [
        PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
        PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
      ];
      let options = {
        enableHighAccuracy: true,
        timeout: 2000,
        maximumAge: 3600000,
      };
      checkMultiple(permissions)
        .then(result => {
          const fine = result[PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION];
          const coarse = result[PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION];
          let req;
          if (fine == RESULTS.GRANTED) {
            req = PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION;
          } else if (coarse == RESULTS.GRANTED) {
            req = PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION;
            options.enableHighAccuracy = false;
          }
          if (req) {
                  this.findCurrentPosition(options);
          } else {
            requestMultiple(permissions)
              .then(status => {
                const per1 = status[PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION];
                const per2 = status[PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION];
                if (per1 == RESULTS.GRANTED || per2 == RESULTS.GRANTED) {
                  options.enableHighAccuracy = per2 == RESULTS.GRANTED ? false: true;
                  this.findCurrentPosition(options);
                } else {
                  this.locationError();
                  this.setState({error: true, loading: false});
                }
              })
              .catch(error => {
                crashlytics().recordError(new Error(error.message));
                this.setState({error: true, loading: false});
              });
          }
        })
        .catch(error => {
          crashlytics().recordError(new Error(error.message));
          this.setState({error: true, loading: false});
        });
  • Geolocation Call
findCurrentPosition = (options = {}) => {
 Geolocation.getCurrentPosition(
      position => {
        this.setState({
          lat: position.coords.latitude,
          lng: position.coords.longitude,
        });
      },
      err => {
        console.log(err). ---> Error happens here.
        crashlytics().recordError(new Error(err.message));
        this.setState({loading: false, error: true});
      },
      options
    );
  };
@sabinayakc sabinayakc added the bug Something isn't working label Oct 15, 2022
@michalchudziak
Copy link
Owner

Thank you for reporting, I'll look onto it soon.

@kamal-j
Copy link

kamal-j commented Oct 19, 2022

I am also facing this issue. Is there a way to get only approximate location by just requesting ACCESS_COARSE_LOCATION permission? Right now this package always requests for ACCESS_FINE_LOCATION permission.

@sabinayakc
Copy link
Author

@kamal-j I have switched to using react-native-geolocation-service since its a time sensitive issue. So far its working. Fingers crossed.

@michalchudziak
Copy link
Owner

@kamal-j @sabinayakc I sent a PR to address this issue, would it be possible to check if it solves it on your end?

@sabinayakc
Copy link
Author

@kamal-j if you could try it and let us know that would be great. unfortunately, i already deployed my code changes with the other package.

@michalchudziak
Copy link
Owner

Fixed in 3.0.3 release

@vauruk
Copy link

vauruk commented Feb 19, 2024

I have the same problem, RN 0.71.15 , RNG 3.1.0, someone knows about this problem! My Android 10 with SDK Engenico?

{"ACTIVITY_NULL": 4, "PERMISSION_DENIED": 1, "POSITION_UNAVAILABLE": 2, "TIMEOUT": 3, "code": 3, "message": "Location request timed out"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants