Angular 谷歌地图自动完成与角材料

Angular 谷歌地图自动完成与角材料,angular,google-maps-api-3,angular-material,Angular,Google Maps Api 3,Angular Material,当用户输入一个位置时,我正在尝试以角度材质的形式使用谷歌地图自动完成。它似乎无法正确读取输入,因为我遇到以下错误: 这是我的密码 html: <mat-form-field> <input matInput placeholder="Event Location" [formControl]="eventLocation" required #search type="text" id= "place"> </mat-form-field&

当用户输入一个位置时,我正在尝试以角度材质的形式使用谷歌地图自动完成。它似乎无法正确读取输入,因为我遇到以下错误:

这是我的密码

html:

<mat-form-field>
             <input matInput placeholder="Event Location" [formControl]="eventLocation" required #search type="text" id= "place">
</mat-form-field>
firstFormGroup: FormGroup;
public eventLocation: FormControl;
public searchElementRef: ElementRef;
eventPost: Latlong;
eventLat: number;
eventLng: number;
@ViewChild('search')

ngOnInit() {

      this.firstFormGroup = this._formBuilder.group({
        eventName: ['', Validators.required],
        eventLocation: ['', Validators.required]
      });
      this.eventLocation = new FormControl;
      this.mapsAPILoader.load().then(() => {
        const autocomplete = new google.maps.places.Autocomplete(
          this.searchElementRef.nativeElement, {
            types: []
        });
          autocomplete.addListener('place_changed', () => {
            this.ngZone.run(() => {
              const place: google.maps.places.PlaceResult = autocomplete.getPlace();
              if (place.geometry === undefined || place.geometry === null) {
                return;
              }
              this.eventPost = {
                latitude: place.geometry.location.lat(),
                longitude: place.geometry.location.lng()
              };
              this.mydata.latlongSource.next({ ...this.eventPost });
              this.eventLat = this.eventPost.latitude;
              this.mydata.markerLatSource.next( this.eventLat);
              this.eventLng = this.eventPost.longitude;
              this.mydata.markerLngSource.next( this.eventLng);
              this.eventLocation.reset();
            });
          });
      });

nativeElement
是一个DOM元素。您在
OnInit
hook中还没有DOM。使用
AfterViewInit
访问DOM元素:

ngAfterViewInit() {

      this.firstFormGroup = this._formBuilder.group({
        eventName: ['', Validators.required],
        eventLocation: ['', Validators.required]
      });

从您的代码中,我看到您只定义了
searchElementRef
,但没有为它设置任何值-
publicsearcheelementref:ElementRef。所以我假设您不是
publicsearchElementRef:ElementRef
@ViewChild('search')
想要
@ViewChild('search')searchElementRef:ElementRef