- Make your markers array global.
- Make your map variable global (or at least in scope).
Run this code:
for (var i=0; i < markers.length; i++) { if (map.getBounds().contains(markers[i].getPosition())) { // markers[i] in visible bounds } else { // markers[i] is not in visible bounds } }
Update: You may need to do this (if you are trying to add the loop to your init function). Other options (you haven't been real clear about why you want to do this):
- the marker test in your initial loop that adds the markers.
- to use addListenerOnce rather than addListener.
you may want to re-render the "listOfItems" whenever the map is zoomed or panned (when the bounds changes again)
var map = null; var markers = []; function init() { var myOptions = { zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map"), myOptions); for (var i = 0; i < data.coords.length; i++) { var dataInd = data.coords[i]; var latLng = new google.maps.LatLng(dataInd[1],dataInd[2]); var marker = new google.maps.Marker({ position: latLng }); markers.push(marker); $('#listOfItems').append('<li>' + latlng + '</li>'); } var markerCluster = new MarkerClusterer(map, markers); google.maps.event.addListener(map, 'bounds_changed', function() { for (var i = 0; i < markers.length; i++) { if (map.getBounds().contains(markers[i].getPosition())) { // markers[i] in visible bounds } else { // markers[i] is not in visible bounds } } }); } google.maps.event.addDomListener(window, 'load', init);