Geolocation Plugin doesn't work on Android

IMPORT IS PLATFORM :

import { Platform } from 'ionic-angular';

  constructor( public platform: Platform )
   
  }

  ionViewDidLoad() {
    this.platform.ready().then(() => this.obtenerPosicion());
  }

  obtenerPosicion(): any {
    this.geolocation.getCurrentPosition().then(response => {
      console.log(response);
    })
      .catch(error => {
        console.log(error);
      })
  }

So I faced the same problem. What I was getting was an error like this: “{}”.

I looked at the source code and was amazed. No wonder it does not work, because there is no code implementing it! Just permission request. Maybe I am missing something… but the thing is: it does not work.

I used this fork and it works now:

Hope that helps someone.

3 Likes

I think that your problem is related to Ionic DevApp, here I have the same problem. It will works fine into your browser and after build your APK.

I was experiencing similar problems on Android with the geolocation plugin not returning a success or error response on Android if I disabled location services.

I noted the following section from the github page README:

Android Quirks

If Geolocation service is turned off the onError callback is invoked after timeout interval (if specified). If timeout parameter is not specified then no callback is called.

Adding a timeout to the call resolved the issue for me.

Adding timeout helped me as well. I proposed sooner to use different fork of the plugin, but specifying the timeout parameter solves the problem.

Also, I experienced that the location services are not working while using live reload. So I suggest to test it without it.

sir in my case getCurrentPosition are not fire initially and i have trigger function only inside ionViewDidLoad() function why function not fire initially …

/* Ensure the platform is ready */
ionViewDidLoad()
    /* Perform initial geolocation */
    this.geolocation.getCurrentPosition().then((position) => {
        console.log(position.coords.latitude)
    }).catch((err) => {
        console.log('Error getting location', error);
    });
}

Please explain sir why function not fire 1st time but logut then login again it will fire why?

sir same case with initially 1st time not fire any callback but after logout then login again function is fire again ?

sir are you fix above issue afer triiger function inside platform.ready() function?

For me I found out the problem was that the GPS was simply disabled on the device, after I activated the location services everything worked using the following code:

ngOnInit(){
    this.platform.ready().then(() => {
      this.geolocation.getCurrentPosition().then((resp) => {
        this.lat = resp.coords.latitude;
        this.lng = resp.coords.longitude;

        console.log("SUCCESS!");
      }).catch((error) => {
        console.log('Error getting location', error);
      });
    });
  }

If this solution helps, try using ionic diagnostics: https://ionicframework.com/docs/native/diagnostic
To determine if user has GPS enabled, hope that helps!

@durian777
I realized that my problem is the same as yours!

Appreciated!

1 Like

Getting the current position during start up runs the risk of being rejected on start up in browsers.

[Violation] Only request geolocation information in response to a user gesture.

There’s a recommendation from Google not to load geolocation on page load:

Users are mistrustful of or confused by pages that automatically request their location on page load. Rather than automatically requesting a user’s location on page load, tie the request to a user’s gesture, such as a tapping a “Find Stores Near Me” button. Make sure that the gesture clearly and explicitly expresses the need for the user’s location.

I had several issues with location, especially on Android.

switching to this plugin solved them all - it’s a clone of the original with some issues fixed.

    "cordova-plugin-geolocation": "git+https://github.com/chemerisuk/cordova-plugin-geolocation.git",

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.

Hi,
are you using it in the background or just with an activated app? I am struggling with background geolocation ($50 for solution)