I’ve been developing an application in ionic 3. Everything perfect but now I want to implement what is a button and a place finder, for the moment I’m concentrating on the button called “my location”.
I do everything through service, and these cordenadas I keep them in firabase and on the map I return them and the samples.
Now if I am in the Map and I move to a place and we say that I want to return to my coordinates, I only give it to my button and it returns me to my coordinates, that is where my marker is.
This is the screenshot:
This is my service code:
import { Injectable } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation';
import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database';
import { UsuarioService } from './../usuario/usuario';
/*
Generated class for the UbicacionProvider provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular DI.
*/
@Injectable()
export class UbicacionService {
usuario: FirebaseObjectObservable<any[]>;
private watch: any;
constructor(private geolocation: Geolocation,
private af: AngularFireDatabase,
private _us: UsuarioService) {
if (!this._us.telefono) {
return;
}
this.usuario = this.af.object('/tb01_usuario/' + this._us.telefono);
}
iniciar_localizacion() {
this.watch = this.geolocation.watchPosition()
.subscribe((data) => {
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
if (!this._us.telefono) {
return;
}
this.usuario.update({ lat: data.coords.latitude, lng: data.coords.longitude })
});
}
Miubiacion() {
//Aqui iria mi codigo si solo supiera cual es xd//
}
detener_watch() {
this.watch.unsubscribe();
}
}
And well this is in my MapPage:
import { HomePage } from './../home/home';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { UbicacionService } from './../../providers/ubicacion/ubicacion';
import { UsuarioService } from './../../providers/usuario/usuario';
@IonicPage()
@Component({
selector: 'page-map',
templateUrl: 'map.html',
})
export class MapPage {
usuario: any = {};
constructor(public navCtrl: NavController, public navParams: NavParams,
private _ubicacion: UbicacionService,
private _us: UsuarioService) {
this._ubicacion.iniciar_localizacion();
this._ubicacion.usuario
.subscribe(data => {
//console.log(data)
this.usuario = data;
})
}
Miubiacion() {
//Aqui iria mi codigo si solo supiera cual es xd//
}
salir() {
this._us.borrar_usuario();
this._ubicacion.detener_watch();
this.navCtrl.setRoot(HomePage);
}
}