IE8中的Google Maps API加载失败(“Google'is undefined”)

Google Maps API load failure ("'google' is undefined") in IE8

本文关键字:Google is undefined Maps 中的 API 加载 IE8 失败      更新时间:2023-09-26

加载时http://maps.google.com/maps/api/js?sensor=false在脚本标签中,Chrome、Safari、Firefox和IE9中的一切都很好。

然而,当我在兼容模式下查看IE9(或者,我被告知,在IE8中),映射不会加载,并且"google"未定义"会记录在控制台中。

以下是相关代码,触发错误的行用注释标识:

<html>
<head>
<title>Test Map</title>
<script type="application/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
</head>
<body>
<div id="map_canvas"></div>
<script type="text/javascript"> 
var lat=37.763154;
var lon=-122.457941;
var initialZoom=17;
var mapTypeId = 'Custom Map';
var mapStyle = [{featureType:"landscape", elementType:"all", stylers:[{hue:"#dae6c3"},{saturation:16},{lightness:-7}]}, 
                {featureType:"road", elementType:"geometry", stylers:[{hue:"#ffffff"},{saturation:-100},{lightness:100}]}];
//**The error is tripped in this next line, again only in IE9 compatibility mode or IE8*     
var styledMap = new google.maps.StyledMapType(mapStyle);
var mapType = new google.maps.ImageMapType({
    tileSize: new google.maps.Size(256,256),
    getTileUrl: function(coord,zoom) {
        return "img/tiles/"+zoom+"/"+coord.x+"/"+coord.y+".png";
    }
});
var map = new google.maps.Map(document.getElementById("map_canvas"), 
        {center:new google.maps.LatLng(lat,lon),
         mapTypeId:google.maps.MapTypeId.ROADMAP,
         zoom:initialZoom,
         mapTypeControl:false});
map.overlayMapTypes.insertAt(0, mapType);
map.mapTypes.set(mapTypeId, styledMap);
map.setMapTypeId(mapTypeId);
</script>
</body>
</html>

因此,出于某种原因,并且仅在IE9+兼容模式和IE8中,脚本标记指定http://maps.google.com/maps/api/js?sensor=false在正文中的后续嵌入脚本之前没有加载和/或执行。

其他人能复制吗?如何更正此问题?

显然,问题是IE 8没有找到"application/javascript"。我在<head>部分的<script>标记中将其更改为"text/javascript",现在我的代码就可以工作了。当然,如果我把它改回"application/javascript",它就会停止工作。

一种猜测是,您的页面通过https工作,而来自谷歌的请求是http。将Google请求转换为https,错误就会消失。这对我有用。

请参阅:可能的解决方案

我的谷歌地图v3站点在IE 9兼容视图模式、IE 8和IE7下停止工作。

原因:只有在使用IE F12 Developer工具检查脚本时,才发现使用jQuery的java脚本中的错误。这是冒犯性的台词。错误是标记类中缺少单引号。

$('<tr>', { 'class': country }).appendTo(tableSelector).append(h1).append(h2);

起初,我以为这是添加了一把钥匙,于是就开始寻找一个错误的线索=http://maps.googleapis.com/maps/api/js?key=MY_KEY_FROM_API_CONSOLE&传感器=错误

教训是:使用工具,Firebug或IE工具或其他任何工具,检查您的javascript是否存在引入的问题。

确保IE未处于脱机模式。听起来浏览器没有连接到互联网。

IE正在下载JS,然后尝试在本地机器上执行JS,而其他浏览器只是将其作为文本文件打开。默认情况下,你可以在stuff下载到的任何地方找到从IE下载的JS。

编辑:根据更新,请参阅此Fiddle以查看某种工作修复。http://jsfiddle.net/h6rc3/