2013-07-24 72 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

正如我告訴你,我已經無法正常工作有很長一段時間..所以我不知道這可以幫助你,但至少,這是你可以看第一個軌道!

請讓我知道! :-)

相關問題