2013-07-24 22 views
1

使用Google地球我有一个加载的kml图层,显示美国每个县的多边形。点击一个气球时弹出一些关于状态的相关信息(名称,哪个状态,区域等)当用户单击多边形时,我希望信息也可以在其他地方的DIV元素上弹出。无法将事件侦听器附加到kml图层的多边形

这是我的代码到目前为止。

var ge; 
google.load("earth", "1"); 

function init() { 
    google.earth.createInstance('map3d', initCB, failureCB); 
} 

function initCB(instance) { 
    ge = instance; 
    ge.getWindow().setVisibility(true); 
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO); 
    ge.getNavigationControl().setStreetViewEnabled(true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true); 

    //here is where im loading the kml file 
    google.earth.fetchKml(ge, href, function (kmlObject) { 
     if (kmlObject) { 
      // show it on Earth 
      ge.getFeatures().appendChild(kmlObject); 
     } else { 
      setTimeout(function() { 
       alert('Bad or null KML.'); 
      }, 0); 
     } 
    }); 

    function recordEvent(event) { 
     alert("click"); 
    } 

    // Listen to the mousemove event on the globe. 
    google.earth.addEventListener(ge.getGlobe(), 'click', recordEvent); 
} 

function failureCB(errorCode) {} 

google.setOnLoadCallback(init); 

我的问题是,当我改变ge.getGlobe()到kmlObject或ge.getFeatures()这是行不通的。

我的第一个问题是,当用户点击kml图层的多边形时,我应该如何将ge.getGlobe()更改为能够获取点击侦听器?

之后我打算使用getDescription()getBalloonHtml()来获取多边形气球信息。我是否在正确的轨道上?

回答

1

...我应该怎么改变ge.getGlobe()来...

你并不需要从GEGlobe改变事件对象。事实上,这是最好的选择,因为您可以使用它来捕获所有事件,然后检查处理程序中的目标对象。这意味着您只需在API中设置单个事件侦听器。

另一种选择是以某种方式解析KML并将特定事件处理程序附加到特定对象。这意味着你必须为每个对象创建一个事件监听器。

我是否在正确的轨道?

所以,你是在正确的轨道上。我会保留通用的GEGlobe事件侦听器,但会扩展您的recordEvent方法以检查您感兴趣的KML对象的类型。您不显示KML,因此很难知道您的KML结构(您的<Polygon>是否嵌套在<Placemarks>或`元素例如)。

在简单情况下,如果您的多边形位于地标中,那么您可以执行以下操作。基本上监听所有对象的点击,然后筛选所有Placmark(通过API创建或通过KML加载)。

function recordEvent(event) { 
    var target = event.getTarget(); 
    var type = target.getType(); 
    if(type == "KmlPolygon") { 
    } else if(type == "KmlPlacemark") { 
    // get the data you want from the target. 
    var description = target.getDescription(); 
    var balloon = target.getBalloonHtml(); 
    } else if(type == "KmlLineString") { 
    //etc... 
    } 
}; 

google.earth.addEventListener(ge.getGlobe(), 'click', recordEvent); 

如果您想要选择另一个选项,您可以在KML Dom加载后迭代,然后将事件添加到特定对象。你可以使用类似kmldomwalk.js的东西来做到这一点。虽然我不会在这里推荐这种方法,因为您将在api中创建大量的事件侦听器(在这种情况下每个地标都会创建一个)。最重要的是,这些事件是从kml文件附加到每个特定的对象,所以如果你有其他Plaemarks等,不应该有相同的'点击'行为,那么它可以是有用的。

function placeMarkClick(event) { 
    var target = event.getTarget(); 
    // get the data you want from the target. 
    var description = target.getDescription(); 
    var balloon = target.getBalloonHtml(); 
} 

google.earth.fetchKml(ge, href, function (kml) { 
    if (kml) { 
     parseKml(kml); 
    } else { 
     setTimeout(function() { 
      alert('Bad or null KML.'); 
     }, 0); 
    } 
}); 

function parseKml(kml) { 
    ge.getFeatures().appendChild(kml); 
    walkKmlDom(kml, function() { 
     var type = this.getType(); 
     if (type == 'KmlPlacemark') { 
      // add event listener to `this` 
      google.earth.addEventListener(this, 'click', placeMarkClick); 
     } 
    }); 
}; 
+1

嘿,这非常有帮助,非常感谢你......我结束了第一个选择 – RidinAGrvyTrain

0

很长一段时间,因为我有这个工作..但我可以试着帮你或给你一些曲目...

关于“google.earth.addEventListener(ge.getGlobe你的问题() ,'click',recordEvent);“ ge.getGlobe不能ge.getFeatures()来代替:如果您的文档(https://developers.google.com/earth/documentation/reference/interface_g_e_feature_container-members)为GEFeatureContainer(这是getFeatures()的输出类型看,Click事件没有被定义

GE! getGlobe与kmlObject取代:啥子是kmlObject这里??

关于使用getDescription,你可以对getTarget方法,getCurrentTarget看看... (https://developers.google.com/earth/documentation/reference/interface_kml_event

正如我告诉你,我已经无法正常工作有很长一段时间..所以我不知道这可以帮助你,但至少,这是你可以看第一个轨道!

请让我知道! :-)

相关问题