Reputation: 493
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
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
Reputation: 175
The error is because of 'autocomplete' object is not created.
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