我收到以下错误:错误错误:未捕获(承诺):TypeError:无法读取未定义的属性'nativeElement'。
当我尝试在google map中添加带有ngIf的div时会发生这种情况:
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);
}
});
}
错误: Image error
尽管出现此错误,但地图工作正常,但是我无法通过在输入中输入地址来实现对地点的搜索,如果我删除了包含ngIf的div,它就可以工作。这是怎么回事 有什么想法吗?
答案 0 :(得分:0)
使用ngAfterViewInit代替ngOnInit
ngAfterViewInit () {
this.getPlacesAutocomplete();
}
视图查询是在调用ngAfterViewInit回调之前设置的。
您需要将*ngIf
更改为hidden
,这样才能使elemnt变得可用,否则searchElementRef
将为null,直到条件为真
<div [hidden]="mapVisibility === 'visible'">
...
</div>
答案 1 :(得分:0)
在ngAfterViewInit
上初始化组件(DOM已加载)后,调用您的函数
ngAfterViewInit() {
this.getPlacesAutocomplete();
}
或将代码放在setTimeout上(错误的主意):
ngOnInit() {
setTimeout( _ => this.getPlacesAutocomplete() , 1000)
}
如果您使用的是ngAfterViewInit
,请不要忘记class MyComponent implements AfterViewInit