Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.61k stars 291 forks source link

Android Unable to retrieve location after first retrieve location #183

Closed rikopramayudi closed 3 years ago

rikopramayudi commented 4 years ago

after first retrieve there's error log '2 Unable to retrieve location'

[Mon Jun 08 2020 20:17:59.373]  LOG      Home.js line 103 position
[Mon Jun 08 2020 20:17:59.408]  LOG      Home.js line 105 latitude: -0.4936573 | longitude: 117.1535063
[Mon Jun 08 2020 20:17:59.552]  LOG      Home.js line 135 response
[Mon Jun 08 2020 20:18:30.584]  LOG      2 Unable to retrieve location
[Mon Jun 08 2020 20:19:38.203]  LOG      2 Unable to retrieve location
[Mon Jun 08 2020 20:20:21.852]  LOG      2 Unable to retrieve location

and if i put googlePlayServicesVersion = "17.0.0" that make comflict with react-native-camera so i not put that on build.gradle, is there any sugestion?

react-native-cli: 2.0.1 react-native: 0.62.2

swaxtic commented 4 years ago

same problem here

allanloji commented 4 years ago

Have you tried putting playServicesVersion = "17.0.0"? That works for me. react-native-geolocation-service: 5.0.0 react-native: 0.62.2 react-native-camera: 3.23.1

swaxtic commented 4 years ago

@allanloji i have, but still "Unable to retrieve location"

DeVoresyah commented 4 years ago

I got this on iOS only, works fine on Android

ryanm commented 4 years ago

@DeVoresyah I ran into this on iOS too. I was using NSLocationWhenInUseUsageDescription in Info.plist because I don't want the "Always" option. The only thing that "solved" it for me was to also enable NSLocationAlwaysAndWhenInUseUsageDescription in Info.plist. Somehow that fixed the "Unable to retrieve location" error and now "While Using the App" works. The downside is, of course, the "Always" option is visible (in Settings > [App Name] > Location) which I had hoped to avoid.

Agontuk commented 4 years ago

For android, are you facing this in real device or emulator ? If emulator, which one and which android version ? For iOS, I can't verify this cause I don't have ios devices.

Saad-Bashar commented 4 years ago

Has anyone solved this issue? I am also facing the same issue. Geolocation.getCurrentPosition() only works the first time. If I close the app and come back again it does not detect anything. No error message, nothing. the function does not even trigger.

tomoima525 commented 4 years ago

@Saad-Bashar We are having the same issue on Android devices with react-native-geolocation-service 3.1.0. I found that the second try will fail without any error but the third requests and after succeed. So here's my workaround to this issue.


defaultOptions = {...};

// Async function to fetch location from react-native-geolocation-service
async function getCurrentPosition(options?: Object) {
  return new Promise((resolve, reject) => {
    let opt = defaultOptions;
    if (options) {
      opt = { ...defaultOptions, ...options };
    }
    Geolocation.getCurrentPosition(
      data => {
        resolve(data);
      },
      error => {
        const errorMessage = `getCurrentPosition error: ${JSON.stringify(error)}`;
        const errorObj = new Error(errorMessage);
        notifyErrorParams(errorObj, { errorMessage });
        reject(errorMessage);
      },
      opt,
    );
  });
}

// Retry when the request fails with timeout
async function getPosition() {
  let didTimeOut = false;
  return new Promise((resolve, reject) => {
    const timeout = setTimeout(() => {
      didTimeOut = true;
      getCurrentPosition().then(r => resolve(r));
    }, 2000);

    getCurrentPosition()
      .then(r => {
        clearTimeout(timeout);
        if (!didTimeOut) {
          resolve(r);
        }
      })
      .catch(e => {
        if (didTimeOut) {
          return;
        }
        reject(e);
      });
  });
}

I'll try and see if this issue resolved on 5.0.0

Saad-Bashar commented 4 years ago

Wow, this seems to be a super hack :/

Agontuk commented 3 years ago

Please try the new version and see if this issue exists. If so, feel free to open new issue.

paddy57 commented 3 years ago

I'm getting the same error, Unable to retrieve location, error code - 2

I'm using "react": "16.8.6", "react-native": "0.60.5", "react-native-geolocation-service": "^5.3.0-beta.1", iOS 14.4 and Xcode - 12.4

permission on app is ON (set to while using )

anhdevit commented 3 years ago

Check set location in debug of simulator

Screen Shot 2021-05-16 at 16 08 53
hamam99 commented 3 years ago

Try to add timeout in config