Angular 9 & google 地图标记点击事件无法正常工作

Angular 9 & google map marker click event not working proprly

下面是我启动地图并添加集群和标记的代码。一切正常,但标记点击的行为很奇怪,就像事件被立即触发并点击 API 一样,但全局变量会在几秒钟后更新,有时在几分钟后更新。

  renderMap() {

    window['initMap'] = () => {
      this.loadMap();
    };
    if (!window.document.getElementById('google-map-script')) {
      const s = window.document.createElement('script');
      s.id = 'google-map-script';
      s.type = 'text/javascript';
      s.src = 'https://maps.googleapis.com/maps/api/js?key=' + environment.GOOGLE_MAP_API_KEY + '&callback=initMap';

      window.document.body.appendChild(s);
    } else {
      this.loadMap();
    }
  }

  loadMap() {
    const map = new window['google'].maps.Map(this.mapElement.nativeElement, {
      center: {lat: this.center.lat, lng: this.center.lng},
      zoom: this.zoom
    });
    const markers = this.markers.map((location, i) => {
      const marker = new google.maps.Marker({
        position: {lat: location.lat, lng: location.lng},
        label: location.label,
        icon: 'assets/img/marker_.png',
        title: location.title,
        clickable: true
      });

      marker.addListener('click', () => {
        const m = location;
        this.customer = m;
        this.showLocation = true;
        this.loading = true;
        this.center = {
          lat: m.lat,
          lng: m.lng
        };
        this.companyService.company(m.company_id).subscribe((company: any) => {
          this.company = company;
          this.loading = false;
        }, error => {
          this.loading = false;
          console.log(error);
        });
      });
      return marker;
    });

    const markerCluster = new MarkerClusterer(map, markers,
      {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});

  }

  ngOnInt(){
    this.renderMao();
  }

您可能需要 运行 NgZone

中的 click 回调
constructor(private zone: NgZone)


marker.addListener('click', () => {
    this.zone.run(() => { 
     ...
});