2012-12-02 30 views
1
KML元素

也许有人有想法,为什么功能getObjectById(ID)不会在谷歌地球上工作?我已经将kml提取到DOM,但是我尝试使用id找到元素,但它不返回任何内容。编辑获取由ID

我在玩在谷歌地球API操场,也许它不支持额外的lybrary?

这里是我的代码:

<script src="http://www.google.com/jsapi?key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script> 
    <script src="http://earth-api-utility-library.googlecode.com/svn/tags/extensions-0.2.1/dist/extensions.pack.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

................ 


// fetch the KML 
     var url = 'http://kml-samples.googlecode.com/svn/trunk/kml/NetworkLink/placemark.kml'; 
     google.earth.fetchKml(ge, url, finished); 
    } 

    function failureCallback(errorCode) { 
    } 

    function getObjectById(id) { 
    var foundObject = null; 

    // Traverse the DOM, looking for the object with the given ID. 
    gex.dom.walk({ 
    rootObject: ge, 
    visitCallback: function() { 
     if ('getId' in this && this.getId() == id) { 
     foundObject = this; 
     return false; // Stop the walk. 
     } 
     } 
     }); 

     return foundObject; 
    } 

    function buttonClick() { 
     getObjectById("p"); 
     alert("Found:" +foundObject); 
    } 

KML被加载时,按钮的作品,但似乎getObjectById( “P”);不工作...

回答

1

首先,我不认为这是一个getObjectById(),你很可能需要为使用getElementByUrl()getElementById()

由于您使用fetchKml,我认为你应该使用getElementByUrl()像这样的:

var url = 'http://kml-samples.googlecode.com/svn/trunk/kml/NetworkLink/placemark.kml'; 
var object = ge.getElementByUrl(url + '#' + id); 

See this page for details

+0

我根据这个做:访问谷歌地球DOM(https://developers.google.com/earth/articles/domtraversal) – sauletasmiestas

+0

你正确加载gex?即在你的init(实例)函数中,你是先加载ge,然后是gex,例如:ge = instance; gex = new GEarthExtensions(ge); – lifeIsGood

+0

为什么不使用我在回答中提到的功能 - 它们被默认插件,因此无需加载扩展支持(除非你在其他地方使用它们) – lifeIsGood