Google Earth API中基于地区的网络链接

时间:2012-08-02 01:22:07

标签: kml google-earth-plugin

我有许多大型KML数据集,它们使用基于区域的网络链接层次结构提供服务;如KML reference

中所述
  

将Regions与NetworkLinks结合使用,可以创建指针层次结构,每个指针指向一个特定的子区域。 <viewRefreshMode>,如以下KML文件所示,具有onRegion选项,该选项指定仅在Region处于活动状态时加载Region数据。如果您提供具有多个详细级别的嵌套区域,则仅当用户的视点触发下一次加载时才会加载更大量的数据。

在Google地球中加载时效果很好。

我现在希望使用Google Earth plug-in在应用程序中加载它们。 我需要通过Google Earth API访问加载的内容; (即附加单击事件,更改样式)以将内容集成到应用程序中。

问题是,我没有找到任何关于网络链接的'on-load'事件的引用。在我看来,这将起作用的方式是:

  • 通过API加载顶级网络链接,附加一个回调函数,该函数将在加载网络链接时调用。
  • 在回调函数中,解析网络链接返回的KML。对于区域层次结构中的中间级别,此KML将仅包含指向下一个区域级别的网络链接。通过API将它们加载到插件中,再次指定相同的回调函数,当加载它们时(即当它们的区域变得可见时)将调用它。
  • 最终,返回的KML将包含实际的“内容”。在此阶段,我们在执行任何所需的修改(例如附加事件监听器,设置样式等)后,将实际内容(即地标)加载到插件中。

我认为javascript看起来像下面这样 请注意:这只是一个草图,可能有助于理解我的问题。问为什么这段代码不起作用。

//create network link
var networkLink = ge.createNetworkLink("");
networkLink.setName("Regionated hierarchy root");

// create a Link object
//the network-links contained in the kml that will be returned in this file
//are region-based; they will only be loaded when the user zooms into the relevant
//region. 
var link = ge.createLink("");
link.setHref("http://foo.com/regionatedRoot.kml");

// attach the Link to the NetworkLink
networkLink.setLink(link);

//specify the callback function to be invoked when the network link is loaded
//this is is the part that doesn't actually exist; pure fiction...
networkLink.onLoad = networkLinkLoaded;

// add the NetworkLink feature to Earth
ge.getFeatures().appendChild(networkLink);

// function which will be invoked when a network-link is loaded
// i.e. when its region becomes active
function networkLinkLoaded(kml) {

   //parse the kml returned for child network links,
   //this will create the network link KmlObject, with a 
   //region specified on it.
   for (childNetworkLink in parseNetworkLinks(kml)) {
      //and append them, again hooking up the call-back
      childNetworkLink.onLoad = networkLinkLoaded;
      ge.getFeatures().appendChild(childNetworkLink);
   }

   //if the user has zoomed in far enough, then the kml returned will
   //contain the actual content (i.e. placemarks).
   //parse the kml returned for content (in this case placemarks)
   for (placemark in parsePlacemarks(kml)) {
      //here we would attach event-listeners to the placemark
      ge.getFeatures().appendChild(placemark);
   }
}

这可能吗?
我的思维错误了吗?我相信我已经遵循了管理大型KML数据集的推荐做法,但我不确定如何通过API使用它们。

附录

作为我想解决的问题类型的一个例子: 想象一下,您正在使用Google地球插件构建Web应用程序,并且您希望为世界上的每组交通灯显示地标。地标应仅以适当的细节水平显示(例如,当摄像机处于5公里高度时)。当用户点击地标时,我们希望网络应用加载该组交通信号灯的统计信息,并将其显示在侧边栏中。

你会如何设计这个?

2 个答案:

答案 0 :(得分:1)

您无需直接访问对象数据即可提供所需的功能。您将使用基于区域的网络链接层次结构完全像处理一样处理数据加载。 然后,如果您的使用方案与您在附录中列出的方案类似,那么您只需使用click事件中的目标数据,根据需要根据地标加载统计数据。

例如,您可以在窗口对象上设置一个通用的mousedown事件处理程序,然后测试以查看目标是否为地标。您可以在加载任何数据之前添加此通用侦听器,并且在单击动态加载的地标时仍会触发它。根本不需要将单个事件监听器附加到地标。

e.g。

window.google.earth.addEventListener(ge.getWindow(), 'mousedown', onWindowMouseDown);

var onWindowMouseDown = function(event) {
  if (event.getTarget().getType() == 'KmlPlacemark') {
    // get the placemark that was clicked
    var placemark = event.getTarget();

    // do something with it, or one of its relative objects...
    var document = placemark.getOwnerDocument();
    var parent = placemark.getParentNode();

    // etc...
  }
}

答案 1 :(得分:0)

不确定这是否是您想要的,但有一个kmltree api将:

  1. 根据给定的kml为您构建kml树
  2. 允许您拥有'kmlloaded'事件处理程序
  3. http://code.google.com/p/kmltree/

    function initCB(instance){
        ge = instance;
        ge.getWindow().setVisibility(true);
    
        var gex = gex = new GEarthExtensions(ge);
    
        var tree = kmltree({
            url: 'http://foo.com/regionatedRoot.kml',
            gex: gex, 
            mapElement: $('#map3d'), 
            element: $('#tree'),
        });
    
        $(tree).bind('kmlLoaded', function(event, kmlObject){ //do something here });
    
        tree.load();
    }
    

    它确实需要你引入另一个js api,但它工作得很好,并为你提供了一些很好的内置功能。<​​/ p>

    到目前为止,我还没有找到任何可以在加载kml时触发事件的插件......

    你可以尝试使用fetchKml(),特别是如果你在那里为那个链接硬编码?

    google.earth.fetchKml(ge, 'http://foo.com/regionatedRoot.kml', function(kmlObject){
         //do logic here
    });
    
相关问题