Google Earth API

Camera Control

  1. Introduction
  2. Getting the current view
  3. Pan
  4. Tilt
  5. Zoom
  6. Setting the FlyTo speed

Introduction

The 'view' in Google Earth is the image you see in the plugin window; the 'camera' is the viewer's location in space. There are two different ways to define a view:

  • Camera: with the Camera view, the point you specify defines the location of the viewer in space. Setting a latitude and longitude moves the viewer to a specific location; the viewer can then be hoisted into space (or underwater) and rotated around the x, y, and z axes until the view is as desired.
  • LookAt: with a LookAt, the point being specified in the code is the point being viewed. Changing the range, tilt, and heading values will never change the absolute point in space at which the view is aimed.

You can read more about views in the KML developer's guide Cameras documentation.

Getting the current view

You can use the copyAsLookAt() function to return the latitude and longitude of the point at which the camera is looking, the altitude at which the camera is set, its tilt, and the compass heading in degrees. Below, the returned values are passed to a new lookAt object:

var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

Similarly, the view can be returned as a Camera value:

var camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);

Panning the camera

The camera can be moved to either an absolute location, defined by a Camera or LookAt, or to a position relative to its current view.

Panning to an absolute location

Locations on the globe are specified by their latitude and longitude values. The Google Earth Plugin accepts values with up to six digits of precision (0.123456).

To move from the current view to an absolute location, while retaining the tilt, range, heading, and altitude values:

// Get the current view.
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Set new latitude and longitude values.
lookAt.setLatitude(36.584207);
lookAt.setLongitude(-121.754322);

// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);

Alternatively, you can define a new LookAt or Camera object, then set the new object as the view:

// Create a new LookAt.
var lookAt = ge.createLookAt('');

// Set the position values.
lookAt.setLatitude(36.584207);
lookAt.setLongitude(-121.754322);
lookAt.setRange(5000.0); //default is 0.0

// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);

Panning relative to the current view

The view can be moved in any direction relative to its current position. The following example moves the camera north by 25 degrees longitude and east by 25 degrees latitude:

// Get the current view.
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Add 25 degrees to the current latitude and longitude values.
lookAt.setLatitude(lookAt.getLatitude() + 25.0);
lookAt.setLongitude(lookAt.getLongitude() + 25.0);

// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);

Tilting the camera

A LookAt can contain a tilt value between 0 and 90 degrees inclusive, with 0 being directly above the viewed point, and 90 viewing along the horizon.

A Camera can contain a tilt value between 0 and 360 degrees. 0 degrees is a view directly down from the specified point; 90 sets a view along the horizon; 180 views directly up into the sky. In addition, a Camera accepts a roll attribute, which rotates the viewer around the z axis.

Tilting a LookAt:

// Get the current view.
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Add 15 degrees to the current tilt.
lookAt.setTilt(lookAt.getTilt() + 15.0);

// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);

Tilting a Camera:

// Get the current view.
var camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Add 30 degrees to the current tilt
// and 45 to the roll.
camera.setTilt(camera.getTilt() + 30.0);
camera.setRoll(camera.getRoll() + 45.0);

// Update the view in Google Earth.
ge.getView().setAbstractView(camera);

Zooming the camera

Zooming in and out is controlled by the range attribute for a LookAt, and the altitude attribute for a Camera.

Note that changing the altitude attribute of a LookAt changes the altitude of the point being viewed. Because the viewer range is relative to this point, the viewer's altitude is also changed.

LookAt:

// Get the current view.
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Zoom out to twice the current range.
lookAt.setRange(lookAt.getRange() * 2.0);

// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);

Camera:

// Get the current view.
var camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Zoom out to twice the current distance.
camera.setAltitude(camera.getAltitude() + 5000);

// Update the view in Google Earth.
ge.getView().setAbstractView(camera);

Setting the FlyTo speed

You can control the speed at which the plugin moves to newly-specified locations. GEOptions.setFlyToSpeed() accepts a float from 0.0 to 5.0, inclusive, as well as SPEED_TELEPORT. Speed increases with the value; SPEED_TELEPORT moves to the specified location instantly.

// Set the FlyTo speed.
ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);

// Get the current view.
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Add 15 degrees to the current tilt.
lookAt.setTilt(lookAt.getTilt() + 15.0);

// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.