I have several polygons drawn on the surface in google earth plugin. I generate a KML from them.
Question is how can I calculate parameters for the LookAt so that the view includes all the polygons when open in the desktop Google Earth?
For lat and lon it’s quite simple – just calculate the center of bounding box, but what about altitude and range parameters?
For example we have two polygons:
1:
40.724536 -74.001914
40.724102 -74.001091
40.723003 -74.002067
40.723392 -74.002891
2:
40.723186 -74.003112
40.722843 -74.002311
40.721977 -74.003036
40.722320 -74.003738
Then lon and lat will be
lon=(40.724536+40.721977)/2=40.7232565
lat=-(74.001091+74.003738)/2=-74.0024145
Now how to calculate the range and altitude?
Take a look at the Google Earth API Utility Library. It augments the functionality of the Earth API providing useful helper methods which wrap up commonly needed tasks for you.
It includes a createBoundsView() method which will build you a
LookAtobject configured to fit a given bounding box within the viewport. Does exactly what you need I think. e.g.