Google Maps API setBounds of object

1.6k views Asked by At

How do I set the bounds of an existing object (Like a ground overlay, rectangle, etc.) in Google Maps API v3?

For example:

// Assuming a map and a rectangle have already been created.

var newBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-20,-10),
    new google.maps.LatLng(50,30)
);
rectangle.setBounds(newBounds);

Is there an equivalent to setBounds()?

Note: I could change the recangle's bounds array directly, but as Google keeps changing their variable keys for bounds this would eventually break the script.

2

There are 2 answers

0
Laurent On BEST ANSWER

I have found the solution by taking a look at the GroundOverlay constructor:

var groundOverlay = ...

var newBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-20,-10),
    new google.maps.LatLng(50,30)
);

groundOverlay.set("bounds",newBounds);
groundOverlay.map_changed();
0
kubante On

If - for any reason - you should be interested in having the values to store them and have them assigned to your own key specification (avoiding the problem of google changing the keys of the object) you can use the .toString() function. Working for both, bounds or coordinates objects.