首页 > 解决方案 > 当我为当前位置调用地理定位时。我面临这样的错误

问题描述

当我为当前位置调用地理定位时。我面临这样的错误

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

标签: angularjsionic3

解决方案


我试图安排代码,因为我认为代码中的问题

 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


推荐阅读