0
構建Google Maps script
這樣的標記在div
已被渲染後調用,然後調用initialize
初始化地圖,因爲這樣會失敗,因爲我已經用直接調用的匿名封裝了我的代碼函數表達式...無法在Google地圖腳本標記中使用回調
(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);
})();
我得到的錯誤,指出全局對象initialize
不能found.I寧可不拆開包裝初始化,因爲它直接與許多occassions的map
和infoWindow
變量相互作用。
UPDATE:
我也曾嘗試使用下面的代碼:
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);
現在它說:
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
爲什麼不像文檔中那樣使用古老的方式?此外,你在地圖div的jquery選擇器中有一個拼寫錯誤:'#map-canvas'。 – alalp
@ user3280126不是拼寫錯誤,只是無知...將修復,但我回去,發現它的工作,即使沒有'#'...是否jQuery搜索任何DOM元素與類名或id對應的字符串我已發送??? – vamsiampolu
@ user3280126是否有靜態添加腳本標記與回調的優點,或者是否在DOM元素準備就緒之前調用它。 – vamsiampolu