Jayna Tanawala
Jayna Tanawala

Reputation: 493

Cannot read property 'Autocomplete' of undefined while using google-places api

I want to get LatLng of entered address in textbox. I am using google map api for it. By writing only new google.maps.places.Autocomplete({}) I am getting Cannot read property 'places' of undefined this error.

Below is my code:

@ViewChild('addressText') addressText: any;

ngAfterViewInit() {
   this.getPlaceAutocomplete();
}

getPlaceAutocomplete() {
    const autoComplete = new google.maps.places.Autocomplete(this.addressText.nativeElement, {
      types: this.addressText.nativeElement.value
    })
    // google.maps.event.addListener(autoComplete, 'place_changed', () => {
    //   const place = autoComplete.getPlace();

    //   console.log('places... ', place);
    // })
  }

html file:

<input matInput placeholder="Locations" class="input" formControlName="text" #addressText>

Please help me out.

Upvotes: 0

Views: 3768

Answers (2)

Shahzaib Naseer
Shahzaib Naseer

Reputation: 80

I Faced The Same Issue And I was forgot to add &libraries=places after my key src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"

Upvotes: 1

Saima Haji
Saima Haji

Reputation: 175

The error is because of 'autocomplete' object is not created.

  • You need to defined a id for'input' tag.
  • Just giving you a basic example.

HTML:

<input matInput placeholder="Locations" class="input" formControlName="text" id="addressText">

JS:

autocomplete = new google.maps.places.Autocomplete(
  document.getElementById('addressText'), {types: ['geocode']});

For further details please go the through docs google api autocomplete

Upvotes: 0

Related Questions