I have an array of objects (cards) which contain an id
, latitude
and longitude
. I'm then building an array of markers to attach to a GoogleMap map:
for(var c = 0; c < cards.length; c++)
{
var id = cards[c]["id"];
var lat = cards[c]["lat"];
var lon = cards[c]["lon"];
var marker = new google.maps.Marker({ position: new google.maps.LatLng(lat, lon)});
marker.setMap(map);
marker.addListener('click', function(){
myListenerCallback(id);
});
}
On each marker I want to attach a callback on the click event. It can be done quite easily by using the marker's .addListener
method. The problem is, I want to fire a callback which includes the id of the card (as an input parameter): myListenerCallback(id)
. Looks like all the callbacks are firing using the last id of the array. How can I fire callbacks passing the corresponding id?