Here's what I use to set the view to contain a polygon (or rather, the
bounds of the polygon). map_div is the div containing the earth, s,w,n
and e are the south, west, north and east coords of the bounding box:
var sw = new google.maps.LatLng(s,w);
var ne = new google.maps.LatLng(n,e);
var b = new google.maps.LatLngBounds(sw, ne);
var c = b.getCenter();
var d1 = sw.distanceFrom(ne);
var wid = map_div.offsetWidth;
var hig = map_div.offsetHeight;
if (wid > hig) {
d1 = d1*wid/hig;
} else {
d1 = d1 * hig/wid;
}
var cam = ge.createCamera('');
cam.set(c.lat(), c.lng(), d1*.9,
ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 0);
ge.getView().setAbstractView(cam);
It relies on the google maps api, which you may not have loaded. Thats
mainly because I already need it, and it makes it takes care of
wrapping around the dateline, and computing the distance between two
points.
The former is straightforward (and depending on your data, you may not
have to worry about it). You could replace the distance calculation
with something like (note this is an approximation, but should be fine
when used in the above):
var d2r = Math.atan2(1,1)/45;
var r = 6378100;
var h = (n-s);
var w = (e-w)* cos((n+s)*d2r/2);
var d1 = Math.sqrt(h*h+w*w)*d2r * r;
Mark