Google maps api 3 与require.js一起使用的谷歌地图V3的AMD版本?
有没有人使用过谷歌地图V3,在AMD版本中需要用到require.js之类的东西?在require.js中,您可以使用异步插件,然后像这样调用它:Google maps api 3 与require.js一起使用的谷歌地图V3的AMD版本?,google-maps-api-3,requirejs,js-amd,Google Maps Api 3,Requirejs,Js Amd,有没有人使用过谷歌地图V3,在AMD版本中需要用到require.js之类的东西?在require.js中,您可以使用异步插件,然后像这样调用它: define([ 'async!http://maps.google.com/maps/api/js?sensor=false' ], function(){ //Create your map. }); 您还可以使用jQuery.Deferred()和一些全局变量(不理想,但我需要它,这样我就可以使用grunt rjs优化文件,而
define([
'async!http://maps.google.com/maps/api/js?sensor=false'
], function(){
//Create your map.
});
您还可以使用jQuery.Deferred()和一些全局变量(不理想,但我需要它,这样我就可以使用grunt rjs优化文件,而grunt rjs不适用于异步): 然后,要使用它:
define(["gmapsDone"], function(gmapsDone) {
function load() {
// Do something
}
gmapsDone(load);
});
受的启发,我最近帮助一位朋友解决了这个问题,采用了上面提到的美元延迟方法。这与优化器配合得很好,不会导致多个脚本加载 模块 要点如下: 用法 要使用上述模块并利用promise与google.maps解析的事实,请执行以下操作:
define([ 'app/lib/google-maps-loader' ], function(GoogleMapsLoader){
GoogleMapsLoader.done(function(GoogleMaps){
// your google maps code here!
var geocoder = new GoogleMaps.Geocoder();
}).fail(function(){
console.error("ERROR: Google maps library failed to load");
});
});
或者,正常情况下只需引用google.maps
对象即可
define([ 'app/lib/google-maps-loader' ], function(GoogleMapsLoader){
GoogleMapsLoader.done(function(){
// your google maps code here!
var geocoder = new google.maps.Geocoder();
}).fail(function(){
console.error("ERROR: Google maps library failed to load");
});
});
我在这里写了一篇关于这个方法的短文,这可能会有一些用处:我把一个!加载器
require.config({
googlemaps: {
params: {
key: 'abcd1234', // sets api key
libraries: 'geometry' // set google libraries
}
}
});
require(['googlemaps!'], function(gmaps) {
// google.maps available as gmaps
var map = new gmaps.Map('map-canvas');
});
注意,在运行RequireJS优化器之后,优化的脚本将不会加载google maps API。您如何像这样设置API键<代码>http://maps.google.com/maps/api/js?key=&sensor=falseThaks zlog-它在工作-你能看看我吗?我只是你的才是我要找的。如果我在单页应用程序中使用它,是否有可能销毁您上面定义的
gmap
。让听众安静下来怎么样?
define([ 'app/lib/google-maps-loader' ], function(GoogleMapsLoader){
GoogleMapsLoader.done(function(){
// your google maps code here!
var geocoder = new google.maps.Geocoder();
}).fail(function(){
console.error("ERROR: Google maps library failed to load");
});
});
require.config({
googlemaps: {
params: {
key: 'abcd1234', // sets api key
libraries: 'geometry' // set google libraries
}
}
});
require(['googlemaps!'], function(gmaps) {
// google.maps available as gmaps
var map = new gmaps.Map('map-canvas');
});