Google Maps InfoWindow - (单击)触发Angular 2功能

时间:2017-08-31 22:22:59

标签: javascript angular google-maps infowindow

我有一个在Angular 2+应用中利用谷歌地图的组件。是否有一种不错的方法可以获得一个链接到标记的infowindow,它会在我的组件中触发一个方法?下面的 istringstream strm(raw_text); Point p; while( strm >> p ) 是问题所在。如果我想搞制一个全局方法(希望避免这种情况),我可以交换到INSERT IGNORE INTO mytable VALUES(:date,:data1,:data2,:data3,:data4,:data5)

(click)

1 个答案:

答案 0 :(得分:1)

找到了几件可以让这项工作没有疯狂的角度开销。关键是:

  1. 只有1个信息窗口(成员)
  2. 为锚标记添加唯一ID
  3. 当infoWindow打开(domready)时,将点击监听器添加到已点击的锚点ID中
  4. 以下代码:

    //Add directions if we have a current location
    let additionalContent = "";
    if (this.positionMarker) {
      additionalContent = `<br><a href='#' id='marker_${markerId}'>Directions</a>`;
    }
    
    //Add the marker listener to open the infowindow
    google.maps.event.addListener(marker, 'click', () => {
      this.infoWindow.setContent(content + additionalContent);
      this.infoWindow.open(this.map, marker);
    
      //Once infow window is open, add a click listener based on the ID in the anchor tag
      if (additionalContent) {
        google.maps.event.addListenerOnce(this.infoWindow, 'domready', () => {
          document.getElementById(`marker_${markerId}`).addEventListener('click', () => {
            this.startNavigating(marker.position.lat(), marker.position.lng());
          });
        });
      }
    });