开发者

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

开发者 https://www.devze.com 2023-03-07 15:38 出处:网络
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()

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开发者_Python百科 map.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消