点击新标记的角度谷歌地图如何关闭信息窗口?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (402)

我正在使用Angular谷歌地图:https//angular-maps.com/。现在作为默认功能,它将打开标记内定义的信息窗口。点击任何新标记后,它仍然是打开的旧窗口。我需要点击另一个标记来关闭信息窗口。这是html代码:

<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
          <agm-marker 
              *ngFor="let m of markers; let i = index"
              (markerClick)="clickedMarker(m.label, infoWindow, i)"
              [latitude]="m.lat"
              [longitude]="m.lng"
              [markerDraggable]="m.draggable"                
              [iconUrl]="m.icon">

            <agm-info-window #infoWindow>
              <strong>{{m.name}}</strong><br>
            </agm-info-window>

          </agm-marker>
        </agm-map>

这是点击标记时的代码:

clickedMarker(label: string, infoWindow, marker, index: number) {
     if( this.infoWindowOpened ===  infoWindow)
      return;

    if(this.infoWindowOpened !== null)
    {
     this.infoWindowOpened.close();
    }
    this.infoWindowOpened = infoWindow;
}

它在页面刷新时起作用。但只要我刷新标记变量它显示此错误:

ERROR TypeError: Cannot read property 'then' of undefined
at InfoWindowManager.close (info-window-manager.js:48)
at AgmInfoWindow.close (info-window.js:94)
at SearchComponent.clickedMarker (search.component.ts:188)

组件代码刷新标记:

this.markers.push({
              name: item.Name,  
              lat: parseFloat(item.latitude.toString()),
              lng: parseFloat(item.longitude.toString()),
              draggable: false,      
              icon:'assets/img/marker.png'
            });
提问于
用户回答回答于

你可以按照以下方法重写你的简单内容

previous;
...
clickedMarker(infowindow) {
    if (this.previous) {
      this.previous.close();
    }
    this.previous = infowindow;
  }

对模板中的事件绑定功能进行更改

(markerClick)="clickedMarker(infoWindow)"
用户回答回答于

你需要检查你的代码中的未定义:“this.infoWindowOpened!== undefined”

public clickedMarker(label: string, infoWindow: any, marker: any, index: number) {

    if (this.infoWindowOpened === infoWindow) {
        console.log("window already opened");
        return;
    }

    if (this.infoWindowOpened !== null && this.infoWindowOpened !== undefined) {
        this.infoWindowOpened.close();
    }
    this.infoWindowOpened = infoWindow;
}

扫码关注云+社区

领取腾讯云代金券