Accessing and altering objects in a loaded KML file (google earth plugin)
I am creating an example us开发者_C百科ing the google earth browser plugin and API. I am trying to load a kml file using the fetchKml method
function addKmlFromUrl(kmlUrl) {
google.earth.fetchKml(ge, kmlUrl, kmlFinishedLoading);
}
function kmlFinishedLoading(kmlObject)
{
if (kmlObject) {
ge.getFeatures().appendChild(kmlObject);
}
}
I am sucessfully loading the object, but once it is loaded, I want to be able to alter some of the parameters on the fly. I also want to be able to parse throug the points in it and do a few additional things. I looked through the documentation and code samples, but I can't seem to find a way to do this.
Any ideas?
The kmlObject has a DOM just like XML (or any SGML language) you would need to recursively iterate over the elements in the DOM and then alter the ones which are applicable to you. Unfortunatly there are no direct methods in the api for this so you would either need to cook yor own or use a library the has the functions you need.
Something like the following might work for you.
function addKmlFromUrl(kmlUrl) {
google.earth.fetchKml(ge, kmlUrl, kmlFinishedLoading);
}
function kmlFinishedLoading(kmlObject)
{
if (kmlObject) {
ge.getFeatures().appendChild(kmlObject);
traverseKml(kmlObject);
}
}
function traverseKml(node) {
if(node.getFeatures().hasChildNodes()) {
var subNodes = node.getFeatures().getChildNodes();
var length = subNodes.getLength();
for(var i = 0; i < length; i++) {
var eachSubNode = subNodes.item(i);
var nodeType = eachSubNode.getType();
switch(nodeType) {
// kml containers
case 'KmlFolder' :
case 'KmlDocument' :
traverseKml(eachSubNode);
break;
// do something with all placemarks...
case 'KmlPlacemark' :
alert(eachSubNode.getName());
break;
}
}
}
}
精彩评论