jQuery getScript和Google Maps API错误消息

k0n*_*0ni 4 jquery google-maps

我在加载google maps api时遇到了问题.

我用一个初始化地图的函数得到了我自己的对象,谷歌地图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

    });
});
Run Code Online (Sandbox Code Playgroud)

怎么了?为什么这不运行?

Rup*_*Rup 6

你已经进入async=2了脚本URL行,这意味着异步加载映射核心 - 在调用InitializeMaps之前需要等待它完成.您可以尝试async=2从网址中删除,也可以使用Google Map的异步和回调代替getScript回调函数,例如

$.getScript(MyGMap.GMapScriptURL + CurrentKey + "&callback=MyGMap.InitializeMaps");
Run Code Online (Sandbox Code Playgroud)