2017-01-23 81 views
0

我有一个标记对象数组markers。然后我使用for循环向它们添加事件监听器。但是,我似乎无法弄清楚如何确定哪些标记被点击。Typescript/Google Maps:如何确定点击了哪个标记

这里是我现在的代码:

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" 
    }); 
} 

我试图传递参数箭头的功能,但似乎没有任何工作。有任何想法吗?

回答

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 
     }); 
    } 
} 
相关问题