简体   繁体   English

访问和更改已加载的KML文件中的对象(Google Earth插件)

[英]Accessing and altering objects in a loaded KML file (google earth plugin)

I am creating an example using the google earth browser plugin and API. 我正在使用Google Earth浏览器插件和API创建示例。 I am trying to load a kml file using the fetchKml method 我正在尝试使用fetchKml方法加载kml文件

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. kmlObject具有与XML(或任何SGML语言)相似的DOM,您需要递归地迭代DOM中的元素,然后更改适用于您的元素。 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. 不幸的是,在api中没有直接的方法,因此您需要自己做饭或使用具有所需功能的库。

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; 
      } 
    } 
  } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM