简体   繁体   English

是什么导致IE8中的Google Maps错误?

[英]What is causing a Google Maps error in IE8?

We are using Google Maps and have identified an issue that only seems to happen in IE8 (and possibly below). 我们正在使用谷歌地图,并确定了一个似乎只在IE8(可能在下面)发生的问题。 The functionality works correctly in FF, Chrome, IE9. 该功能在FF,Chrome,IE9中正常运行。

The code that the error happens around is: 发生错误的代码是:

google.load("maps", "3.x", { other_params: "sensor=false" });
    var mapdiv = null;
    $(function () {
        mapdiv = document.getElementById("map");
        map = new google.maps.Map( mapdiv, {
            zoom: 1,
            center: new google.maps.LatLng(6, 35),
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        });
        var latlngbounds = new google.maps.LatLngBounds( );

In particular on this line: 特别是在这一行:

map = new google.maps.Map( mapdiv, {
    zoom: 1,
    center: new google.maps.LatLng(6, 35),
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.TERRAIN
});

and the error is: 而错误是:

Object doesn't support this property or method Object不支持此属性或方法

I've had a bit of a play with the IE dev tools and if I replace map = with something like var x = there isnt any error, so this leads me to believe that the map object is the culprit that is missing some property/method. 我已经对IE开发工具进行了一些游戏,如果我用var x =替换map =没有任何错误,那么这让我相信map对象是缺少某些属性的罪魁祸首/方法。 Although I don't really know where the map object comes from, I assume it gets loaded from the google.load call. 虽然我不知道map对象的来源,但我认为它是从google.load调用中加载的。

Does anyone know what is going on here? 有谁知道这里发生了什么?

When the line: 当行:

map = new google.maps.Map(mapdiv, { zoom: 1, center: new google.maps.LatLng(6, 35), disableDefaultUI: true, mapTypeId: google.maps.MapTypeId.TERRAIN });

gets executed the JavaScript interpreter will start traversing up the scope chain looking for where map is declared. 执行JavaScript解释器将开始遍历范围链,寻找声明map位置。 Since it's not declared locally, ie var map = ... , it doesn't find it in your local scope, and it ultimately gets added to the global scope. 由于它没有在本地声明,即var map = ... ,它在本地范围内找不到它,并最终被添加到全局范围。

In your case map is already defined in the global scope as window.map because, it's the Id of a div on your page. 在您的案例中, map已在全局范围内定义为window.map因为它是您网页上div的ID。 In all the browsers you don't see an error message, window.map is set to a new google.maps.Map object. 在所有浏览器中,您都没有看到错误消息, window.map设置为新的google.maps.Map对象。 For some reason, good or bad, IE8 won't let you create a global variable whose name conflicts with an existing global variable that refers to a DOM element. 出于某种原因,无论好坏,IE8都不会让你创建一个全局变量,其名称与引用DOM元素的现有全局变量冲突。

You could resolve this several different ways: 您可以通过以下几种方式解决:

  1. Create a local var map 创建一个局部var map
  2. Change the id of your div from map to something else 将div的id从map更改为其他内容
  3. If you have to have map exist in global scope, set it using window.map =... 如果必须在全局范围内存在map ,请使用window.map =...设置window.map =...

    (3b) Change the variable name to be one that doesn't conflict with window.map , eg myMap = new google.maps.Map(... (3b)将变量名称更改为与window.map不冲突的名称,例如myMap = new google.maps.Map(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM