我试图在ionic2 hello world项目中使用地理定位,我按照official site上的说明添加了ionic插件"地理定位".
我运行了以下两个命令:
$ ionic plugin add cordova-plugin-geolocation
$ npm install --save @ionic-native/geolocation
这是我的家.ts:
import { Component } from '@angular/core';
import {Geolocation} from '@ionic-native/geolocation'
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
map:any=null;
geoInfo:any={
resp:'',
data:''
};
constructor(
public navCtrl: NavController,
private geolocation: Geolocation
) {
}
test(){
this.geolocation.getCurrentPosition().then((resp) => {
this.geoInfo.resp=JSON.stringify(resp);
// resp.coords.latitude
// resp.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
this.geoInfo.resp='Error getting location';
});
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
this.geoInfo.data=JSON.stringify(data);
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
}
}
然而,我在chrome的控制台中出现了以下错误:
EXCEPTION: Error in ./TabsPage class TabsPage - inline template:0:0 caused by: No provider for Geolocation!
error_handler.js:56ORIGINAL EXCEPTION: No provider for Geolocation!
起初我以为这是因为我在浏览器中调试,但后来我在我的Android手机中遇到了同样的错误.
那么,"没有地理定位Provider "是什么意思?我应该如何在ionic2项目中使用地理定位?
谢谢!