【发布时间】:2019-02-09 11:43:19
【问题描述】:
我无法在 Ionic 3 中使用 GeoLocation。我已经 看了几个教程并遇到了同样的问题。 这是我的设置。
PS C:\> ionic info
cli packages: (C:\Users\myID\AppData\Roaming\npm\node_modules)
@ionic/cli-utils : 1.9.2
ionic (Ionic CLI) : 3.9.2
global packages:
Cordova CLI : 8.1.2 (cordova-lib@8.1.1)
local packages:
@ionic/app-scripts : 3.1.0
Cordova Platforms : none
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v8.10.0
npm : 5.6.0
OS : Windows 10
PS C:\>
PS C:\>
PS C:\>
PS C:\> ionic cordova plugin list
> cordova plugin ls
v Running command - done!
cordova-plugin-geolocation 4.0.1 "Geolocation"
PS C:\>
PS C:\>
PS C:\>
在设置 hello-world/空白地理位置应用程序时,我在引用 地理位置插件。
// app.module.ts
import { Geolocation } from '@ionic-native/geolocation';
// ...
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
Geolocation // <-- Errors here
]
我可以通过添加 ngx 后缀来解决这个错误:
// app.module.ts
import { Geolocation } from '@ionic-native/geolocation/ngx';
// ...
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
Geolocation // OK now
]
但是,当我真正去使用它时:
//home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation/ngx';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
lat: any;
lng: any;
constructor(public navCtrl: NavController,
public geo: Geolocation) {
}
ionViewDidLoad() {
this.geo.getCurrentPosition().then(pos => {
this.lat = pos.coords.latitude;
this.lng = pos.coords.longitude;
}).catch(err => console.log(err));
}
}
我开始收到此错误:
TypeError: Object(...) is not a function at Geolocation.getCurrentPosition
注释掉对getCurrentPosition 的调用会使错误消失。
我错过了什么?我是否得到了错误的插件版本?有没有办法 想知道哪个适用于 Ionic 3 的最新版本?
【问题讨论】: