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 - Turn on Location Service popup not coming #108

Closed
Android773 opened this issue Jul 31, 2019 · 36 comments
Closed

Android - Turn on Location Service popup not coming #108

Android773 opened this issue Jul 31, 2019 · 36 comments

Comments

@Android773
Copy link

Hi,

My setup is

"dependencies": {
    "@react-native-community/geolocation": "^1.4.2",
    "react": "16.8.6",
    "react-native": "0.60.4",
    "react-native-geolocation-service": "^3.0.0",
    "react-native-gesture-handler": "^1.3.0",
    "react-navigation": "^3.11.1"
},

And code is:

Geolocation.getCurrentPosition(
    (position) => {
        console.warn("Position " + position.coords.latitude + " " + position.coords.longitude);
    },
    (error) => {
        // See error code charts below.
        console.warn("Error " + error.code, error.message);
    },
    { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000, }
);

Scenario:

  1. I have already added code for PermissionsAndroid and after getting the permission I am hitting the above mentioned code.
  2. Android -

When the Location in top drawer menu is ON then I am getting location.

But when Location is off, it is not showing the following popup to ask user to enable the location service:

image

Instead, it directly goes to (error) and says, "error.code = 2, No location provider available."

I think in earlier versions of library this used to work. But facing this problem in 3.0.0.
3. In iOS it is working fine. It is showing popup to ask for Permission for Location.

Any help on this would be appreciated.

P.S. @Agontuk Thanks for this library. We really appreciate your efforts.

@Agontuk
Copy link
Owner

Agontuk commented Aug 2, 2019

You have both @react-native-community/geolocation & react-native-geolocation-service installed. Make sure you're importing the correct library in your component. I tried 3.0.0 in my example project and the dialog is showing up correctly.

On another note, remove all android related setup described in README file. It is not needed in RN60 as it'll integrate the module automatically when installed.

@Android773
Copy link
Author

@Agontuk I understand these things.

  1. Yes, my import is correct.

import Geolocation from 'react-native-geolocation-service';

I have added @react-native-community/geolocation module because when I ran it on iOS, it was asking to link this module to app, I searched about it and got to know that in RN 0.60 geolocation is removed as built in module and now is available as a separate module. And library had internal dependency on that so I had to install that as well.

a. After installing @react-native-community/geolocation Location is working fine on iOS.
b. On Android it wasn't working even when I had not added @react-native-community/geolocation

  1. And yes, RS60 links the module automatically so I have not performed any manual steps mentioned in README file.

As you said it I just created a new empty project and integrated this library, but unfortunately the result is same. I am not getting popup.

