Geolocation Plugin doesn't work on Android


#1

Hi, I have a trouble with Geolocation native plugin of Ionic

this is my code!

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';


/**
 * Generated class for the Trymap page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-trymap',
  templateUrl: 'trymap.html',
})

export class TrymapPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, private geolocation: Geolocation) {

    this.get_current_position();

  }


  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');
  }

}

The console on android emulator shows nothing!

Can i resolve it?


#2

Not even ionViewDidLoad Trymap?

Ah yes, don’t call the function in constructor.


#3

I try but doesn’t work.

the problem is here:

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

      console.log(position.coords.latitude)

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

this function return nothing.

nothing if i have enabled location and nothing if i have disabled location!


#4

You didn’t answer my question and didn’ tell me what you did to remove the method call from the constructor.


#5
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';


/**
 * Generated class for the Trymap page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-trymap',
  templateUrl: 'trymap.html',
})
export class TrymapPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, private geolocation: Geolocation, public plt: Platform) {


  }

  get_current_position() {
    this.geolocation.getCurrentPosition().then((position) => {
      console.log(position.coords.latitude)

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


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

}

#6

chrome inspector return this only


#7

I would add another console.login the first line of get_current_position so we know the code is executed.


#8
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';


/**
 * Generated class for the Trymap page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-trymap',
  templateUrl: 'trymap.html',
})
export class TrymapPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, private geolocation: Geolocation, public plt: Platform) {


  }

  get_current_position() {

    console.log("try to enter on this function get_current_position");

    this.geolocation.getCurrentPosition().then((position) => {
      console.log(position.coords.latitude)

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


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

}


#9

@Sujan12 any news for this issue?

Can I open an issue on github?


#10

I don’t know any further, otherwise I would have posted. (Still think this is not a plugin problem as hundreds of users are using this)

You can always open issues on Github - include this topic as a link to not be redirected just back here to solve this.


#11

@filippodicostanzo You might try making sure the platform is ready prior to making the geolocation call. Something along these lines:

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

I’m executing that code from within ionViewDidLoad and it is currently working for me in my app.

Hope it helps,
Chris


#12

Thank you @blazetopher.

Are you trying on an Android device or an Android emulator?


#13

I can’t get Geolocation to work either. I’m testing on 2 different devices. Both getCurrentPosition and watchPosition just returns an empty object ‘{}.’ I’ve tried with and without live reload, nothing seems to work. Google Map’s getMyLocation works, but I need to watch/subscribe to the device’s position.


#14

Yes, i have the same issue


#15

@filippodicostanzo - apologies for the delay in response. I have been focusing primarily on iOS and hadn’t tried things in an Android emulator in a while.

I just spun up an SDK23 emulator (which should be supported) and unfortunately I appear to have the same issue as you and @jfbguy are encountering. Neither the getCurrentPosition or watchPosition are returning valid data.

Hope someone addresses this soon (or finds a viable workaround)!


#17

@jaydz

Yes, I do call my location functions from within ionViewDidLoad - I omitted that from my sample as it was already mentioned higher in the post. The call-stack is basically ionViewDidLoad --> this.platform.ready --> geolocation.

I’ve seen numerous references stating that all native plugins should be executed/initialized from within this.platform.ready, but perhaps that’s not the case any longer?


#18

No, you’re right. I posted too quick. I’m calling from this.platform.ready also. Had to take a second look. And actually, I’m running my this.geolocation.getCurrentPosition() like so:
userCoordinates: object;

this.userCoordinates = {};
 this.userCoordinates['latitude'];
this.userCoordinates['longitude'];
this.initializeApp();
}```

``` initializeApp() { 
      this.platform.ready().then(() => {
        this.geolocation.getCurrentPosition().then((position => {
          this.userCoordinates['latitude'] = position.coords.latitude;
           this.userCoordinates['longitude'] = position.coords.longitude;
         })
.catch((error) => {
 console.warn(error);
  })
}
   }```

Sorry for no indentation in my example. No idea why it wont indent. It looks like i'm doing a couple/few things I shouldn't be doing (like running the function from my constructor?), but it works. maybe initializing the userCoordinates object ahead of time is helping on my end?

#19

I took my time to learn Ionic, seeing these kind of things just coming it’s just too hard to handle.
It’s not possible to produce any ionic 3 for the moment with theses kind of problem


#20

Thanks, I wonder if you solved it?
I have the same problem yet
Greetings


#21

Any news? I am facing the same problem.