2012-12-19 70 views
3

我目前正在嘗試使用require.js模塊定義和依賴關係處理程序加載Google地圖庫及其擴展RichMarkerRequire.js問題同時加載Google地圖和它的Richmarker擴展

我已經宣佈的路徑既谷歌地圖模塊和擴展類似以下內容:

"gmaps": "modules/google_maps", 
"richmarker": "vendor/google/maps/richmarker.min" 

的GOOGLE_MAPS模塊看上去就像

define('gmaps', ['async!http://maps.googleapis.com/maps/api/js?key=mykey&sensor=true'], 
function(){ 
    return window.google.maps; 
}); 

最後,該模塊消耗google地圖庫和advancedMarker擴展定義如下:

define(['common', 'gmaps','jquery','jqueryui', 'bootstrap',"vendor/google/maps/clusterer.min", "tmp/clusteringData", "richmarker"], 
function(common, gmaps){ 

然而,GoogleMap的正確inits onload事件,但我得到的錯誤關於我的控制檯richmarker擴展:

Uncaught ReferenceError: google is not defined richmarker.min.js:1 
Uncaught ReferenceError: RichMarker is not defined google.init.js:267 

我在哪裏我做錯了?謝謝您的幫助。

回答

0

我已經回答了同樣的問題,前不久:

https://stackoverflow.com/a/13955963/1916258

我認爲你需要「中間層」谷歌的對象。

require.config({ 
    baseUrl: 'js', 
    paths: { 
     gmaps: 'path/to/gmaps' 
    }, 
    shim: { 
     gmaps: { 
     exports: 'google' 
     } 
    } 
}); 
+2

我不認爲這適用於異步require.js插件。 –

+1

即使當我映射到沒有異步的本地路徑時,這也不適用於我。 – pacman

+0

也不適用於我 – tibalt

2

我與infobox.js有相同的問題,嚴格依賴谷歌地圖。

我做了什麼:

  1. 我定義模塊google(而不是gmaps)返回整個谷歌的對象(而不是單maps屬性)

    define('google', ['async!http://maps.googleapis.com/maps/api/js?key=mykey&sensor=true'], 
        function() { 
        return window.google; 
        }); 
    
  2. 我已經在RequireJS配置文件中定義了infoboxgoogle之間的依賴關係:

    paths: { 
        google: 'ref/google', 
        infobox: 'ref/infobox' 
    }, 
    shim: { 
        infobox: { 
        deps: ['google'] 
        } 
    }