2012-10-23 97 views
0

以下代碼有效。我可以加載一個頁面並讓地球顯示。TypeScript和Google地球

我想顯示導航控件。如果我在initCB中取消註釋行,它就可以工作。

我認爲代碼有一個範圍問題,需要微調才能正常工作。

謝謝。

declare var google; 

class GoogleEarth { 

    static pluginInstance; 

    static display() { 

     google.load("earth", "1"); 
     google.setOnLoadCallback(init); 
    } 

    static ShowNavigation() { 

     this.pluginInstance.getNavigationControl().setVisibility(this.pluginInstance.VISIBILITY_AUTO); 
    } 

    private static init() { 

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

    private static initCB(instance) { 

     this.pluginInstance = instance; 
     this.pluginInstance.getWindow().setVisibility(true); 
     //this.pluginInstance.getNavigationControl().setVisibility(this.pluginInstance.VISIBILITY_AUTO); 
    } 

    private static failureCB(errorCode) {} 
} 

GoogleEarth.display(); 
GoogleEarth.ShowNavigation(); 

回答

0

更新基於您的評論...

有這裏三個問題的組合。

  1. 競爭條件,其中變量在設置之前使用,因爲它們在回調中設置,但在回調發生之前使用。

  2. 範圍問題。回調函數在錯誤的範圍內執行。

這些可以使用下面的示例是固定的:

declare var google; 

class GoogleEarth { 

    private pluginInstance; 

    constructor (private showNavigation = false){ 
    } 

    display() { 
     var self = this; 
     google.load("earth", "1"); 
     google.setOnLoadCallback(function (instance) { 
      google.earth.createInstance('map3d', function() { 
       this.pluginInstance = instance; 
       this.pluginInstance.getWindow().setVisibility(true); 
       if (this.showNavigation) { 
        this.pluginInstance.getNavigationControl().setVisibility(this.pluginInstance.VISIBILITY_AUTO); 
       } 
      }, self.failureCB); 
     }); 
    } 

    ShowNavigation() { 

    } 

    failureCB(errorCode) {} 
} 

var earth = new GoogleEarth(true); 
earth.display(); 

我曾提到有雖然三個問題!

最後一個問題是您正在傳遞的instance沒有名爲getWindow()的函數 - 因此在代碼的此階段出現錯誤。這是一個雙重檢查Google文檔的案例。

+0

我看你要去哪裏showNavigation與pluginInstance有同樣的問題。它仍然是由它獲取initCB甚至當我通過真實的顯示() 下面是HTML,以獲得時間假這項工作 樣品 <腳本類型=「文/ JavaScript的」 SRC = 「https://www.google.com/jsapi」> <腳本類型= 「文本/ JavaScript的」 SRC = 「app.js」>

+0

@DougFinke我已經更新了我的答案。 – Fenton

+0

謝謝你的幫助Sohnee。當我做複製和粘貼時,這個例子在getWindow上得到一個錯誤。當我有空時,我會看看我是否造成問題。我是JavaScript新手,所以似乎可以使用TypeScript,OOP和狀態完全對象,但通過瀏覽器以及與Google Earth等庫的交互,我們仍在等待「事件」發生,以便完全實現對象?然後我們可以對它們執行操作。 –