Ionic and geolocation issue on Android device when Location is off


#1

Hello Ionic 2 funs and developers,

I created an example application (similar to this) to test geolocation on android device.

Application work normally on firefox browser using ionic serve, but the problem appears when application is running on my android device (4.4) with ‘location’ option is off.

Geolocation part of the code looks like in the doc

let options = {timeout: 5000, enableHighAccuracy: true, maximumAge: 60000}; 
Geolocation.getCurrentPosition().then( 
          (resp) => {
            console.log("Geolocation : " + resp.coords.latitude + ", " + resp.coords.longitude);
            // center on the location
          },
          (error) => {
            console.log(error);
          },
          options
        );

and when ‘location’ option is off I expect to obtain an error PositionError.TIMEOUT or PositionError.POSITION_UNAVAILABLE, however the onError callback is never executed on the device.

If somebody knows how to deal with the problem, please share your experience

Cheers


#2

So a few things…

getCurrentPosition returns a promise, not an observable, so the setup is a bit off

    Geolocation.getCurrentPosition()
      .then((resp) => {
        console.log('Geolocation : ' + resp.coords.latitude + ', ' + resp.coords.longitude);
        // center on the location
        this.map.setCenter({ lat: resp.coords.latitude, lng: resp.coords.longitude });
      })
      .catch((error)=>{
        console.log(error)
      })

can you try that?


#3

Yes, it works as you said ! Thank you


#4

geolocation not working with Marshmallow android 6.0


#5

Hey,

Do you still have this issue?? damn Ioninc on Android phones!!!


#6

I have the similar issue on Android Phone.

Can I resolve it?

I use this simple code

get_current_position() {
    this.geolocation.getCurrentPosition().then((position) => {

      console.log(position.coords.latitude)

      // resp.coords.latitude
      // resp.coords.longitude
    }).catch((error) => {
      console.log('Error getting location', error);
    });
  }


  ionViewDidLoad() {
    console.log('ionViewDidLoad Trymap');
  }

And the console shows nothing