-1

In my first click event all markers are shown in google map, I want to remove them in the second click. when I execute my code, only the last marker is removed.
this is my javascript code:

var showmarkers = false;
google.maps.event.addDomListener(hotel, 'click', function () {

        if (showmarkers == false) {

            showmarkers = true;
            for (var i = 0; i < len; i++) {
                //add makers 
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(results.rows.item(i).lat, results.rows.item(i).long),
                    map: map,
                    icon: icons[1],
                    animation: google.maps.Animation.DROP,

                });
                markers.push(marker);
            }


            //add infowindow
            google.maps.event.addListener(marker, 'click', (function (marker, i) {
                    return function () {
                        //if we create the infowindow here, all the windows 'll stay shown 
                        infowindow.setContent("<div style='background-color:red;'><h3>" + results.rows.item(i).nom + "</h3><br/><center>" + "<img src='" + results.rows.item(i).img + "' style='width:20px; height:20px;' /></center><br/></div>")
                        infowindow.open(map, marker);
                    }
                })
                (marker, i));

        } else {
            alert("false");
            showmarkers = true;
            marker.setMap(null);
        }
duncan
  • 31,401
  • 13
  • 78
  • 99
iteb khayati
  • 107
  • 1
  • 10
  • I think you need to add your markers into an array. The way you are doing it, it seems to me, is just overwriting the "marker" variable over and over, placing them on the map, but only leaving the last one available to change. – dmgig Apr 28 '14 at 17:26
  • I tried to replace "marker.setMap(null)" by "markers[i].setMap(null)" but nothing happened. I already added all markers in the array "markers" by doing this : "markers.push(marker)" – iteb khayati Apr 28 '14 at 17:34
  • It's the third question you open for the same problem. – MrUpsidown Apr 28 '14 at 23:35
  • because in the last one I didn't put the code in addition to that I didn't find a clear response. I'm sorry because I'm beginner :( – iteb khayati Apr 29 '14 at 00:07

1 Answers1

1
else {
alert("false");
showmarkers = true;
  for(var lp=0;lp<markers.length;lp++){
            markers[lp].setMap(null);
  }
}