import { Injectable } from "@angular/core"; import { Geolocation } from "@ionic-native/geolocation/ngx"; import { HttpClient } from "@angular/common/http"; import { AlertController } from "@ionic/angular"; @Injectable({ providedIn: "root", }) export class GooglemapService { public data: any = {}; TU_LLAVE = "AIzaSyAw_VEeJAY7R9kuABvgz9e9CiKogTW3MFc"; //wtredata //TU_LLAVE = "AIzaSyBrY7wrURAlHMM_h__9cCRodYALQg1kuzI"; //mauricio constructor( private geolocation: Geolocation, private http: HttpClient, private alertController: AlertController ) {} async loadMap(direccion:any) { console.log("serviceeeeee") const myLatLng: any = await this.getLocation(); this.data.latitud = myLatLng.lat; this.data.longitud = myLatLng.lng; let data = await this.direccion(direccion); return data; } private async getLocation() { const rta = await this.geolocation.getCurrentPosition(); return { lat: rta.coords.latitude, lng: rta.coords.longitude, }; } private async verificationLocation() {} /// Permite obtener la direccion y el barrio a partir de las coordenadas private direccion(direccion:any) { console.log(direccion) return new Promise((resolve, reject) => { const lat = this.data.latitud; const long = this.data.longitud; let idCiudad = direccion.ciudad; let ciudad ="" let abrebiatura = ""; let numero = direccion.numero; let casa = direccion.casa; if(idCiudad!=""){ ciudad = direccion.departamento[idCiudad].nombre abrebiatura = direccion.departamento[idCiudad].abreviatura console.log(ciudad+" "+abrebiatura) } this.http .get<{ status: string; results: any[] }>( `https://maps.googleapis.com/maps/api/geocode/json?address=CL+`+numero+`+`+casa+``+ciudad+`, +`+abrebiatura+`,+CO&key=${this.TU_LLAVE}`, { responseType: 'json' } ) .subscribe((e) => { if (e.status === 'OK') { // console.log(e.results[0].geometry.location) // this.data.direccion = e.results[0].formatted_address; //lat = e.results[0].geometry.location.lat const lat = e.results[0].geometry.location.lat const long = e.results[0].geometry.location.lng // this.data.barrio = e.results[0].address_components[2].long_name; resolve({ dir: e.results[0].formatted_address, lat, lng: long, }); } else { resolve({ dir: '', lat: null, lng: null, }); } }); }); } }