Google Earth Determining zoom level from bounding box
Asked Answered
S

1

7

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!!

Supersession answered 14/7, 2010 at 15:30 Comment(0)
L
7

To "zoom to the polygon" or any other KmlObject it is probably easiest to use the earth-api-utility-library.

See the methods createBoundsView, setToBoundsView and especially flyToObject

For example, where ge is the GEPlugin object and polygon is a KmlPolygon.

var gex = new GEarthExtensions(ge);
gex.util.flyToObject(polygon, { boundsFallback: true, aspectRatio: 1 } );
Lemures answered 19/7, 2010 at 2:33 Comment(2)
There are a couple issues with this. First off, to find half the chord length you should use var dist = r * Math.sin(d / 2) since the radius is the hypotenuse (not the adjacent side) of the relevant triangle. Second, the average latitude/longitude you use as the center will not be correct in many cases (e.g. across "more than" a hemisphere number line–wise).Swithin
To be fair I never really used it, it is from here: groups.google.com/forum/?fromgroups#!topic/… - I would actually now suggest using the earth-api-utility-library [ code.google.com/p/earth-api-utility-library/wiki/…Lemures

© 2022 - 2024 — McMap. All rights reserved.