0

Is there a way to do this instruction on google map v3 ? I have a button on the map, I want ,on the first click, to show markers and ,on the second, to remove them from map. Thank you for advance.

//Add hotel's markers and infowindows to the map

google.maps.event.addDomListener(hotel, 'click', function() {
             for (var i = 0; i < len; i++) { 
                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);


            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));


            }


        });
iteb khayati
  • 107
  • 1
  • 10

1 Answers1

0
  1. Create a global showMarkers variable
  2. Create a function toggleMarkers and call it when you click on the button

Here is a quick example:

var showMarkers = false;

function toggleMarkers() {

    if (showMarkers === false) {

        for (var i=0; i<markers.length; i++) {

            markers[i].setMap(map);
        }

        showMarkers = true;

    } else {

        for (var i=0; i<markers.length; i++) {

            markers[i].setMap(null);
        }

        showMarkers = false;
    }    
}
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • Plz,Where can I put this code exactly ? – iteb khayati Apr 28 '14 at 16:13
  • `showMarkers` : outside any function, at the beginning of your code. The function itself, anywhere in your code, outside any other function. To bind the function to the button click: on document ready. – MrUpsidown Apr 28 '14 at 16:15
  • I called this function inside the : google.maps.event.addDomListener(hotel, 'click', function() {}); but it didn't walk :( – iteb khayati Apr 28 '14 at 16:33