How to get all visible markers on current zoom level

nefo_x picture nefo_x · May 25, 2010 · Viewed 48.5k times · Source

Here are some points:

  1. I have some markers on the map and records associated with it on the right panel besides the map. They are connected via numeric id, which is stored as a property of marker.
  2. All the markers are stored in an array.
  3. When the user zooms in the map, records, associated to only visible markers should be shown on the right panel.

So, how to get the list of all visible markers on the current zoom level? I've searched over the internet and didn't find something useful. Some kind of what I'm trying to achieve could be found here

Answer

bruha picture bruha · Oct 14, 2011

In Google Maps JavaScript API V3 we can use something like this:

let markers; // your markers
let map; // your map
let bounds = map.getBounds() 
for (let i=0; i<markers.length; i++){
    if(bounds.contains(markers[i].getPosition()) ){
        // code for showing your object, associated with markers[i]
    }
}