Permissions for geolocation access


#1

Hello,
I’m using geolocation in my app.
For some reason, when I launch the app, I’m not asked anything, and if geolocation access wasn’t authorized, then the app doesn’t work as it should.
So I was looking for how to have the app check the permission, and then ask for permission.
I installed the plugin called cordova-plugin-android-permissions

I followed the example and everythig was working fine.

Here’s what I did:

		var permissions = cordova.plugins.permissions;
		var perm_to_check = permissions.CAMERA;
		// Vérification permission
		permissions.hasPermission(perm_to_check, callback);
		function callback(status) {
			if ( status.hasPermission ) {
			alert("Permission is set");
		  }
		  else {
			alert("Permission is not set");
			permissions.requestPermission(perm_to_check, success, error);
		  }
		}
		function error() {
		  console.warn('permission is not turned on');
		  alert('permission is not turned on');
		}
		function success( status ) {
		  if( !status.hasPermission ) error();
		  else alert('permission request accepted, permission turned on');
		}

When I run the app, it checks if the camera has permission. If it doen’t, then I’m asked if I want to authirize or not. Until here everything’s fine.

But in my case, I don’t need camera, I need geolocation.

So I replaced this:

var perm_to_check = permissions.CAMERA;

by this:

var perm_to_check = permissions.ACCESS_FINE_LOCATION;

And what happens is: I have a message showing that I don’t have permission, and that’s it. I’m not asked for anything.

I even tried other values like ACCESS_COARSE_LOCATION, ACCESS_LOCATION_EXTRA_COMMANDS, ACCESS_MOCK_LOCATION - everything that contains the word location :slight_smile: but nothing worked.

What am i doing wrong ?
Can anyone help me with this please ?

Thanks a lot.


#2

You might need to check that Geolocation is enabled, see…

LocationAccuracy

if (this.platform.is('cordova')) {

      this.platform.ready()
        .then(() => {

          console.log('Enable Geolocation');

          this.locationAccuracy.canRequest()
            .then((canRequest: boolean) => {

              console.log("Can request: ", canRequest);

              if (canRequest) {
                // the accuracy option will be ignored by iOS
                let accuracy = this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY;
                this.locationAccuracy.request(accuracy)
                  .then(() => console.log('Request successful'),
                  error => console.log('Error requesting location permissions', error)
                  );
              }

            });

        })

    }

#3

Hello,

Thanks for your answer.
In the meantime, I found a way to do what I needed.
The thing is that I will check if geolocation is enabled only if the user go to some pages - not all the screens of the app.
So I have 4 checks to do at 4 differents places - or better, it’s the same check, at 4 different places.

I wouldn’t want to copy / paste the same code several times.
So I added this code in a separate js file, but how can I have this code run when I open the different screens ?

Thanks for your help.


#4

How did you do it?

You make a “Provider” and { import } that provider into wherever you need it, then you can call the functions in the provider whenever you need them.

Are you using Typescript?


#5

What I did is:

  • I added these lines to config.xml:
<feature name="Geolocation">
	<param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
  • I added these lines to platform/android/AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

Then I my app.js file, I do this:

		var permissions = cordova.plugins.permissions;
		var perm_to_check = permissions.ACCESS_FINE_LOCATION;
		// Vérification permission
		permissions.hasPermission(perm_to_check, callback);
		function callback(status) {
			if ( status.hasPermission ) {
				alert("Permission is set");
			}
			else {
				alert("Permission ACCESS_FINE_LOCATION is not set");
				permissions.requestPermission(perm_to_check, success, error);
			}
		}
		function error() {
		  console.warn('permission is not turned on');
		  alert('permission is not turned on');
		}
		function success( status ) {
		  if( !status.hasPermission ) error();
		  else alert('permission request accepted, permission turned on');
		}

So when I run the app, it first checks if geolocation is enabled or not. If it’s not, then I’m asked if I want to authorize. That’s exaclty what I wanted.

But as I said, now I’d like to include this into a javascript file, and be able to call it not in the app.js, but in the files of the different screens where this check is needed.

I’m not using Typescript but angular, I’m in Ionic 1. How can I do then ?
Could you please give me the code ?


#6

Finally I decided to do another way: the check is executed several times in the app, in 4 menus, so I decided to check on main screen, and if the permission request fails, then I exit the app.

I still have an issue though:
now I’d like to check for permission about when the user clicks to change his picture in the app.
For geolocation, the permission name was permissions.ACCESS_FINE_LOCATION.
In the case of changing picture (accessing to the medias of the device), what would be the permission name ?


#7

Hello again,

I’m facing another issue.
When the app runs for the fisrt time, it asks the user if he wants to authorize location permission.
If he says yes, then everything’s ok.
But if the “position icon” (at the top window in android when you slide, next to wifi icon) is set to disabled, then nothing happens.
I mean, in this case, the user is asked for permission, and the permission is set, but when user clicks to see the map, it’s empty.

How can I do to enabled the “position button” when it’s disabled ?
I’m not sure if I’m clear…

Thansk for your help.

Regards


#8

any help ?
Please, i really don’t know what to do…
Thanks


#9

same problem with me :frowning: :frowning: