Patricio Vargas
Patricio Vargas

Reputation: 5522

How to store lat and long from google places autocomplete in Angular

I decided to post the question and answer in stackoverflow, since when I followed this tutorial a year ago: https://www.youtube.com/watch?v=pxyX_5mtlTk. I tried to retrieve the lat and long from the google places autocomplete in Angular. I tried to google the solution,posted the question here (nobody answered so I deleted my question) and I didn't have luck, eventually I was able to figured it out and I decided to post on the youtube tutorial that I know how to do it, since a year ago I have got 35 emails asking me for the solution, so I decided to share it here too.

Upvotes: 2

Views: 3200

Answers (1)

Patricio Vargas
Patricio Vargas

Reputation: 5522

The problem is that the google places autocomplete uses callbacks and when you want to store information that is being populated by the api, you will have a hard time storing it, if it's not inside the callback.

In your ts file

autocompleteSearch() {
        this.mapsAPILoader.load().then(
      () => {        
         let autocomplete = new google.maps.places.Autocomplete(this.searchElement.nativeElement, {types:["address"]});

         autocomplete.addListener("place_changed", ()=>{
           this.ngZone.run(()=>{
             let place: google.maps.places.PlaceResult = autocomplete.getPlace();

             if(place.geometry === undefined || place.geometry === null) {
               return;
             }
             this.lat = place.geometry.location.lat();
             this.lng = place.geometry.location.lng();

           })
         })
      }      
     );
     console.log(this.lat, this.lng)
  }

In your html

<input [hidden]="!isLoggedIn()" class="albergueInfo" type="text" autocorrect="off" autocapitalized="off" 
      spellcheck="off"
      placeholder=""
      #address [value]="addressValue" (input)="addressValue=$event.target.value"/>

You can also retrieve more information than just the latitude and longitude . Following the google places api json object structure (https://developers.google.com/places/web-service/details) you can do something like:

autocompleteSearch() {
    this.mapsAPILoader.load().then(
    () => {        
      let autocomplete = new google.maps.places.Autocomplete(this.addressElement.nativeElement, {types:["address"]});
      autocomplete.addListener("place_changed", ()=> {
        this.ngZone.run(()=>{
          let place: google.maps.places.PlaceResult = autocomplete.getPlace();

          if(place.geometry === undefined || place.geometry === null) {
            return;
          }

          this.lat = place.geometry.location.lat();
          this.long = place.geometry.location.lng();
          this.addressArray = place.address_components;         
          this.address = place.formatted_address
          this.city = this.retriveAddressComponents('locality');
          this.country = this.retriveAddressComponents('country');
          this.zipcode = this.retriveAddressComponents('postal_code');
          this.state = this.retriveAddressComponents('administrative_area_level_1');  
        })
      })
    });
  }

  retriveAddressComponents(type : string) {
    let res =  this.addressArray.find(address_components => address_components.types[0] === type);
    return res.long_name;
  }

Upvotes: 5

Related Questions