当我为currentlocation调用geolocation时,我会遇到这样的错误
TypeError: Object(...) is not a function
at Geolocation.getCurrentPosition
import { Geolocation } from '@ionic-native/geolocation/ngx';
constructor(public geolocation:Geolocation ){}
this.geolocation.getCurrentPosition().then((resp) => {
// resp.coords.latitude
// resp.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
});发布于 2019-02-28 08:38:07
我试着整理代码,因为我认为代码中的问题
constructor(public navCtrl: NavController,private geolocation: Geolocation) {
navigator.geolocation.getCurrentPosition(this.onSuccess, this.onError);
}
onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
onSuccess(position) {
let element = document.getElementById('map');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;
console.log(element)
}如果不起作用,您可以看到这个https://forum.ionicframework.com/t/geolocation-issues/157072/7
此外,您还可以在此处查看教程https://github.com/apache/cordova-plugin-geolocation#coords
https://stackoverflow.com/questions/54689293
复制相似问题