IONIC : TypeError: Object(...) is not a function at Geolocation.getCurrentPosition

Posted

技术标签:

【中文标题】IONIC : TypeError: Object(...) is not a function at Geolocation.getCurrentPosition【英文标题】: 【发布时间】:2020-06-29 05:45:27 【问题描述】:

尝试在我的应用中添加地理定位功能时遇到问题。我正在使用https://ionicframework.com/docs/native/geolocation

home.ts,在ionViewDidLoad()中调用这个findUserLocation():

import  Geolocation  from '@ionic-native/geolocation/ngx';

...

constructor(private geolocation: Geolocation) 
ionViewDidLoad()
    this.findUserLocation()


  

findUserLocation()
    let options = 
      enableHighAccuracy: true,
      timeout: 25000
    ;

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

      this.location = 
        latitude: position.coords.latitude,
        longitude: position.coords.longitude
      ;

      this.mapsProvider.init(this.location, this.mapElement);

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

   

这是我的package.json

"dependencies": 
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@ionic-native/core": "4.20.0",
    "@ionic-native/geolocation": "^5.22.0",
    "@ionic-native/splash-screen": "4.20.0",
    "@ionic-native/status-bar": "4.20.0",
    "@ionic/storage": "2.2.0",
    "cordova-plugin-geolocation": "^4.0.2",
    "ionic-angular": "3.9.9",
    "ionicons": "3.0.0",
    "rxjs": "^6.3.3",
    "rxjs-compat": "^6.3.3",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.29"
  ,
  "devDependencies": 
    "@ionic/app-scripts": "3.2.4",
    "typescript": "2.6.2"
  ,
  "description": "An Ionic project",
  "cordova": 
    "plugins": 
      "cordova-plugin-geolocation": 
    
  

然后当我运行 ionic serve 时,控制台出现错误:

ERROR TypeError: Object(...) is not a function
    at Geolocation.getCurrentPosition (index.js:10)
    at HomePage.webpackJsonp.263.HomePage.findUserLocation (home.ts:45)
    at HomePage.webpackJsonp.263.HomePage.ionViewDidLoad (home.ts:34)
    at ViewController._lifecycle (view-controller.js:487)
    at ViewController._didLoad (view-controller.js:370)
    at NavControllerBase._didLoad (nav-controller-base.js:789)
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)

请帮忙解决。

【问题讨论】:

【参考方案1】:

地理定位插件仅在deviceready 事件触发后可用。你可以找到这个here。

要解决您的问题,您只能在 Platform.ready() 完成后使用 Geolocation。尝试将您的代码修改为以下内容:

import  Geolocation  from '@ionic-native/geolocation/ngx';
import  Platform  from '@ionic/angular';

constructor(
  private geolocation: Geolocation
  public platform: Platform,
) 

ionViewDidLoad()
   this.platform.ready().then(() => 
     this.findUserLocation()
   );

另外,请确保您已通过运行以下命令正确安装插件,然后重新构建/运行您的应用:

ionic cordova plugin add cordova-plugin-geolocation
npm install @ionic-native/geolocation --save

【讨论】:

已完成您的建议,但仍然无法正常工作 ERROR Error: Uncaught (in promise): TypeError: Object(...) is not a function TypeError: Object(...) is not a功能在 Geolocation.getCurrentPosition (index.js:10) 在 HomePage.webpackJsonp.272.HomePage.findUserLocation (home.ts:45) 在 home.ts:33 这些版本对我来说很好用:“@ionic-native/geolocation”:“^5.15.0”,“cordova-plugin-geolocation”:“^4.0.2”,你能确认一下吗?使用这些版本也失败了?

以上是关于IONIC : TypeError: Object(...) is not a function at Geolocation.getCurrentPosition的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图在 Ionic 2 应用程序中不起作用

ionic 3 Call-Number插件问题

Ionic Studio - TypeError:无法读取 null 的属性“主要”

NgZone/Angular2/Ionic2 TypeError:无法读取未定义的属性“运行”

Ionic3测试:TypeError:无法读取undefined的属性'subscribe'

ionic 5 : TypeError: 不能读取未定义的属性'subscribe'。