Google Earth API Samples - Smooth Animation with frameend

Last Modified:
06/08/2009
Installed Plugin Version:
...

Relevant Resources:

Relevant Code Excerpt:

var animRunning = false;
var ANIM_ALTITUDE = 100;
function startAnimation() {
  if (!animRunning) {
    ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);
    animRunning = true;
    google.earth.addEventListener(ge, 'frameend', tickAnimation);
    
    // start it off
    tickAnimation();
  }
}

function stopAnimation() {
  if (animRunning) {
    google.earth.removeEventListener(ge, 'frameend', tickAnimation);
    animRunning = false;
  }
}

function tickAnimation() {
  // an example of some camera manipulation that's possible w/ the Earth API
  var camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
  var dest = destination(camera.getLatitude(), camera.getLongitude(), 10,
                         camera.getHeading());

  camera.setAltitude(ANIM_ALTITUDE);
  camera.setLatitude(dest[0]);
  camera.setLongitude(dest[1]);

  ge.getView().setAbstractView(camera);
}

/* Helper functions, courtesy of
   http://www.movable-type.co.uk/scripts/latlong.html */
function distance(lat1, lng1, lat2, lng2) {
  var a = Math.sin(lat1 * Math.PI / 180) * Math.sin(lat2 * Math.PI / 180);
  var b = Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
      Math.cos((lng2 - lng1) * Math.PI / 180);
  return 6371000 * Math.acos(a + b);
}

function destination(lat, lng, dist, heading) {
  lat *= Math.PI / 180;
  lng *= Math.PI / 180;
  heading *= Math.PI / 180;
  dist /= 6371000; // angular dist
  
  var lat2 = Math.asin(Math.sin(lat) * Math.cos(dist) + 
                       Math.cos(lat) * Math.sin(dist) * Math.cos(heading));

  return [
      180 / Math.PI * lat2,
      180 / Math.PI *
        (lng + Math.atan2(Math.sin(heading) * Math.sin(dist) * Math.cos(lat2),
                          Math.cos(dist) - Math.sin(lat) * Math.sin(lat2)))];
}