2013-04-03 69 views
42

我正在努力加載gmaps API與requireJS。這是我已經試過:如何使用RequireJS加載Google Maps API?

requirejs.config({ 
    urlArgs: "noCache=" + (new Date).getTime(), 
    paths : { 
     "jquery": "vendor/jquery-1.8.2.min", 
     "bootstrap": "vendor/bootstrap.min",  
     "underscore": "libs/underscore-min", 
     "backbone": "libs/backbone-min",  
     "template": "libs/template", 
     "gmaps": "http://maps.google.com/maps/api/js?v=3&sensor=false" 
    }, 
    shim: { 
     'backbone': { 
      deps: ['jquery', 'underscore'], 
      exports: 'Backbone' 
     }, 
     'underscore': { 
      exports: '_' 
     }, 
     'bootstrap': { 
      deps: ['jquery'] 
     }, 
     'gmaps': { 
      deps: ['jquery'] 
     }, 
     'main':{ 
      deps: ['jquery','gmaps'] 
     } 
    } 
}); 

require(["main"], function (main) {}) 

但是這裏面main.js當我嘗試實例我得到了地理編碼器,,不確定是不是一個函數」錯誤

var geocoder = new google.maps.Geocoder(); 

任何想法可能我是做錯了

+0

是否有網絡錯誤?地圖API實際上加載JS? – 2013-04-03 19:11:12

+1

沒有它未被加載... – hjuster 2013-04-03 19:16:36

+0

這個答案可能有幫助 - http://stackoverflow.com/questions/6398342/cant-initiate-the-google-maps-geocoder。我沒有API密鑰,因此可能無法完全適用於我。似乎必須要求加載Geocoder。 – 2013-04-03 19:26:36

回答

63

我已經成功地將其與async插件理清

一個簡單的例子是:?

require.config({ 
    paths: { 
     'async': 'lib/requirejs-plugins/src/async' 
    } 
}); 

define(['async!http://maps.google.com/maps/api/js?sensor=false'], function() { 
    // Google Maps API and all its dependencies will be loaded here. 
}); 
+7

http://blog.millermedeiros.com/requirejs-2-0-delayed-module-evaluation-and-google-maps/ – 2013-08-07 15:09:58

+0

不要與https://github.com/caolan/async混淆 – Brett 2014-03-27 01:56:37

+0

對我來說,斷點沒有達到callBack,您評論// Google Maps ...將在這裏加載。 – 2016-05-28 18:06:37

4

還有goog插件一個簡單的例子(需要異步和propertyParser),可在github

用法谷歌地圖示例:

require(["goog!maps,3,other_params:sensor=false"], function(){}); 
+0

這是做到這一點的方法。對我來說,它與谷歌的地方沒有任何問題:'''goog!地圖,3,other_params:鍵= XXX和庫=地方''' – sucotronic 2015-10-13 12:08:16

12

感謝user1706254導致官方文檔:https://github.com/millermedeiros/requirejs-plugins/正在使用關鍵字'定義',這不是爲我工作,但'要求'工作正常。

我能不能直接加載:

require(["goog!maps,3,other_params:sensor=false"], function(){}); 

但使用異步方式奏效了:

require(['async!http://maps.google.com/maps/api/js?sensor=false'], function(){}); 
+0

嗯,它是爲我工作。 goog模塊要求在路徑中定義'async'和'propertyParser',就像那樣,包括駱駝套管。順便說一句,傳感器現在什麼都不做,可以排除 – light24bulbs 2014-11-02 18:54:15

+0

任何人都可以獲得API密鑰來處理這個?據我所知,這不支持在URL中包含API密鑰。將一個硬編碼插入插件並不難,但我認爲這是錯過了一個觀點。我現在更喜歡使用異步模塊來手動請求URL,因爲這可以讓您完全控制 – light24bulbs 2014-11-04 18:56:53

0

@ hjuster的回答使我的方式,我已經被回調解決功能

define(['async!http://maps.google.com/maps/api/js?key=YOURKEY!callback'], 
    function (_ExpectedMap) { 
           callback(); 
          }); 

注意的!回調在網址的結尾開始與異步!,當加載操作完成時,將調用回調方法。

​​

還有一種question我最近注意到,另一個功能(的onLoad)在使用中的回調,而不是從超時錯誤,以防止。有趣。

-2

無法使插件出於某種原因,但這種解決方法救了我的一天:

require(['https://apis.google.com/js/client.js?onload=doNothing'], function() { 
    // Poll until gapi is ready 
    function checkGAPI() { 
     if (gapi && gapi.client) { 
     self.init(); 
     } else { 
     setTimeout(checkGAPI, 100); 
     } 
    } 

    checkGAPI(); 
    }); 
}); 

只是檢查是否gapi準備每100毫秒,直到最後的負載。

找到的代碼本文http://dailyjs.com/2012/12/06/backbone-tutorial-2/

在我想你也可以用

if (google && google.maps && google.maps.Geocoder) { 
    // now google.maps.Geocoder is gonna be defined for sure :) 
} 
1

試試你不需要異步插件來使用谷歌地圖與require.js。我們的目標可以只使用一個簡單的shim配置來實現:

require.config({ 
 
    paths: { 
 
     gmaps: '//maps.googleapis.com/maps/api/js?' // question mark is appended to prevent require.js from adding a .js suffix 
 
    }, 
 
    shim: { 
 
     gmaps: { 
 
      exports: 'google.maps' 
 
     } 
 
    } 
 
}); 
 

 
require(['gmaps'], function (gmaps) { 
 
    var center = {lat: -34.397, lng: 150.644}; 
 
    var map = new gmaps.Map(document.getElementById('map'), { 
 
     center: center, 
 
     zoom: 8 
 
    }); 
 
    new gmaps.Marker({ 
 
     map: map, 
 
     position: center 
 
    }); 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script> 
 

 
<div id="map" style="width: 100%; height: 200px"></div>