Angular 爱奥尼亚地理定位插件错误:";“无地理定位提供商”;
我试图在我的ionic2 hello world项目中使用地理定位,我按照上的说明添加了ionic插件“geolocation” 我已经运行了以下两个命令:Angular 爱奥尼亚地理定位插件错误:";“无地理定位提供商”;,angular,typescript,ionic2,cordova-plugins,Angular,Typescript,Ionic2,Cordova Plugins,我试图在我的ionic2 hello world项目中使用地理定位,我按照上的说明添加了ionic插件“geolocation” 我已经运行了以下两个命令: $ ionic plugin add cordova-plugin-geolocation $ npm install --save @ionic-native/geolocation 这是我的家。ts: import { Component } from '@angular/core'; import {Geolocation} fro
$ 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手机中遇到了同样的错误
那么,“没有地理定位提供商”意味着什么?我应该如何在ionic2项目中使用地理定位
非常感谢 您需要将提供程序添加到NgModule,即提供程序下的module.ts
providers: [
Geolocation
]
您需要导入Geolocation类并将其添加到app.module.ts文件中的提供者列表中
import { Geolocation } from '@ionic-native/geolocation';
providers: [
Geolocation
]
这对我来说很管用,我在任何地方都导入了它,但忘了将它添加到app.module.ts上的提供者中 app.module.ts
import { Geolocation } from '@ionic-native/geolocation';
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
AuthService,
EmailValidator,
DataService,
Geolocation
]
然后在页面上,我显示了lat和long to,在本例中作为测试主页
import { Geolocation } from '@ionic-native/geolocation';
lat: number;
longt: number;
this.geolocation.getCurrentPosition().then((resp) => {
this.lat = (resp.coords.latitude);
this.longt =(resp.coords.longitude);
}).catch((error) => {
console.log('Error getting location', error);
});
然后在home.html{{lat}{{long}}
我知道这很简单,但我只是把数据拿出来,然后再把它放到地图上。我也遇到了同样的问题,我必须面对,我的原生核心插件在我的package.json中的版本不一样 您可以在以下位置查看此解决方案和其他文章:
对于您添加到
app.module.ts的离子4,
顶部:
import { Geolocation } from '@ionic-native/geolocation/ngx';
在底部,在提供者
数组内部,添加地理定位
:
providers: [
Geolocation,
...
]
将地理定位添加到
app.module.ts
后,我收到一个新错误:Uncaught ReferenceError:Geolocationa未定义
。检查拼写Geolocationa这些链接的具体说明有助于解决此问题吗?在Ionic 4中,当我向提供程序添加Geolocation时,我收到此错误:NgModule“AppModule”的提供程序无效-仅限于允许提供程序和类型,获得:[AuthGuardService、状态栏、SplashScreen、[object]?,…]
,最后一个是Geolocation@devnosaur,您必须像这样使用导入:import{Geolocation}来自'@ionic native/Geolocation/ngx'代码>@EgorMedvedev谢谢!在爱奥尼亚4中,/ngx
是我的钥匙!你应该把它做成一个答案,这样更容易找到。我确认,到2020年2月为止,这一切都是完美的。非常感谢分享!