Flupkear
Flupkear

Reputation: 2231

How to iterate markers on a Google maps (API 3)

I have the following map:

var map = new google.maps.Map(document.getElementById('map'), myOptions);

and a couple of markers created calling to new google.maps.Marker()

Now I need to hide all the markers of a certain group when a checkbox is clicked but can't find a way to iterate through all the markers on the map.

Upvotes: 1

Views: 9013

Answers (2)

Alsaket
Alsaket

Reputation: 175

var gmarkers = Array();
.
.
 for( i = 0; i < [your locations array].length; i++ ) {
        var position = new google.maps.LatLng([your locations array].lat, [your locations array].long);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i].title 
        });
gmarkers.push(marker);
}
.
.
.
// hide all the markers 
for(i = 0 ; i< gmarkers.length; i++) gmarkers[i].setVisible(false);

Upvotes: 4

planetjones
planetjones

Reputation: 12633

It's up to you to keep track of all markers on a Google Map. Google API does not keep track of all overlays you have added. When you create the Marker Objects add them to an array (different arrays for different groups). Then iterate through that specific array and hide all markers in that group, when the appropriate event is triggered.

Upvotes: 9

Related Questions