5

我正在尝试加载两个具有$.getScript获取谷歌地图脚本功能的脚本,然后在加载之后,我得到另一个脚本(goMap),这使得地图小程序很容易制作。

但是,加载后,获取 Google Map API 的第一个脚本很好,然后第二个脚本返回解析错误并显示如下:

TypeError:'未定义'不是构造函数'

然而,我不知道它是从哪里引用或从哪一行引用的,我认为它一定是试图在这个文件上执行地理编码器(第一行之后(function($){

http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js

这是我的代码:

$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function()
{
    $.getScript('../js/gomap.js').done(function()
    {
            // this never gets called
            alert('gomap loaded');
    }).fail(function(jqxhr, settings, exception)
    {
        alert(exception); // this gets shown
    });
}).fail(function()
{
    alert('failed to load google maps');
});

我尝试将 AJAX 设置更改为 set asyncfalse但它根本没有帮助。

4

2 回答 2

4

该错误是由于 Google Maps API 期望在头部加载,使用<script src="http://maps.google.com/maps/api/js?sensor=true"></script>.

如果由于某种原因你不能做到这一点,那么仍然有希望。Google Maps API 不起作用,因为它用于document.write在页面中注入依赖项。要使代码正常工作,您可以覆盖本机document.write方法。

演示:http: //jsfiddle.net/ZmtMr/

var doc_write = document.write; // Remember original method;
document.write = function(s) {$(s).appendTo('body')};
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function() {
    $.getScript('../js/gomap.js').done(function() {
        alert('gomap loaded');
        document.write = doc_write; // Restore method
    }).fail(function(jqxhr, settings, exception) {
        alert(exception); // this gets shown
        document.write = doc_write; // Restore method
    });
}).fail(function() {
    alert('failed to load google maps');
});
于 2012-02-19T21:51:21.183 回答
0

@lolwut 试试

$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function()
{
    alert(11);
    $.getScript('http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js').done(function()
    {
            // this never gets called
            alert('gomap loaded');
    }).fail(function(jqxhr, settings, exception)
    {
        alert(exception); // this gets shown
    });
}).fail(function()
{
    alert('failed to load google maps');
});

如果这可行,那么您的相对路径../js/gomap.js是错误的!

于 2012-02-19T11:20:48.697 回答