0

I'm using the below code but i'm little confuse with click event. How, can i add click event on every marker??

        var address = 'Dubai';
     var neighborhoods = [
       new google.maps.LatLng(25.23247465817403, 55.30191340351564),
       new google.maps.LatLng(25.244586082480332, 55.29822268391115),
       new google.maps.LatLng(25.230844181976337, 55.32225527668459),
       new google.maps.LatLng(25.224787936110832, 55.28526224995119)
     ];
     var markers = [];
     var iterator = 0;
     var map;
     var geocoder = new google.maps.Geocoder();
     function initialize() {
         var mapOptions = {
             zoom: 12,
         };
         map = new google.maps.Map(document.getElementById('mapCanvas'),
                 mapOptions);
         geocoder.geocode({
             'address': address
         },
         function (results, status) {
             if (status == google.maps.GeocoderStatus.OK) {
                 map.setCenter(results[0].geometry.location);
             }
         });
     }
     function drop() {
         for (var i = 0; i < neighborhoods.length; i++) {
             setTimeout(function () {
                 addMarker();
             }, i * 200);
         }
     }
     function addMarker() {
         var image = 'img/flagred.png';
         markers.push(new google.maps.Marker({
             position: neighborhoods[iterator],
             map: map,
             icon: image,
             title:"Hello World!",
             draggable: false,
             animation: google.maps.Animation.DROP
         }));
         iterator++;
     }

I have looked at the other answers here but that didnt solve my problem.

Rohit Khurana
  • 269
  • 3
  • 8
  • 18

1 Answers1

6

Simply add it to every marker you add to your map. So change the addMarker-definition to

function addMarker() {
     var image = 'img/flagred.png';
     var marker = new google.maps.Marker({
         position: neighborhoods[iterator],
         map: map,
         icon: image,
         title:"Hello World!",
         draggable: false,
         animation: google.maps.Animation.DROP
     });
     markers.push(marker);

     google.maps.event.addListener(marker, 'click', function() {
         // your magic goes here
     });

     iterator++;
 }
Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29