JavaScript click event for any google maps markers?

Evanss picture Evanss · Feb 28, 2012 · Viewed 16k times · Source

I need to make something happen when any google maps marker is clicked on. Im using the demo from this link as a starting point:

http://gmap3.net/examples/tags.html

UPDATE - ive tried the following:

$.each(markers, function(i, marker){
  marker.click(function(){  
     alert('alert');
  });
});

$.each(markers, function(i, marker){
  $(marker).click(function(){  
     alert('alert');
  });
});

UPDATE Ive tried adding this middle section to existing code:

        $.each(markers, function(i, marker){
            marker.setMap( checked ? map : null);

           //added code begins
            $(marker).click(function(){ 
                 alert('dfd');
              });
           //added code ends

        });

Answer

Diode picture Diode · Feb 28, 2012
google.maps.event.addListener(marker, 'click', function() {

});

ref: https://code.google.com/apis/maps/documentation/javascript/events.html#UIEvents

Example : http://jsfiddle.net/diode/yXQwp/