Google Earth API

Accessors

  1. getUrl()
  2. getComputedStyle()
  3. getElementByUrl()
  4. getElementById()
  5. getElementsByType()

getUrl()

An object's URL is its base address concatenated with its ID using the # character. This URL can be returned by calling getUrl() on the object.

var placemarkUrl = placemark.getUrl();

Objects created with the API (rather than imported as KML) do not have a base address; their URL will consist of a # character and their ID.

getComputedStyle()

Returns the object's style properties as a KMLStyle object, merging any inline styles with styles imported from setHref() or a StyleUrl.

var placemark = ge.createPlacemark('');
placemark.setStyleUrl(
  'http://kml-samples.googlecode.com/svn/trunk/kml/Style/styles.kml#blueIcons');

var icon = ge.createIcon('');
icon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
var style = ge.createStyle('');
style.getIconStyle().setIcon(icon);
style.getIconStyle().setScale(5);
placemark.setStyleSelector(style);

var placemarkStyle = placemark.getComputedStyle();

alert('Icon color: '
      + placemarkStyle.getIconStyle().getColor().get()
      + '\n'
      + 'Icon scale: '
      + placemarkStyle.getIconStyle().getScale());

getElementByUrl()

Objects that are imported as KML have an identifying URL consisting of their base address and ID, joined with the # character. For example, if http://code.google.com/foo.kml is fetched and contains the following:

<kml>
  <Document>
    <Placemark id='elvis_was_here'>
    ...
    </Placemark>
  </Document>
</kml>

The placemark can be retrieved with:

var pm = ge.getElementByUrl('http://code.google.com/foo.kml#elvis_was_here');

If the object was created with the API, it can be retrieved by including only the hash and the object's ID:

ge.getElementByUrl('#mike_was_there');

Alternatively, API-created objects can be returned using getElementById().

getElementById()

When an object is created with the API, rather than imported as KML, the object does not have a base address. In this case, the object can be returned by passing only its ID to getElementById().

var overlay = ge.createPhotoOverlay('graceland_panorama');
ge.getFeatures().appendChild(overlay);
var myPhotoOverlay = ge.getElementById('graceland_panorama');

getElementsByType()

You can obtain an array of all elements of a certain type, by passing that type as a string to getElementsByType().

var placemarks = ge.getElementsByType('KmlPlacemark');
for (var i = 0; i < placemarks.getLength(); ++i) {
  var placemark = placemarks.item(i);
}

This method can also be called on any KMLFeature object, such as a folder, to return only objects within that specific container.

For a list of valid types, refer to the API Reference. You can also call getType() on any existing object to return its type as a string.

alert(polygon.getType());

Authentication required

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

Signing you in...

Google Developers needs your permission to do that.