"dependencies": {
    "react": "16.8.6",
    "react-native": "0.60.4",
    "react-native-geolocation-service": "^3.0.0"
},
import Geolocation from 'react-native-geolocation-service';
---
componentDidMount() {
        // Instead of navigator.geolocation, just use Geolocation.
        Geolocation.getCurrentPosition(
            (position) => {
                console.warn(position);
            },
            (error) => {
                // See error code charts below.
                console.warn(error.code, error.message);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
        );
}

I have mentioned the ACCESS_FINE_LOCATION in Manifest file and enabled the permission for app from settings.

@oakis
Copy link
Contributor

oakis commented Aug 5, 2019

What Android version are you using on your device when this happens?

@Android773
Copy link
Author

@oakis I have tested on two devices with Android 8.0 and Android 7.0

@oakis
Copy link
Contributor

oakis commented Aug 5, 2019

@Android773 Can you try with Android 9? Might be a similar issue a bunch of others have been experiencing.

@Android773
Copy link
Author

@oakis I tried with Android 9 as well, but it is not asking to turn Location Service on when Location is off.

@vjsingh96
Copy link

have you solved this issue?

@prasan2421
Copy link

prasan2421 commented Aug 7, 2019

Getting the same issue in Android version 9, package version 3.0.0 and RN version 0.60.4. No solutions so far.

@vjsingh96
Copy link

vjsingh96 commented Aug 7, 2019

@prasan2421 Which React Native version you are using?

@tobikl1
Copy link

tobikl1 commented Aug 7, 2019

Same for us with GPS Sensor deactivated on Android:

"react-native": "0.60.4",
"@react-native-community/geolocation": "^1.4.2",
"react-native-geolocation-service": "^3.0.0",

Tested on an Android 9 Device:

Geolocation.getCurrentPosition(
  pos => {
    // does not go here
  },
  err => {
    // ends up here
    // err.code = 2
    // err.message: No location provider available
  },
  {
    enableHighAccuracy: true,
    timeout: 20000,
    maximumAge: 120000,
    showLocationDialog: true,
    forceRequestLocation: true
  }
);

on iOS it works (when deactivating the locationService in device settings it reports back as no permission for app though)

@prasan2421
Copy link

@prasan2421 Which React Native version you are using?

v 0.60.4

@vjsingh96
Copy link

Remove @react-native-community/geolocation": "^1.4.2", plugin and try

@tobikl1
Copy link

tobikl1 commented Aug 8, 2019

as stated before, that is a dependency for the plugin to work with iOS.

Is it possible this is an issue with the "@react-native-community/geolocation" plugin instead which is causing this one to show this behaviour?

@prasan2421
Copy link

Remove @react-native-community/geolocation": "^1.4.2", plugin and try

i havent kept this plugin yet i have the issue, but im using @react-native-community/netinfo.
Did you solve this issue by removing @react-native-community/geolocation?

@Agontuk
Copy link
Owner

Agontuk commented Aug 9, 2019

Guys, can anyone give me a sample project for this issue ? I am running an example with RN60 in my mobile and location popup is working fine for me. Also please provide any steps necessary to reproduce this.

@prasan2421
Copy link

Guys, can anyone give me a sample project for this issue ? I am running an example with RN60 in my mobile and location popup is working fine for me. Also please provide any steps necessary to reproduce this.

https://github.com/prasan2421/location-test-project
Above is the link to my repo.
I've created a fresh RN project of version 0.60.4 and installed react-native-geolocation-service version 3.0.0. i have not kept any other package but the issue is still there. It looks like a bug of new react native itself.

@AliMSbaee
Copy link

I have the same issue.
Android 9
React Native: v0.60.4
react-native-geolocation-service: v 3.0.0

@Agontuk
Copy link
Owner

Agontuk commented Aug 17, 2019

It seems like PR #68 introduced an additional check to determine whether location providers are enabled or not. If no providers are available, it'll immediately return error response instead of showing popup.

This check is needed because it enables us to request location even when location setting is set to Device Only.

We may need to find an alternate solution. What are your thoughts guys ?

@prasan2421
Copy link

so will this issue be fixed? i was also wondering how few others above managed to make this working.

@Agontuk
Copy link
Owner

Agontuk commented Aug 23, 2019

Guys, let me know if this flow makes sense. Looping in @cladjules since he implemented the forceRequestLocation feature.

  1. If location settings are ok, it'll invoke success callback with location
  2. If not ok and showLocationDialog is false, it'll invoke error callback with codeSETTINGS_NOT_SATISFIED.
  3. If not ok and showLocationDialog is true, it'll show location popup to improve location accuracy.
  4. If user clicks OK, it'll update the location settings and invoke success callback with location.
  5. If user clicks NO THANKS and forceRequestLocation is false, it'll invoke error callback with codeSETTINGS_NOT_SATISFIED.
  6. If user clicks NO THANKS and forceRequestLocation is true but location provider is not enabled, it'll invoke error callback with code POSITION_UNAVAILABLE.
  7. If user clicks NO THANKS and forceRequestLocation is true and location provider is enabled, it'll invoke success callback with location if it's possible.

@oakis
Copy link
Contributor

oakis commented Aug 23, 2019

@Agontuk Sounds good to me.

@duytq94
Copy link

duytq94 commented Aug 28, 2019

I have the same problem (of course in Android)

  • With RN < 0.60.0 and this library < 3.0.0. After grant permission, it will show dialog request to turn on GPS and then everything fine.

  • With RN >= 0.60.0 and this library >= 3.0.0. After grant permission, it won't show dialog request to turn on GPS and throw error message: 'No location provider available.', code: 2. So I have to turn on GPS manually on the device.

  • And about @react-native-community/geolocation, we have to install it for iOS working, but I think the issue won't relative to this geolocation since when I haven't installed it, the issue still happens on Android.

Did anyone get any solution? Thanks.

@Agontuk
Copy link
Owner

Agontuk commented Aug 30, 2019

Fixed in 3.1.0

@Agontuk Agontuk closed this as completed Aug 30, 2019
@duytq94
Copy link

duytq94 commented Aug 30, 2019

Everything's fine, thanks to @Agontuk

@Android773
Copy link
Author

@Agontuk Great!

It is working fine after updating to 3.1.0.

Thanks a lot.

@gvankeerberghen
Copy link

Guys, let me know if this flow makes sense. Looping in @cladjules since he implemented the forceRequestLocation feature.

  1. If location settings are ok, it'll invoke success callback with location
  2. If not ok and showLocationDialog is false, it'll invoke error callback with codeSETTINGS_NOT_SATISFIED.
  3. If not ok and showLocationDialog is true, it'll show location popup to improve location accuracy.
  4. If user clicks OK, it'll update the location settings and invoke success callback with location.
  5. If user clicks NO THANKS and forceRequestLocation is false, it'll invoke error callback with codeSETTINGS_NOT_SATISFIED.
  6. If user clicks NO THANKS and forceRequestLocation is true but location provider is not enabled, it'll invoke error callback with code POSITION_UNAVAILABLE.
  7. If user clicks NO THANKS and forceRequestLocation is true and location provider is enabled, it'll invoke success callback with location if it's possible.

@Agontuk, @cladjules : this works as intended, in my case I have the options

{
  enableHighAccuracy: true,
  timeout: 15000,
  maximumAge: 1000 * 60 * 3,
  forceRequestLocation: true,
  showLocationDialog: true,
}

and I see the dialogue about improving location by activating Location Services, I can answer 'No, thanks' and I still get a location! Thanks for the work btw.

This is great because I do have a few users for whom activating the Google Location Services isn't an option but many others for whom it's fine.

However, for users without these Google Location Services, it seems the dialogue will show every single time my app needs to grab the location. Which might quickly become annoying for them since the main interaction in my app makes the app ask for the current location.

Is that the way you guys deliver this or did I miss some way to be more user-friendly? By only showing that dialogue once and remembering to force without dialogue afterwards for instance?

@fabripeco
Copy link

fabripeco commented Feb 10, 2020

Hi!

Using react-native-geolocation-service@4.0.0 (react-native 0.61.3 Android 6), calling

Geolocation.watchPosition(successCb, errorCb, {
    enableHighAccuracy: true,
    interval: 20000,
    showLocationDialog: true,
    useSignificantChanges: true,
})

the location dialog doesn't appear if the GPS is off and it results in error callback {"code": 2, "message": "No location provider available."}.
The dialog appears calling getCurrentPosition instead.
Is it the right behaviour? Should I call getCurrentPosition and afterwards watchPosition?

Thank you!

@oakis
Copy link
Contributor

oakis commented Feb 11, 2020

@fabripeco Check for permissions first I guess https://facebook.github.io/react-native/docs/permissionsandroid

@fabripeco
Copy link

fabripeco commented Feb 11, 2020

No, I get the necessary permissions before calling watchPosition. Actually when I call getCurrentPosition, the location dialog is shown and GPS is turned on when I click Ok. The problem raises only with watchPosition and GPS turned off. Something wrong with my code? Ideas?

@millisande
Copy link

I get this issue using Android Studio Emulator of a Pixel 2 API 30 using Android 10.0+ and having chosen in my location settings to "Ask every time". It always triggers the error callback. Is this specific to using an emulator?

@betoharres
Copy link

betoharres commented Aug 7, 2020

I get this issue using Android Studio Emulator of a Pixel 2 API 30 using Android 10.0+ and having chosen in my location settings to "Ask every time". It always triggers the error callback. Is this specific to using an emulator?

I'm also having this issue on Android 10.0 API 29 on a Nexus S emulator. And even trying to set force to true is not making the pop up to show.
Any idea?
$ react-native info

System:
    OS: Linux 5.1 Arch Linux undefined
    CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
    Memory: 1.20 GB / 15.59 GB
    Shell: 5.7.1 - /usr/bin/zsh
  Binaries:
    Node: 12.10.0 - /usr/bin/node
    Yarn: 1.17.3 - /usr/bin/yarn
    npm: 6.11.3 - /usr/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 16, 26, 28, 29
      Build Tools: 26.0.3, 28.0.3, 29.0.0, 30.0.1
      System Images: android-16 | Intel x86 Atom, android-16 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom
      Android NDK: 21.0.6113669
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.1 => 0.61.1
  npmGlobalPackages:
    react-native-cli: 2.0.1

@Agontuk
Copy link
Owner

Agontuk commented Aug 11, 2020

I get this issue using Android Studio Emulator of a Pixel 2 API 30 using Android 10.0+ and having chosen in my location settings to "Ask every time". It always triggers the error callback. Is this specific to using an emulator?

Unable to reproduce this, using the same emulator. Can you provide reproducible steps ?

Also I don't think location service popup will be shown in android 10+ devices, because location mode is no longer available in those api versions.

@Deepak13312
Copy link

    **React Native iOS**
***As you can see my code that the location popup permission is not showing in iOS, while I am using "react-native-geolocatio-service". I am struggling with, please help me, it would be more appreciable.****
    
        onPressLocation = () => {
            var that = this;
            async function requestLocationPermission() {
              const options = {
                maxWidth: 500,
                maxHeight: 500,
                storageOptions: {
                  skipBackup: true,
                },
              };
              const granted = await PermissionsAndroid.request(
                PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
                {
                  title: 'ThoughtBulb App location Permission',
                  message: 'ThoughtBulb App needs access to your location ',
                },
              );
              if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                Geolocation.getCurrentPosition(
                  position => {
                    const currentLongitude = JSON.stringify(position.coords.longitude);
                    const currentLatitude = JSON.stringify(position.coords.latitude);
                    that.setState({
                      currentLatitude: currentLatitude,
                      currentLongitude: currentLongitude,
                    });
                  },
                  error => {
                    console.log(error);
                  },
                  {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
                );
              } else {
                alert('Location Permission is denied.');
              }
            }
            if (Platform.OS === 'android') {
              requestLocationPermission();
            } else {
    *****Here I need to get iOS location permission popup before execute below funtion.
I think this issue raised after updated the Xcode from 10 to 12***
              Geolocation.getCurrentPosition(
                position => {
                  const currentLongitude = JSON.stringify(position.coords.longitude);
                  const currentLatitude = JSON.stringify(position.coords.latitude);
                  that.setState({
                    currentLatitude: currentLatitude,
                    currentLongitude: currentLongitude,
                  });
                },
                error => {
                  console.log(error);
                },
                {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
              );
            }
          };

@ivanovyoan
Copy link

@Deepak13312 also have the same problem, haven't found a solution to it.

@reggi49
Copy link

reggi49 commented Jul 1, 2021

@Deepak13312 also have the same problem, haven't found a solution to it.

This may be of little help, first make sure no library location other than react-native-geolocation-service. then if there is no use react hook. you might try this method below.

hasLocationPermission = async () => {
  const hasPermission = await PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);

if (hasPermission === PermissionsAndroid.RESULTS.GRANTED) {
  console.log('lagi req permission');
  return true;
}

const status = await PermissionsAndroid.check(
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);

if (status) {
  console.log('permission diberikan');
  return true;
}

if (status === PermissionsAndroid.RESULTS.DENIED) {
  ToastAndroid.show(
    'Location permission denied by user.',
    ToastAndroid.LONG,
  );
} else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
  ToastAndroid.show(
    'Location permission revoked by user.',
    ToastAndroid.LONG,
  );
}
return false;
};
async componentDidMount() {
  if (await this.hasLocationPermission()) {
  Geolocation.getCurrentPosition(
      position => {
        console.log('posisi ditemukan',position.coords.latitude, position.coords.longitude);
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
        this.goToRegion(position.coords.latitude, position.coords.longitude);
      },
      error => {
        Alert.alert(
          'Oopss!',
          error.message,
          [
            {
              text: 'OK',
              onPress: () => console.log('Cancel Pressed'),
            },
          ],
        );
        console.log(error.message);
        this.setState({error: error.message});
      },
      {
        accuracy: {
          android: 'high',
          ios: 'best',
        },
        enableHighAccuracy: true,
        timeout: 15000,
        maximumAge: 10000,
        distanceFilter: 0,
        forceRequestLocation: true,
        forceLocationManager: false,
        showLocationDialog: true,
      },
    );
  }
}

@RajuPedimalla
Copy link

RajuPedimalla commented Dec 14, 2023

Can anyone help me on this ?
I am facing the same issue, Unable to show the Prompt to the users. Its going into the else statement and giving error code and saying No location provider available

const result = Functions.getInstance().requestLocationPermission();
result.then(res => {
if (res) {
Geolocation.getCurrentPosition(
position => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
},
error => {
console.log("Error getting geolocation: " + error.code, error.message);
},
{ timeout: 15000, forceRequestLocation: true,showLocationDialog: true,}
);
}
}

    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests