IONIC:TypeError:Object(..)不是Geolocation.getCurrentPosition的函



我在应用程序中添加地理定位功能时遇到问题。我正在使用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": {}
}
}

然后当我运行离子发球时,控制台中出现错误:

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)

请帮助解决方案。

Geolocation插件只有在deviceready事件触发后才可用。你可以在这里找到这个。

为了解决您的问题,您必须在Platform.ready()完成后才使用地理定位。尝试将您的代码修改为以下内容:

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

相关内容

  • 没有找到相关文章

最新更新