Searching Google geolocation in the angular

Asked

Viewed 485 times

-1

Hi, I have to search Longitude and Latitude, by Google API, with Angular..

I’m using this code to perform the requisition:

constructor(private http: Http, private vwServicePagination: VwServicePagination){}

    obtendoGeolocalizacao() {  
        return this.http.get('https://maps.googleapis.com/maps/api/geocode/json?address=09771220')
            .map(response => response.json());
    }

And here at Oninit I’m calling this method created above... This way:

ngOnInit(){
        const teste = this.vwServiceApi.obtendoGeolocalizacao();
        
        console.log(this.teste);
    }

However, the returned object has nothing to do with the json that is returned by the API! How do I get the JSON object returned by the API?

  • 1

    vc have to use subscribe, this.vwServiceApi.getting locationGocation(). subscribe(reply=> console.log(reply))

  • Good, If you want to paste in your answer... ngOnInit(), # this.vwServiceApi.getting the location(). subscribe(res => {this.apiGoogle = res.json(); const lat = this.apiGoogle.Results[0].geometry.Location.lat; const lng = this.apiGoogle.Results[0].geometry.Location.lng; }); this.dealerships = this.vwServiceApi.dealerships(); }

1 answer

1


I used it as follows to get the object...

this.vwServiceApi.obtendoGeolocalizacao().subscribe(res => {this.apiGoogle = res.json();                
                this.lat = this.apiGoogle.results[0].geometry.location.lat;
                const lng = this.apiGoogle.results[0].geometry.location.lng;
                const uf = this.apiGoogle.results[0].address_components[3].short_name;               
            });     

Browser other questions tagged

You are not signed in. Login or sign up in order to post.