我正试图在Ionic中获得GPS坐标,但我并没有成功。我试图在控制台中显示它们,但它们从未出现过。这是我第一次使用Ionic,所以我不确定我是否正确使用它。
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
/**
* Generated class for the GpstestPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-gpstest',
templateUrl: 'gpstest.html',
})
export class GpstestPage {
constructor(public navCtrl: NavController, public navParams: NavParams, private geolocation: Geolocation) {
this.geolocation.getCurrentPosition().then((resp) => {
let userLocations = {
lat: resp.coords.latitude,
lon: resp.coords.longitude
}
console.log("Locations acquired");
console.log(userLocations);
// resp.coords.latitude
// resp.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
});
}
ionViewDidLoad() {
}
}
答案 0 :(得分:0)
尝试在this.platform.ready()
中包装原生呼叫。这样,在尝试访问本机插件之前,cordova就会准备就绪。
顺便说一下,Geolocation插件也有点挑剔,而且经常不起作用,所以这也可能是你的问题。