views:

66

answers:

1

Hi guys,

I got a Windows Forms app making use of Google Earth where users can draw a polygon on the map which is used as a geofence.

What I'd like to do is to be able to zoom to the polygon so that it fits nicely on screen with a click of a button. A sort of zoom to fit function.

Finding the centre of the polygon and setting the Google Earth camera to that lat/long is easy.

What I need is an algorithm that takes a bounding box of lats \ longs, screen height \ width and then determines the altitude to set the camera.

Does anyone have this algorithm or know where one can be found?

Thanks!!

+2  A: 

The best place to find this kind of information is on the Google Earth Api Forum. That said the following active scripting method from there should be exactly what you need.

function createCameraFromRectangle(plugin, lat1, lng1,  lat2, lng2) { 
     // Approx. Radius of the earh.
    var r = 6378700;

    // Field of view
    var fov = 32; 

    var camera = plugin.createCamera(''); 
    camera.setLatitude((lat1 + lat2) / 2.0); 
    camera.setLongitude((lng1 + lng2) / 2.0); 
    camera.setHeading(0.0); 
    camera.setTilt(0.0); 

    // determine if the rectangle is portrait or landscape
    var dy = Math.max(lat1, lat2) - Math.min(lat1, lat2); 
    var dx = Math.max(lng1, lng2) - Math.min(lng1, lng2); 

    // find the longest side
    var d = Math.max(dy, dx); 

    // convert the longest side degrees to radians
    d = d * Math.PI/180.0; 

    // find half the chord length
    var dist = r * Math.tan(d / 2); 

    // get the altitude using the chord length
    var alt = dist/(Math.tan(fov * Math.PI / 180.0)); 

    camera.setAltitude(alt); 
    return camera; 
} 
Fraser