[英]Cannot use callback in Google Maps script tag
Building the Google Maps script
tag like this to be appended after the div
has been rendered and then calling initialize
which initializes the map like this fails because I have wrapped my code in an anonymous immediately invoked function expression... 在渲染
div
之后构建像这样的Google Maps script
标记,然后调用initialize
初始化像这样的初始化失败,因为我已经将我的代码包装在一个匿名的立即调用的函数表达式中...
(function(){
var map,infoWindow;
//lots of stuff...helper functions to initialize
var initialize=function()
{
//init the map and infoWindow and other stuff here
//manipulate the DOM here...
//add controls that draw shapes on the map here...
};
var loadScript=function()
var script=document.createElement('script');
script.type='text/javascript';
//cannot call initialize...
script.src='http://maps.googleapis.com/maps/api/js?v=3.exp&key=API_KEY&sensor=true&'+'libraries=geometry&'+'callback=initialize';
document.body.appendChild(script);
};
$("map-canvas").ready(loadScript);
})();
I get the error saying that the global object initialize
could not be found.I would rather not unwrap initialize because it interacts directly with the map
and infoWindow
variables on many occassions. 我得到错误说无法找到全局对象
initialize
我宁愿不解包初始化,因为它在很多场合直接与map
和infoWindow
变量交互。
I have also tried to use the following code: 我也尝试使用以下代码:
var res=(function(){
var map,infoWindow;
//lots of stuff...helper functions to initialize
var initialize=function()
{
//init the map and infoWindow and other stuff here
//manipulate the DOM here...
//add controls that draw shapes on the map here...
};
var loadScript=function()
var script=document.createElement('script');
script.type='text/javascript';
//cannot call initialize...
script.src='http://maps.googleapis.com/maps/api/js?v=3.exp&key=API_KEY&sensor=true&'+'libraries=geometry&'+'callback=initialize';
document.body.appendChild(script);
};
return {init:initialize};
})();
var loadScript=function()
{
var script=document.createElement('script');
script.type='text/javascript';
//cannot call initialize...
script.src='http://maps.googleapis.com/maps/api/js?v=3.exp&key=API_KEY&sensor=true&'+'libraries=geometry&'+'callback='+res.init;
document.body.appendChild(script);//line 509
};
$("map-canvas").ready(loadScript);
Now it says: 现在它说:
GET http://maps.googleapis.com/maps/api/js?v=3.exp&key=PART_OF_API_KEY%20and%20display%20on%20map...have%20to%20figure%20this%20one%20out...}
400 (Bad Request) gmaps.js:509
loadScript gmaps.js:509
c jquery-latest.js:3048
p.fireWith jquery-latest.js:3160
x.extend.ready jquery-latest.js:433
q
Regarding to attempt #1: 关于尝试#1:
the scope of variables created in the self-executing function is the function. 在自执行函数中创建的变量的范围是函数。 So when you use the
var
-keyword this variable will not be visible outside of the function. 因此,当您使用
var
-keyword时,此变量在函数外部将不可见。
Remove the var-keyword, and initialize will be global visible. 删除var-keyword,初始化将是全局可见的。
attempt #2 can't work, because res.init
is an object and not a string. 尝试#2无法工作,因为
res.init
是一个对象而不是一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.