How to find out the coordinates of the corners of the visibility zone of the Google maps api map

The problem is that there are over 999999 markers on the map, you need to pull from the base only those that are in the field of view, respectively, the latitude and longitude are in the range of the left/right upper/lower corner. But how do I get the coordinates of the angles of the visibility zone? Google api looked, there is nothing suitable. Working with js.

Author: Den Rupp, 2015-09-24

1 answers

I think the .getBounds() method is what you need.

Below is a code that may be useful to you.
When you change the map borders (zoom or just move the map), the code checks all the markers (array markers) and pushes the ones inside the visible window into the object visibles.

map.addListener('bounds_changed', function() {
        visibles = {};
        for (var i=0; i < markers.length; i++) {
            if (map.getBounds().contains(markers[i].getPosition())) {
                visibles[i] = markers[i];
            }
        }
}

Remark: please note that the above code may not work correctly if you have marker clustering enabled

 1
Author: cyadvert, 2015-09-24 16:20:22