【发布时间】:2017-08-17 00:15:37
【问题描述】:
我正在尝试在我的 ionic2 hello world 项目中使用地理定位,并按照official site 上的说明添加离子插件“地理定位”。
我已经运行了这两个命令:
$ 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!
一开始我以为是在浏览器中调试,但后来我的安卓手机也出现了同样的错误。
那么“没有地理定位提供者”是什么意思,我应该如何在我的 ionic2 项目中使用地理定位?
非常感谢!
【问题讨论】:
标签: angular typescript ionic2 cordova-plugins