Wednesday, February 15, 2012

Google Earth Web Plugin: Deleting placemarks

How do I delete placemarks on Google Earth web plugin?

The key would be, KML traversal. This piece of code removes a placemark from google earth. ge.getFeatures().removeChild(placemark);

Let's give a more complete example. Suppose I'd like to delete all the placemarks which does not have a 3D model.
var kmlObjectList = ge.getFeatures().getChildNodes();
for(var i = 0; i<kmlObjectList.getLength(); i++){
var item = kmlObjectList.item(i);
        //checks and see if it is a place mark and if the placemark does not have a 3D model.
  if(item.getType() == 'KmlPlacemark' && item.getGeometry().getType() != 'KmlModel'){
ge.getFeatures().removeChild(item);
}
}

The code above seems to be very slow, especially with large KML files, since it still has to pass several checks before you can actually remove the placemarks. But if let's say, you just want to remove everything from google earth, and as in everything, you can use the code below. It's much faster.
var features = ge.getFeatures();
  while (features.getFirstChild()) {
   features.removeChild(features.getFirstChild()); 
}

Tuesday, February 14, 2012

Google Earth Web Plugin: KML Traversal

How do I modify a KML file before displaying it to Google Earth Web Plugin?
According to the Google Earth API, you can modify KML files before or after displaying them on the Earth by: 
1. fetching the KML file
2. traversing its DOM
3. finally altering stuff that you want


Let's assume that we have this KML file:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document><Folder><name>POLES</name>
  <Placemark>
      <name>0115430</name>
      <Point><coordinates>123.89578580810728,10.319223048287485,89.251724109053612</coordinates></Point>
  </Placemark>
  <Placemark>
      <name>0115444</name>
      <Point><coordinates>123.8955885882454,10.319175821957716,89.609039922244847</coordinates></Point>
  </Placemark>
  <Placemark>
      <name>0115430</name>
      <Point><coordinates>123.8956338841908,10.319157360055167,89.548814755864441</coordinates></Point>
  </Placemark>
  <Placemark>
      <name>0115493</name>
      <Point><coordinates>123.89518425542499,10.318997879222097,89.596587631851435</coordinates></Point>
  </Placemark>
  <Placemark>
      <name>0115486</name>
      <Point><coordinates>123.89510651027278,10.319166037410572,89.483854516409338</coordinates></Point>
  </Placemark>
  <Placemark>
      <name>0115472</name>
      <Point><coordinates>123.89492175886454,10.319134054194818,89.526313836686313</coordinates></Point>
  </Placemark>
</Folder></Document></kml>

Suppose, you want to add a 3D model instead of placemarks inside your KML file. All you have to do is fetch, traverse and modify. Take a look at the code below for an example:
/*experiment by kreigh*/ 
function render3DPoles(){
var polesKmlLink = 'http://205.196.120.211/9t6bqwy56phg/006b3b9p7o98lxp/POLES3.kml';

        google.earth.fetchKml(ge,polesKmlLink, function(object){
if(object){
var item = object.getFeatures().getChildNodes().item(0);
      var folder = item.getFeatures().getChildNodes();

var latitude;
var longitude;
     
     for(var i = 0; i<folder.getLength(); i++){

var placemark = folder.item(i);

latitude = placemark.getGeometry().getLatitude();
longitude = placemark.getGeometry().getLongitude();
     
      var model = ge.createModel('');
var location = ge.createLocation('');

model.setLocation(location);
var link = ge.createLink('');


link.setHref('insert URL of collada file here');
model.setLink(link);
     
      location.setLatitude(latitude);
      location.setLongitude(longitude);
      model.setLocation(location); 
      placemark.setGeometry(model);

ge.getFeatures().appendChild(placemark);
}
   
var la = ge.createLookAt('');
la.set(latitude,longitude, 25, ge.ALTITUDE_RELATIVE_TO_GROUND,180, 60, 100);
ge.getView().setAbstractView(la);

} else {
   setTimeout(function() {
     alert('Bad or null KML.');
     },0);
  }
});
}