Javascript 更改googlemap回调的此值

Javascript 更改googlemap回调的此值,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在使用以下代码为我的网站按需加载谷歌地图: var googleMaps = document.createElement("script"); googleMaps.type = "text/javascript"; googleMaps.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&" + "callback=googlemapready"; 问题是,无论我做什么,回调都是以“window.goog

我正在使用以下代码为我的网站按需加载谷歌地图:

  var googleMaps = document.createElement("script");
  googleMaps.type = "text/javascript";
  googleMaps.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&" +
  "callback=googlemapready";

问题是,无论我做什么,回调都是以“window.googlemapredy”的形式执行的,有什么方法可以将它设置为另一个对象的方法吗?这是在不污染全局名称空间的情况下,我知道我可以将上述代码封装在一个函数中,然后设置window.googlemapredy=something…

基本上,回调函数必须是一个在全局范围内可访问的函数(这是google运行回调函数的地方)

API使用
eval
执行回调,因此过程如下:

//the global callback
function bar(){ alert('callback executed');}

//string provided as callback
callbackname='bar' ;

//execute it
eval('window.'+callbackname+'()') ;
查看eval部分时,应该可以运行未定义为全局的函数,例如作为对象属性的函数:

//a global object with a method
foo={bar:function(){ alert('callback executed too');}};

//string provided as callback
othercallbackname='foo.bar';

//execute it
eval('window.'+othercallbackname+'()');
…并且它可以工作(也可以使用maps API)

问题是:你仍然需要一个全局的东西(
foo

但这不是一个真正的问题,当你使用谷歌API时,至少会有一个全局对象你无法摆脱:
google

因此:将回调定义为全局
google
-对象的属性

您可能会问,当您尚未加载API时,如何设置google的属性:在加载API之前自行定义:

window.google={bar:function(){/*do something*/}};
…然后使用:
callback=google.bar

示例实现(适用于多个调用):


演示:

使用jsapi的加载程序:据我所知,这是行不通的,因为我不仅想动态初始化map,还想根据请求动态地将googlemap脚本插入到我的页面上(以便在发出使用map的请求之前不会下载该脚本)。maps API将在加载时加载(必须事先加载jsapi)这就是问题所在,因为我在我的网站上只使用这个地图,这只是另一个需要下载的脚本冗余层,我需要将脚本元素的src中的某些内容传递给google以更改默认回调上下文,或者使用一些js解决方法将与googlemap关联的回调重定向到其他对象,而不会污染全局nam不是说它会毁了我的项目,但如果能做到这一点,最好有这个功能。上下文是窗口,当google试图访问回调函数时,函数必须存在(没有解决方法)。你可以做什么:在调用回调后立即将其从全局命名空间中删除。非常聪明,这应该是我一直在寻找的。非常聪明,我不敢相信我是第一个+1!如果map脚本完全覆盖
窗口会怎么样。google
对象?它将失败,因为回调将不是它们的。这是开发人员的工作oper不控制。
//create maps of elements with className map
document.addEventListener('click',function(e){
  var node=e.target,callback,script,execCallbacks;

  //check the class
  if(node.className==='map'){
     //assign a new class to prevent from further clicks 
     node.className='mapped';

     //when window.google isn't available, create it
    if(!window.google){
      window.google={}; 
    }

    //we will store the callbacks in an array  
    if(!window.google.callbacks){
      window.google.callbacks=[];
    }
    //function that executes the callbacks
    if(!window.google.execCallbacks){
      window.google.execCallbacks=function(){
        while(this.callbacks.length){
          this.callbacks.shift()();
        }
      };
    }

    //define the callback(creates a simple map) 
    callback=function(){
        new google.maps.Map(node,
                           {zoom:1,
                            center:new google.maps.LatLng(0,0)
                           });
    };

    //google.maps isn't loaded yet
    if(!window.google.maps){
       //push the callback to the callbacks
       window.google.callbacks.push(callback);
       //load the API
       script=document.createElement('script');
       script.src="https://maps.googleapis.com/maps/api/js?v=3&" +
                  "callback=google.execCallbacks";
       document.getElementsByTagName('script')[0].parentNode.appendChild(script);
     }
     //API is already loaded, run the callback directly
     else{
      callback();
     }

  }
});