plugin custom angular typescript ionic2 cordova-plugins

angular - custom - Error del complemento de geolocalización iónica: "No hay proveedor de Geolocalización"



ionic native (4)

Debe agregar el proveedor a NgModule, es decir, módulo.ts bajo proveedores,

providers: [ Geolocation ]

Estoy tratando de usar la geolocalización en mi proyecto ionic2 hello world, y agrego el complemento iónico "Geolocalización" siguiendo las instrucciones del sitio oficial .

He ejecutado estos dos comandos:

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

Y esta es mi casa.

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

Sin embargo, recibí el siguiente error en la consola de mi 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!

Al principio pensé que era porque estaba depurando en el navegador, pero luego tuve el mismo error en mi teléfono Android.

Entonces, ¿qué significa "No hay proveedor de Geolocalización" y cómo debo usar la geolocalización en mi proyecto ionic2?

¡Muchas gracias!


Debe importar la clase de Geolocalización y agregarla a su lista de proveedores en el archivo app.module.ts

import { Geolocation } from ''@ionic-native/geolocation''; providers: [ Geolocation ]



eso funcionó para mí, lo importé en todas partes, pero había olvidado agregarlo a los proveedores en app.module.ts app.module.ts

import { Geolocation } from ''@ionic-native/geolocation''; providers: [ StatusBar, SplashScreen, {provide: ErrorHandler, useClass: IonicErrorHandler}, AuthService, EmailValidator, DataService, Geolocation ]

luego, en la página, estaba mostrando el tiempo y el tiempo en este caso como una casa de prueba;

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

luego en home.html {{lat}} {{longt}}

Sé que solo era simple, pero estaba obteniendo los datos antes de pasar a colocarlos en el mapa.