I've obtained a PlaceResult
object based on a location search, and now I would like to fly to the recommended Viewport
for that location.
Here is what I have so far:
function flyToLocation() {
var place = autocomplete.getPlace();
var geometry = place.geometry;
var location = geometry.location;
var viewport = geometry.viewport; // {fa: {b: 70, d: 40}, la: {b: 27, d: 179}}
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
lookAt.setLatitude(location.b);
lookAt.setLongitude(location.d);
lookAt.setTilt(lookAt.getTilt() + 15.0);
lookAt.setRange(500); // todo base this off of viewport
ge.getView().setAbstractView(lookAt);
ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);
}
This will aim the camera at the correct location, but the range is currently hard coded. I would like to base the range off the viewport, but I'm not quite sure how.
Questions
The code must exist somewhere because the Google Earth desktop app does this perfectly, but I can't seem to find it in the API.
If you want to translate the Viewport
(LatLngBounds) to something that can be displayed in the Google Earth plugin I would recommend the GEarthExtensions Library.
With it you can create a bounds object based on the viewport and then set that to the current view. e.g.
// the viewport is simply the SouthWest and NorthEast points.
var viewport = geometry.viewport;
// create a geo.bounds based on the viewport
var bounds = new geo.Bounds(viewport.getSouthWest(), viewport.getNorthEast());
// set the bounds (viewport) to the GEPlugin view
gex.view.setToBoundsView(bounds, { aspectRatio: 1.0 });
Here is a fully working example.
"The code must exist somewhere because the Google Earth desktop app does this perfectly"
Just to note, simply because some functionality exists in one, it doesn't mean that it is available in the other. Indeed, there are lots of features in the full application not available to the plugin.