Google Maps脚本中断了Safari TypeError:表达式'document.body'[null]的结果不是对象

时间:2011-07-19 15:35:47

标签: javascript google-maps google-maps-api-3

我在我的网页上显示谷歌地图,它在除Safari之外的所有浏览器中都能正常工作。

Safri在页面上突然出现,JS错误说:

TypeError: Result of expression 'document.body' [null] is not an object.  maps.google.com/maps/api/js?sensor=true&region=nz&async=2&callback=initializeConfigMap:11
TypeError: Result of expression 'a' [null] is not an object.  maps.gstatic.com/intl/en_us/mapfiles/api-3/5/11/main.js:30

这似乎来自的代码是我使用此行从谷歌链接到的脚本:

$.getScript("http://maps.google.com/maps/api/js?sensor=true&region=nz&async=2&callback=initializeConfigMap", function () {});

脚本如下所示:

window.google = window.google || {};
google.maps = google.maps || {};
(function() {

    function getScript(src) {
        var s = document.createElement('script');
        s.src = src;
        document.body.appendChild(s);
    }

    var modules = google.maps.modules = {};
    google.maps.__gjsload__ = function(name, text) {
        modules[name] = text;
    };

    google.maps.Load = function(apiLoad) {
        delete google.maps.Load;
        apiLoad([null,[[["http://mt0.googleapis.com/vt?lyrs=m@158\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/vt?lyrs=m@158\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"m@158"],[["http://khm0.googleapis.com/kh?v=88\u0026hl=en-US\u0026","http://khm1.googleapis.com/kh?v=88\u0026hl=en-US\u0026"],null,null,null,1,"88"],[["http://mt0.googleapis.com/vt?lyrs=h@158\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/vt?lyrs=h@158\u0026src=api\u0026hl=en-US\u0026"],null,null,"imgtp=png32\u0026",null,"h@158"],[["http://mt0.googleapis.com/vt?lyrs=t@127,r@158\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/vt?lyrs=t@127,r@158\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"t@127,r@158"],null,[[null,0,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1.14\u0026hl=en-US\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1.14\u0026hl=en-US\u0026"]],[null,0,8,9,[[[330000000,1246050000],[386200000,1279600000]],[[345000000,1279600000],[386200000,1286700000]],[[348900000,1286700000],[386200000,1293600000]],[[354690000,1293600000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1.14\u0026hl=en-US\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1.14\u0026hl=en-US\u0026"]],[null,0,10,19,[[[329890840,1246055600],[386930130,1284960940]],[[344646740,1284960940],[386930130,1288476560]],[[350277470,1288476560],[386930130,1310531620]],[[370277730,1310531620],[386930130,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1.14\u0026hl=en-US\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1.14\u0026hl=en-US\u0026"]],[null,3,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=en-US\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=en-US\u0026"]],[null,3,8,9,[[[330000000,1246050000],[386200000,1279600000]],[[345000000,1279600000],[386200000,1286700000]],[[348900000,1286700000],[386200000,1293600000]],[[354690000,1293600000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=en-US\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=en-US\u0026"]],[null,3,10,null,[[[329890840,1246055600],[386930130,1284960940]],[[344646740,1284960940],[386930130,1288476560]],[[350277470,1288476560],[386930130,1310531620]],[[370277730,1310531620],[386930130,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=en-US\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=en-US\u0026"]]],[["http://cbk0.googleapis.com/cbk?","http://cbk1.googleapis.com/cbk?"]],[["http://khmdb0.googleapis.com/kh?v=40\u0026hl=en-US\u0026","http://khmdb1.googleapis.com/kh?v=40\u0026hl=en-US\u0026"],null,null,null,null,"40"],[["http://mt0.googleapis.com/mapslt?hl=en-US\u0026","http://mt1.googleapis.com/mapslt?hl=en-US\u0026"]],[["http://mt0.googleapis.com/mapslt/ft?hl=en-US\u0026","http://mt1.googleapis.com/mapslt/ft?hl=en-US\u0026"]],[["http://mt0.googleapis.com/vt?hl=en-US\u0026","http://mt1.googleapis.com/vt?hl=en-US\u0026"]]],["en-US","NZ",null,0,null,"http://maps.google.com","http://maps.gstatic.com/intl/en_us/mapfiles/","http://csi.gstatic.com","https://maps.googleapis.com","http://maps.googleapis.com"],["http://maps.gstatic.com/intl/en_us/mapfiles/api-3/5/11","3.5.11"],[3021543403],1.0,null,null,null,null,1,"initializeConfigMap",null,null,0,"http://khm.googleapis.com/mz?v=88\u0026"], loadScriptTime);
    };
    var loadScriptTime = (new Date).getTime();
    getScript("http://maps.gstatic.com/intl/en_us/mapfiles/api-3/5/11/main.js");
})();    

我认为这可能是导致问题的因素:

document.body.appendChild(s);

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

此更改解决了我在safari和ie7中的一些加载问题:

  • 尝试在脚本网址中添加版本:&v=3.7这是冻结版本。避免使用导致一些加载问题的3.exp版本。

答案 1 :(得分:0)

我找到了解决方案。尝试异步加载Google Maps API。为我工作

Asynchronously Loading the API

答案 2 :(得分:0)

我在使用OS X 10.10和Safari时遇到了同样的问题。通过添加async和defer to script tag来解决它。



<script async defer src="https://maps.googleapis.com/maps/api/js?key=...."/>
&#13;
&#13;
&#13;

下面是文档的内容:

  

同步加载API

     

在加载Maps API的脚本代码中,可以省略   async属性和回调参数。这将导致   加载API以阻止,直到下载API。

     

这可能会减慢您的页面加载速度。但这意味着你可以写   假设API已经加载,后续脚本标记。

除此之外,你当然应该确保div有一个高度。如果未指定,div的默认高度为0,即使加载也会隐藏地图。

祝你好运。