首页 > 解决方案 > 事件监听器参考打字稿函数

问题描述

我正在尝试添加引用本地 TypeScript 函数的事件侦听器。一旦我得到我的位置,我会循环我的响应,为每个标记添加一个事件。

        for (count = 0; count < this.historyList.length; count++) {
          let position = new google.maps.LatLng(this.historyList[count].latitude, 
              this.historyList[count].longitude)
          marker = new google.maps.Marker({position: position});
          
          marker.setMap(this.map);
          //now add an event to the marker.
        google.maps.event.addListener(marker, 'click', (function (marker) {
          return function () {
            infowindow.setContent(address);
            infowindow.open(this.map, marker);
            console.log(marker.getPosition().lng())
            this.map.setCenter(marker.getPosition() as google.maps.LatLng);
          }
        })(marker));

现在这对于单击标记很有效,但我无法引用我的任何打字稿函数,所以我尝试添加一个不同的侦听器:

    google.maps.event.addListener(marker, 'click', () => {
      this.highlightItem(3);  
    });

这有效(使用=>)允许我访问我的本地打字稿,但现在我不会得到被点击的特定标记,它总是会引用循环中的最后一个。如何获取已单击的标记和参考打字稿方法?

谢谢

标签: typescriptgoogle-maps-api-3google-maps-markers

解决方案


推荐阅读