2017-01-23 82 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 
     }); 
    } 
} 
相關問題