我在加载 Google map API 时遇到问题。
我得到了自己的对象,其中包含一个初始化 map 的函数,并且 google map api 通过 jquery.getscript 加载。但我总是在回调函数中收到错误消息:
var MyGMap = {
GMapScriptURL: "http://maps.google.com/maps?file=api&v=2&async=2&key=",
Map: null,
Geocoder: null,
InitiazlizeMaps: function () {
if (GBrowserIsCompatible()) {
this.Map = new GMap2(document.getElementById("map_canvas"));
this.Map.setCenter(new GLatLng(37.4419, -122.1419), 13);
this.Geocoder = new GClientGeocoder();
}
}
}
$(function(){
var CurrentKey = "MY_KEY";
$.getScript(MyGMap.GMapScriptURL + CurrentKey, function () {
MyGMap.InitiazlizeMaps();
// throws GMap2 is undefined
});
});
怎么了?为什么这没有运行?
最佳答案
脚本 URL 行中有 async=2
,这意味着也异步加载映射核心 - 您需要等待它完成,然后才能调用 InitializeMaps。您可以尝试从 URL 中删除 async=2
,或者使用 Google map 的异步和回调而不是 getScript 回调函数,例如
$.getScript(MyGMap.GMapScriptURL + CurrentKey + "&callback=MyGMap.InitializeMaps");
关于jQuery getScript 和 Google Maps API 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2965898/