jquery - Get a list of markers in bounds of google map using markerclusterer v3 -


i have google map populated data database. using markerclusterer v3 cluster markers make map load faster. have scoured docs , cannot seem find way listing of markers in map bounds. trying create external list of markers addresses in external div. on page load appends whole list of addressees. have markers, , markers contained in clusters appear on map @ time appear in list. @ zoom 13 there 6 clusters 3 in each , 1 solo marker. @ zoom 14 there 3 clusters of 3 , 2 solo markers. @ zoom 13 there 19 addresses in list , @ zoom 14 there 11 addresses in list. list of markers in bounds of map. load addresses once on initial map creation. thought of creating ajax post server on each zoom, thought little unnecessary have server call on each map movement. there has way list of markers in bounds controlled markerclusterer.

.js

var markers = [];        (var = 0; < 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);  } var map; var myoptions = {     zoom: 13,     maptypeid: google.maps.maptypeid.roadmap }; function init() {     map = new google.maps.map(document.getelementbyid("map"), myoptions);     var markercluster = new markerclusterer(map, markers); } (var i=0; < markers.length; i++) {     if (map.getbounds.contains(markers[i])) {          console.log(markers);     } else {          console.log('failed');     } }  google.maps.event.adddomlistener(window, 'load', init);  

  1. make markers array global.
  2. make map variable global (or @ least in scope).
  3. run code:

    for (var i=0; < markers.length; i++)  {     if map.getbounds().contains(markers[i].getposition())     {         // markers[i] in visible bounds     }      else      {         // markers[i] not in visible bounds     } } 

update: may need (if trying add loop init function). other options (you haven't been real clear why want this):

  1. the marker test in initial loop adds markers.
  2. to use addlisteneronce rather addlistener.
  3. you may want re-render "listofitems" whenever map zoomed or panned (when 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);     (var = 0; < 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()      {         (var = 0; < markers.length; i++)          {             if (map.getbounds().contains(markers[i].getposition()))              {                 // markers[i] in visible bounds             }              else              {                 // markers[i] not in visible bounds             }         }     }); }  google.maps.event.adddomlistener(window, 'load', init); 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -