TypeError: undefined is not an object (evaluating 'result [0] .geometry')

0

Hello I'm trying to get the latitude and longitug in angular and be saved in firebase give me this error

My code is:

  

import {Component, OnInit} from '@ angular / core';   import {PlacesService} from '../services/ places.service';

@Component ({   selector: 'app-create',   templateUrl: './crear.component.html',   styleUrls: ['./crear.component.css'] }) export class CrearComponent {   place: any = {};   constructor (private placesService: PlacesService) {   }   saveLoad () {

var direccion = this.lugar.calle+','+ this.lugar.ciudad+','+this.lugar.pais;
this.lugaresService.obtenerGeoData(direccion)
.subscribe((result) => {
  this.lugar.lat = result[0].geometry.location.lat;
  this.lugar.lng = result[0].geometry.location.lng;
  this.lugar.id = Date.now();
  this.lugaresService.guardarLugar(this.lugar);
  alert('Indicio guardado con exito');
  this.lugar = {};
});

var direccion = this.lugar.calle+','+ this.lugar.ciudad+','+this.lugar.pais;
this.lugaresService.obtenerGeoData(direccion)
    .subscribe((result) =>{
      this.lugar.lat = this.lugar.lat = result[0].geometry.location.lat;;
      this.lugar.lng = this.lugar.lng = result[0].geometry.location.lng;
      this.lugar.id = Date.now();
      this.lugaresService.guardarLugar(this.lugar);
      alert('Indicio guardado con exito');
      this.lugar = {};
    });

}

}

/ *

and so I call the service

public obtenerGeoData(direccion) {
    return this.http.get('http://maps.google.com/maps/api/geocode/json?address=' + direccion + '&key=<tu api key>'   );

}
    
asked by Alejandro Estupiñán 31.10.2018 в 04:20
source

1 answer

0

I did not get anything in the result since the Google maps api had it in http and I changed it to https likewise concatenated my api key like this: 'https://maps.google.com/maps/api/geocode/json?address=' + direccion + '&key=<Aqui tu apiKey>' then I put a debugger to see what the result was, so I saw that I already got the geolocation and through the console navigate to get the latitude and longitude ending like this:

 var direccion = this.lugar.calle +','+ this.lugar.ciudad +','+ this.lugar.pais;
   this.lugarService
     .obtenerGeoData(direccion)
       .subscribe( (result:any) => {
         debugger;
         this.lugar.lat = result.results[0].geometry.location.lat;
         this.lugar.lng = result.results[0].geometry.location.lng;

         if (this.id != 'new') {
           this.lugarService.guardarLugar(this.lugar)
           // alert('¡Negocio editado con exito!')
           swal({
             type: 'success',
             title: 'Indicio editado con exito!',
           })
    
answered by 09.11.2018 / 08:54
source