I encountered the following issue: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of undefined.
This problem arises when I attempt to include a div with an ngIf statement within a Google map:
HTML:
<div class="form-group">
<label>Location by map?</label>
<mat-select formControlName="ubication" [(ngModel)]="mapVisibility" class="form-control" placeholder="Select option">
<mat-option disabled>Select option</mat-option>
<mat-option value="visible">Yes</mat-option>
<mat-option value="hidden">No</mat-option>
</mat-select>
</div>
<div *ngIf="mapVisibility === 'visible'">
<div class="example-form">
<mat-form-field class="example-full-width">
<input matInput type="text" (keydown.enter)="$event.preventDefault()" autocorrect="off" autocapitalize="off" spellcheck="off" #agmSearch placeholder="Enter address">
</mat-form-field>
</div>
<agm-map id="googleMap" [latitude]="lat" [longitude]="lng" [zoom]="zoom">
<agm-marker [latitude]="lat" [longitude]="lng" [markerDraggable]="true" (dragEnd)="markerDragEnd($event)"></agm-marker>
</agm-map>
</div>
TS:
mapVisibility: string;
ngOnInit() {
this.getPlacesAutocomplete();
}
getPlacesAutocomplete() {
// load Places Autocomplete
this.mapsAPILoader.load().then(() => {
this.setCurrentLocation();
this.geoCoder = new google.maps.Geocoder;
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
types: ['address']
});
autocomplete.addListener('place_changed', () => {
this.ngZone.run(() => {
// get the place result
let place: google.maps.places.PlaceResult = autocomplete.getPlace();
// verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
// set latitude, longitude and zoom
this.lat = place.geometry.location.lat();
this.lng = place.geometry.location.lng();
this.zoom = 12;
});
});
});
}
private setCurrentLocation() {
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
this.zoom = 15;
this.getAddress(this.lat, this.lng);
});
}
}
getAddress(latitude, longitude) {
this.geoCoder = new google.maps.Geocoder();
this.geoCoder.geocode({ 'location': { lat: latitude, lng: longitude } }, (results, status) => {
console.log(results);
if (status === 'OK') {
if (results[0]) {
this.zoom = 12;
this.address = results[0].address_components[1].long_name + ' ' + results[0].address_components[0].long_name;
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
The code was sourced from: Angular 8/7 | Add Google Maps with Location Search in Angular 2 plus Applications using Angular Google Maps Module (@agm/core) Easily
ERROR: Image error
Despite encountering this issue, the map functions correctly. However, I am unable to implement location search functionality by entering an address in an input field, which works when the div containing the ngIf is removed. Any insights on this matter?