打字稿/谷歌地图:如何确定点击了哪个标记

时间:2017-01-23 21:46:29

标签: google-maps angular typescript arrow-functions

我有一组标记对象markers。然后我使用for循环向它们添加事件侦听器。但是,我似乎无法弄清楚如何确定点击标记的WHICH。

以下是我现在的代码:

for(var i = 0; i < this.markers.length; i++) //adds listener to all markers
{
  google.maps.event.addListener(this.markers[i], "click", () =>
  {
    //need to get access to which marker was clicked
    //need to use arrow function to retain proper reference to "this"
  }); 
}

我已尝试将参数传递给箭头函数,但似乎没有任何效果。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

for each ( var marker in this.markers ) {
    with( { mark: marker } ) { // <- mark will contain the marker, and keep it all the way
        google.maps.event.addListener( mark, 'click', function() {  
            return mark; // <- this will return the actual marker
        } );
    }
}