GMaps v3 Markers AddListener Getting always the last variable index in a for loop

GMaps v3 Markers AddListener Problem

I am trying to add a mouseover / mouseout event listener to my markers, but I always get the last value of the for loop. In all events, it seems that the latter value is for instead of the current one. Here is my code

for( mark in data ) {
    markers[mark] = new google.maps.Marker({
              position: new google.maps.LatLng(data[mark].lat,data[mark].lng), map: map,
            });
google.maps.event.addListener(markers[mark], "mouseover", function() {
                alert(mark);
            });
            google.maps.event.addListener(markers[mark], "mouseout", function() {
                alert(mark);
            });
        }

The result is a mouse transition / mouseout warning with the same value for all 10 markers, and I expected the marker identifier in each warning.

Thanks Relations

+5
source share
1 answer

, , - , , . ​​ . , ( ):

 function createMarker(latlng, id)
 {
    var marker= new google.maps.Marker({
          position: latlng, map: map,
          });
    google.maps.event.addListener(marker, "mouseover", function() {
            alert(id);
          });
    google.maps.event.addListener(marker, "mouseout", function() {
            alert(id);
          });
    return marker;
 }
 for( mark in data ) {
   markers[mark] = createMarker(new google.maps.LatLng(data[mark].lat,data[mark].lng),
                                mark);
 }
+10

All Articles