简体   繁体   English

ionic geolocation 插件错误:“No provider for Geolocation”

[英]ionic geolocation plugin error: "No provider for Geolocation"

I'm trying to use geolocation in my ionic2 hello world project, and I add the ionic plugin "Geolocation" following the instruction on the official site .我正在尝试在我的 ionic2 hello world 项目中使用地理定位,并按照官方网站上的说明添加离子插件“Geolocation”。

I've run these two commands:我已经运行了这两个命令:

$ ionic plugin add cordova-plugin-geolocation
$ npm install --save @ionic-native/geolocation

And this is my home.ts:这是我的 home.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
      });

  }

}

However, I got the following error in my chrome's console:但是,我的 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!

截屏

At first I thought it was because I was debugging in browser, but then I got then same error in my Android phone.起初我以为是因为我在浏览器中调试,但后来我的 Android 手机出现了同样的错误。

So what does "No provider for Geolocation" mean and how should I use geolocation in my ionic2 project?那么“No provider for Geolocation”是什么意思,我应该如何在我的 ionic2 项目中使用地理定位?

Thanks a lot!非常感谢!

You need to add the provider to the NgModule, ie module.ts under providers, 您需要将提供程序添加到NgModule,即提供程序下的module.ts,

providers: [
  Geolocation
]

You need to import the Geolocation class and add it to your list of providers in the app.module.ts file 您需要导入Geolocation类并将其添加到app.module.ts文件中的提供程序列表中

import { Geolocation } from '@ionic-native/geolocation';

providers: [
     Geolocation
]

that worked for me, I had it imported everywhere but had forgotten to add it to the providers on app.module.ts app.module.ts 这对我有用,我把它导入到处都是但忘记将它添加到app.module.ts app.module.ts上的提供者

import { Geolocation } from '@ionic-native/geolocation';

providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
AuthService,
EmailValidator,
DataService,
Geolocation
]

then on the page I was displaying the lat and long to in this instance as a test home; 然后在页面上我显示lat和long在这种情况下作为测试家庭;

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);
 });

then on home.html {{lat}} {{longt}} 然后在home.html上{{lat}} {{longt}}

I know it was only simple but I was just getting the data out before moving onto to placing it into map. 我知道这很简单但我只是在将数据放入地图之前将数据输出。

I had the same problem and I face it that I had my native core plugins was not in the same versions into my package.json. 我有同样的问题,我面对它,我的本机核心插件与我的package.json中的版本不同。

You can check for this solution and this other article at the followings: 您可以在以下方面检查此解决方案和其他文章:

https://forum.ionicframework.com/t/cant-resolve-peer-dependencies-after-update/107224/2 https://blog.ionicframework.com/help-test-ionic-native-5/ https://forum.ionicframework.com/t/cant-resolve-peer-dependencies-after-update/107224/2 https://blog.ionicframework.com/help-test-ionic-native-5/

https://ionicframework.com/docs/native https://ionicframework.com/docs/native

For Ionic 4 you add to app.module.ts top: 对于Ionic 4,您可以添加到app.module.ts顶部:

import { Geolocation } from '@ionic-native/geolocation/ngx';

And on the bottom, inside of the Providers array, add Geolocation : 在底部,在Providers数组的内部,添加Geolocation

providers: [
    Geolocation,
    ...
]

In 2022 , the following works for me:2022 年,以下对我有用:

I had to do the following changes in order for Geolocation to work:为了使地理定位工作,我必须进行以下更改:

ionic integrations disable capacitor离子集成禁用电容器

ionic cordova plugin add cordova-plugin-geolocation ionic cordova 插件添加 cordova-plugin-geolocation

npm install @awesome-cordova-plugins/geolocation npm 安装@awesome-cordova-plugins/geolocation

npm install @awesome-cordova-plugins/core npm 安装@awesome-cordova-plugins/core

What the above commands do, in the specified order, is to disable the capacitor integration as cordova plugin will not be installed alongside of the capacitor version.上述命令按照指定的顺序执行的操作是禁用电容器集成,因为 cordova 插件不会与电容器版本一起安装。 Then install the geolocations plugins and also install the core or else the angular compiler will throw an error.然后安装地理定位插件并安装核心,否则角度编译器将抛出错误。

In app.module.ts , add the following:app.module.ts中,添加以下内容:

import { Geolocation } from '@awesome-cordova-plugins/geolocation/ngx';

providers: [Geolocation],

Once all the above has been configured, within the page where you need the geolocation to trigger, you may add the following code:以上配置完成后,在需要触发地理定位的页面中,可以添加如下代码:

import { Component, OnInit } from '@angular/core';
import { Geolocation } from '@awesome-cordova-plugins/geolocation/ngx';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.page.html',
  styleUrls: ['./dashboard.page.scss'],
})
export class DashboardPage implements OnInit {

  latitude: any;
  longitude: any;

  constructor(private geolocation: Geolocation) { }

  ngOnInit() {
    this.geolocation.getCurrentPosition().then((resp) => {
      this.latitude = resp.coords.latitude;
      this.longitude = resp.coords.longitude;
    }).catch((error) => {
      console.error('Error getting location', error);
    });
  }

}

You will now be able to show the coordinates in the HTML page like so:您现在可以像这样在 HTML 页面中显示坐标:

{{latitude}} <---> {{longitude}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM