Android geolocation timeout

I am trying to get the default ionic2 geolocation plugin to run on my android test device. The code blow works in browser and iOS, but fails with a timeout on android.

PositionError {code: 3, message: "Timeout expired"}

Here’s the code

    Geolocation.getCurrentPosition({ maximumAge: 60000, timeout: 30000, enableHighAccuracy: true    }).then((position) => {
          console.log(position);
    }, (error) => {
          console.log(error);
    });

The manifest contains

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.location.gps" />

Yet it fails. Any ideas how to solve this?

1 Like

have you found a solution?

same problem with me :frowning:

call this function by using set timeout function like :
setTimeout(function () {
Geolocation.getCurrentPosition({ maximumAge: 60000, timeout: 30000, enableHighAccuracy: true }).then((position) => {
console.log(position);
}, (error) => {
console.log(error);
});
}, 1500);

same problem :frowning:

Try disabling the highAccuracy, this has sometimes worked for me, but I don’t know why…

Maybe this will help. I’ve run into this problem a lot. As usual, the Ionic docs and etc. are lacking… https://stackoverflow.com/questions/46689339/ionic-2-geolocation-timeout-error

Guys, after a 2 day fight i managed to get the location doing the following: (component code below)

1.make sure to call the location plugin, when the device is ready and not before that using the
Platform plugin.
2.make sure to ask for add android permissions using the AndroidPermissions plugin.
3. go to your android.json file → find the "<uses-feature android:name=\"android.hardware.location.gps\" />", and add android:required=\"true\" before the closing tag, so it changes to this <uses-feature android:name=\"android.hardware.location.gps\" android:required=\"true\" />.
4. Now go to your AndroidManufest.xml find the <uses-feature android:name="android.hardware.location.gps"/> and delete it. A new line should be added on the next build that states this: <uses-feature android:name="android.hardware.location.gps" android:required="true" />.
5. reboot your device. If you are asked if you want to use Google Location Service select agree.
6. build your app for android. I did it like such

ionic cordova run andoird --device
and i am using the chome dev tools to debug it.
I have been on this for a really long time so I hope this helps someone. I haven’t tested on multiple devices tho.

public getLocation = async () => {
        console.log(this.andoridPermissions.PERMISSION);

        await this.platform.ready().then( async (data) => {
            console.log('READY!READY!READY!');

            this.andoridPermissions.checkPermission(this.andoridPermissions.PERMISSION.ACCESS_FINE_LOCATION).then((resp) => {
                console.log(resp);
            });

            this.andoridPermissions.requestPermissions([
                this.andoridPermissions.PERMISSION.ACCESS_FINE_LOCATION,
                this.andoridPermissions.PERMISSION.ACCESS_COARSE_LOCATION,
                this.andoridPermissions.PERMISSION.ACCESS_MOCK_LOCATION,
                this.andoridPermissions.PERMISSION.ACCESS_LOCATION_EXTRA_COMMANDS])
                .then( async (response) => {
                    console.log(response);
                    console.log('GETTING LOCATION');
                    await this.location.getCurrentPosition(this.locationOptions).then((resp) => {
                        console.log('succ');
                        console.log(resp);
                        // this.mapElement.nativeElement.innerText = 'LOCATION!';
                    }).catch((err) => {
                        console.log('ERROR!');
                        console.log(err);
                        // this.mapElement.nativeElement.innerText = err.message;
                    });
                });
            });
    }

EDIT: I am using ionic 4. ng 7.