Ionic Native Gelocation not working

Hi,

Somehow the geolocation plugin is not working on one of the app i am building.

I have installed the Geolocation plugin using the following commands

ionic cordova plugin add cordova-plugin-geolocation
npm install --save @ionic-native/geolocation

The plugins are installled correctly.

Following is the code that i am running in Platform.ready() in app.component.ts

let geoOptions: GeolocationOptions = {
        enableHighAccuracy:true,
        timeout:500000,
        maximumAge:10,
      };

      this.geolocation.getCurrentPosition(geoOptions).then((data: Geoposition) => {
        console.log("Success", data);
      }, (error: PositionError) => {
        console.log("Error", error);
      });

The Success callback is never called. I have verified the location access is enabled on the device. Also, in the error call back the error is blank.

What is it i am doing wrong here?

Did you also do Step 2 of the Installation instructions?
https://ionicframework.com/docs/native/geolocation/

@Sujan12
Yes forgot to mention i did add it under app.modules.ts
import {Geolocation} from "@ionic-native/geolocation";

providers: [StatusBar,
   SplashScreen,
   {provide: ErrorHandler, useClass: IonicErrorHandler},
   RequestManagerProvider,
   UserProvider,
   CategoriesProvider,
   PostsProvider,
   Geolocation]

Still the same issue.

Have you granted location access to your app?

Yes. I’m facing similar issue, but only in some Android devices.

To get user location I’m using the following provider:

import {Injectable} from '@angular/core';
import {Geolocation, GeolocationOptions} from "@ionic-native/geolocation";
import {Storage} from "@ionic/storage";

import {Position} from "../../models/position.model";

@Injectable()
export class GeolocationProvider {

    private position: Position = {
        latitud: null,
        longitud: null
    };

    constructor(private geolocation: Geolocation, private storage: Storage) {}

    public getUserCurrentPosition(): Promise<Position> {
        console.log('[GeoProvider] getUserCurrentPosition launched...');

        return new Promise((resolve, reject) => {

            let geolocationOptions: GeolocationOptions = {
                enableHighAccuracy: true,
                timeout: 2500,
                maximumAge: 600000 // 10 minuts
            };

            this.geolocation.getCurrentPosition(geolocationOptions).then(
                (position) => {
                    console.log('[GeoProvider] getCurrentPosition success: ', position.coords.latitude, position.coords.longitude);
                    this.position.latitud = position.coords.latitude;
                    this.position.longitud = position.coords.longitude;
                    this.savePositionLocally();
                    resolve(this.position);
                },
                (err) => {
                    console.error('[GeoProvider] getCurrentPosition error: ', err);
                    if (this.position.latitud != null && this.position.longitud != null) {
                        resolve(this.position);
                    } else {
                        this.getPositionLocally().then(
                            (position: Position) => {
                                if(position) {
                                    console.log('[GeoProvider] getPositionLocally success.', position);
                                    this.position = position;
                                } else {
                                    console.log('[GeoProvider] getPositionLocally success. No older position.', position);
                                    reject();
                                }
                            },
                            () => {
                                console.warn('[GeoProvider] getPositionLocally error.');
                                reject();
                            }
                        );
                    }
                }
            );

        });
    }

    private savePositionLocally(): void {
        this.storage.set("user.position", this.position).then(
            () => {
                console.log('[GeoProvider] Save position success.');
            },
            () => {
                console.warn('[GeoProvider] Save position error.');
            }
        );
    }

    private getPositionLocally(): Promise<any> {
        return this.storage.get("user.position");
    }

}

If I run my APP to an Moto G3 it works. Also in a Sony Xperia T3 and 1+One.
But when I run the application to a BQ Aquaris M10 I allways get timeout error.

Sorry for my english. It’s not my native language.

After a long trial and error. I removed the platorm and the plugin and re added it. It worked, don’t know what exactly happened

I did exactly the same steps as you and it still not working :frowning: any other tips guys? this issue is driving me crazy :sweat_smile:

Hi,
i have also got same problem but in my case not received error callback or nor success callback,i don’t understand what the problem is that,i have also tried remove the platform and again add platform but not working i am using android 6.4 version.bro do you know any another way to fix?

Hi, did you find any solution to this?

NW just have to include this line in your code:
this.geolocation.getCurrentPosition({ ‘enableHighAccuracy’: true }).then(geo=>{
//geo.coords.latitude
});

Please don’t forget to include ‘enableHighAccuracy’ with: ‘’

It was a very pleasure

For all those facing the issue where the function below does not work on Android device.

this.geolocation.getCurrentPosition()

I fixed the issue after a day of scratching my head and Googling…
Just switch on “Location Services” on your android device.
Accepting location services when calling the function does not switch ON the location services.