Question

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);
        }
Was it helpful?

Solution

else {
alert("false");
showmarkers = true;
  for(var lp=0;lp<markers.length;lp++){
            markers[lp].setMap(null);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top