1

我在实现 Google 自动完成时收到此错误

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of undefined
TypeError: Cannot read property 'nativeElement' of undefined

我的组件与它类似:

@ViewChild('search', {static:false})
  public searchElementRef: ElementRef;




ngOnInit() {
 this.mapsAPILoader.load().then(() => {
      this.getMyUbication();
      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;
        });
      });
    });
}

在这部分中,我收到错误“无法读取未定义的属性‘nativeElement’”

HTML 代码:

<input type="text" id="geoloc" matGoogleMapsAutocomplete [(ngModel)]="geolocation" [ngModelOptions]="{standalone: true}" class="form-control" placeholder="Geolocalización" (keydown.enter)="$event.preventDefault()" autocorrect="off" autocapitalize="off" spellcheck="off" type="text" #search (onLocationSelected)="onLocationSelected($event)">
<ul>
   <li style="color: #bd1f2d" *ngIf="errors && errors['getmyubication']">{{errors['getmyubication']}}</li>
</ul>
<div *ngIf="showMap" class="map_st001" style="background: #DDDDDD;">
   <agm-map [latitude]="lat" [zoom]="zoom" [longitude]="lng" [scrollwheel]="null">
       <agm-marker [latitude]="lat" [longitude]="lng" [markerDraggable]="true" (dragEnd)="markerDragEnd($event)"></agm-marker>
   </agm-map>
</div>
4

0 回答 0