Matthew Lock
Matthew Lock

Reputation: 13556

How do setBounds in Google Maps API v2?

I'm upgrading some code written for Google Maps API v2, and I wish to set the bounds of the map (top, left, bottom, right), rather than the centre.

I notice that there's a GMap2.getBounds but I can't seem to find a method which allows me to set the bounds.

How can I do this in Google Maps?

Upvotes: 1

Views: 3329

Answers (1)

Matthew Lock
Matthew Lock

Reputation: 13556

Found a way to do a setBounds here: http://www.mymapofjapan.com/blog/setting-size-and-zoom-level-of-maps/

var bounds = new GLatLngBounds; 
bounds.extend(new GLatLng( South, West )); 
bounds.extend(new GLatLng( North, East )); 
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

Because Google Maps has discrete zoom levels you probably won't be able to set it to exactly the bounds you wanted, so you might need to call map.getBounds(); afterwards to show the user the actual bounds they are looking at.

Upvotes: 4

Related Questions