Zoom-to-fit in Google Maps

I am currently extensively using the Google Maps API for a project.

Their API is great, but good documentation is often lacking. More often than not, I have to google around to find some hints.

Anyway, here’s a little tip for some simple Javascript code in Google Maps API (v2) to zoom-to-fit the map for the best fit on a series of points you use for markers.

function fitMap( map, points ) {
   var bounds = new GLatLngBounds();
   for (var i=0; i< points.length; i++) {

The code takes the map (GMap2) object to target and an array of points (GLatLng)  you want to fit the window for. It uses the undocumented method getBoundsZoomLevel().