Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
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.

I've run these two commands:

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

And this is my 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:

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!

screenshot

At first I thought it was because I was debugging in browser, but then I got then same error in my Android phone.

So what does "No provider for Geolocation" mean and how should I use geolocation in my ionic2 project?

Thanks a lot!

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You need to add the provider to the NgModule, i.e module.ts under providers,

providers: [
  Geolocation
]
about 4 years ago · Santiago Trujillo Relatório

0

You need to import the Geolocation class and add it to your list of providers in the app.module.ts file

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

providers: [
     Geolocation
]
about 4 years ago · Santiago Trujillo Relatório

0

For Ionic 4 you add to app.module.ts top:

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

And on the bottom, inside of the Providers array, add Geolocation:

providers: [
    Geolocation,
    ...
]
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda