Page 1 of 1

Retrieving the bounds of a google map

Posted: Wed Feb 25, 2015 12:19 pm
by Andy Parker

Hi,

I have a map named "map1", this shows various markers and clicking these markers causes an event.

To ensure I only load the markers that would be visible on the map, I need to know the bounds of the map (the user has the ability to zoom in and out, and move the map around, so I have to presume the bounds are variable).

I can't seem to work out how to retrieve the bounds of the google map...I have tried the following without success:

Code: Select all

 var bounds = map1.getBounds(); 

or
var bounds = new google.maps.LatLngBounds(); - but this doesnt return my maps bounds....

Can you help please?

many thanks,

Andy.


Retrieving the bounds of a google map

Posted: Wed Feb 25, 2015 1:09 pm
by Andy Parker

Don't worry, worked out how to do this - solution below for anyone else needing the bounds:

google.maps.event.addListener(map1, 'bounds_changed', function() {
window.mapbounds=map1.getBounds();

this essentially sets the global variable -mapbounds to what ever the bounds of the map are, even if the user scrolls or zooms the map :-)


Retrieving the bounds of a google map

Posted: Wed Feb 25, 2015 1:20 pm
by Maryna Brodina

Hello Andy!

Thank you for update!