【问题标题】:AMD version of Google Maps V3 for use with require.js?与 require.js 一起使用的 AMD 版 Google Maps V3?
【发布时间】:2012-09-28 23:31:54
【问题描述】:

有没有人在 AMD 版本中使用过类似 require.js 的 Googlemaps V3?是否已经在某个地方完成了?

【问题讨论】:

    标签: google-maps-api-3 requirejs js-amd


    【解决方案1】:

    在 require.js 中你可以使用异步插件,然后像这样调用它:

    define([
        'async!http://maps.google.com/maps/api/js?sensor=false'
    ], function(){
        //Create your map.
    });
    

    【讨论】:

    • 请注意,运行 RequireJS 优化器后,优化后的脚本将不会加载谷歌地图 API。
    • 你如何设置这样的API密钥? http://maps.google.com/maps/api/js?key=<MY_KEY>&sensor=false
    【解决方案2】:

    您也可以使用 jQuery.Deferred() 和一些全局变量(不理想,但我需要它以便我可以使用 grunt rjs 优化我的文件,这不适用于异步):

    // gmapsDone.js
    window._mapsLoaded = $.Deferred();
    window.gmapsLoaded = function(data) {
      delete window.gmapsLoaded;
      _mapsLoaded.resolve();
    };
    
    define(["http://maps.google.com/maps/api/js?v=3&sensor=false&callback=gmapsLoaded"], function(gmaps) {
      "use strict";
    
      return window._mapsLoaded.done;
    });
    

    然后,使用它:

    define(["gmapsDone"], function(gmapsDone) {
      function load() {
        // Do something
      }
      gmapsDone(load);
    });
    

    https://gist.github.com/taktran/5389668

    灵感来自http://blog.pixelingene.com/2011/10/using-jquery-dot-deferred-and-requirejs-to-lazy-load-google-maps-api/

    【讨论】:

    【解决方案3】:

    我最近通过使用上述 $.Deferred 方法帮助一位朋友解决了这个问题。这与优化器配合得很好,并且不会导致多个脚本加载。

    模块

    var google_maps_loaded_def = null;
    
    define(['jquery'],function($) {
    
      if(!google_maps_loaded_def) {
    
        google_maps_loaded_def = $.Deferred();
    
        window.google_maps_loaded = function() {
          google_maps_loaded_def.resolve(google.maps);    
        }
    
        require(['http://maps.googleapis.com/maps/api/js?sensor=true&callback=google_maps_loaded'],function(){},function(err) {
          google_maps_loaded_def.reject();
          //throw err; // maybe freak out a little?
        });
    
      }
    
      return google_maps_loaded_def.promise();
    
    });
    

    可用作要点: https://gist.github.com/MattSurabian/7868115

    用法

    使用上述模块并利用 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");
            });
        });
    

    我在这里写了一篇关于这种方法的简短博文,可能会有一些用处:RequireJS Projects and Asynchronously Loading the Google Maps API

    【讨论】:

      【解决方案4】:

      我整理了一个Google Maps AMD loader plugin,它在异步之上添加了一些功能!装载机。

      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');
      });
      

      【讨论】:

      • 我只是你的就是我要找的那个。如果我在单页应用程序上使用它,是否可以破坏您在上面定义的gmaps。清除听众怎么样?
      猜你喜欢
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2013-07-25
      • 2013-11-03
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多