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');
}
}
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.
@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)!
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?
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?
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