简体   繁体   English

Google Maps Cluster IE8问题

[英]Google maps cluster IE8 issues

The below code works fine on firefox and IE9, but having issue updating the markers on IE8. 下面的代码在firefox和IE9上工作正常,但是在IE8上更新标记时出现问题。 i get "SCRIPT16389: Unspecified error. main.js, line 20 character 313" and the markers once crated are not updating with the new set of data 我收到“ SCRIPT16389:未指定的错误。main.js,第20行字符313”,一旦创建,标记也不会使用新的数据集进行更新

my newdata.json format is 我的newdata.json格式是

 {
    "points": [
        {
            "lat": "-28.0000",
            "long": "133.1500",
            "id": 0
        },
        {
            "lat": "-28.4710",
            "long": "153.3443",
            "id": 1
        }
    ]
}

and below is the script to get the json and use clustering to display the points 下面是获取json并使用聚类显示点的脚本

var map=null;
var markersArray = [];
var markerCluster= null;
google.load('maps', '3', {
        other_params: 'sensor=true'
});
google.setOnLoadCallback(initialize);

function initialize() {
    var mapcentre = new google.maps.LatLng(-29,135);
    var mapOptions = {
        zoom: 5,
        center: mapcentre,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
    startTimer();
}

function startTimer(){
    setInterval(function() {
        deleteOverlays();//should delete any existing point and clear the cluster
        addMarker();
    },3000);
}

function addMarker() {
        $.ajax({
            type: "GET",
            url: "newdata.json",
            async: false,
            dataType: "json",
            success: function(data){
                for (var i = 0, dataPoint; dataPoint = data.points[i]; i++) {
                    var latLng = new google.maps.LatLng(dataPoint.lat,dataPoint.long);
                    var marker = new google.maps.Marker({
                            position: latLng
                    });
                    markersArray.push(marker);
                }           

                markerCluster = new MarkerClusterer(map, markersArray);
            } 


        });
}


// Deletes all markers in the array by removing references to them
function deleteOverlays() {
  if (markersArray.length > 0) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
  if(markerCluster!= null) {
       markerCluster.clearMarkers();
  }
}

on IE8 the maps loads fine and the intial data is displaying fine, but the new data is not updated so i guess something wrong with deleteOverlays ? 在IE8上,地图加载正常,初始数据显示良好,但是新数据未更新,所以我猜deleteOverlays有问题吗?

the above example is based on http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/examples/advanced_example.html?compiled 上面的示例基于http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/examples/advanced_example.html?compiled

Well one thing that is very wrong is that for/in loops are intended for objects and not arrays. 好吧,非常错误的一件事是for / in循环用于对象而不是数组。 I've always hated IE but more and more often nowadays it seems to be the one that breaks when I wish everybody else would. 我一直很讨厌IE,但如今越来越多的情况似乎让我希望其他人都能打破。 It might be breaking on that. 它可能会破坏这一点。 What tends to happen in these fancy new JIT young whippersnapper browsers is that they will hit all the keys but also all the array properties which is idiotic, IMO. 在这些新颖的JIT年轻的“小鲷鱼”浏览器中,发生的往往是它们会击中所有键,而且会击中所有愚蠢的IMO数组属性。 This is a typing light and much more efficient way to loop through an array (keep in mind it goes backwards): 这是一种打字提示,是遍历数组的一种更为有效的方式(请记住,它会向后移动):

var i = markersArray.length
while(i--){
    markersArray[i].setMap(null);
}

If it's not this, check on the HTML. 如果不是,请检查HTML。 If you have busted up HTML the other browsers might be smart enough to figure out how to piece it together (again, too smart IMO) whereas IE8 will kick the bucket like a canary at ground zero of the manhattan project. 如果您破坏了HTML,则其他浏览器可能很聪明,可以弄清楚如何将其组合在一起(同样,IMO也太聪明了),而IE8将像曼哈顿的零地金丝雀一样kick倒水桶。

And no, really, stop using for/in on arrays. 而且,实际上,不要在数组上停止使用for / in了。 Even when it works it can be a complete mess. 即使工作正常,也可能是一团糟。

Adding cache:false, triggered IE8 to always get the latest json and updated the markers as intended. 添加cache:false,触发IE8始终获取最新的json并按预期更新标记。

 function addMarker() {
    $.ajax({
        type: "GET",
        url: "newdata.json",
        async: false,
        cache: false,
        dataType: "json",
        success: function(data){
            for (var i = 0, dataPoint; dataPoint = data.points[i]; i++) {
                var latLng = new google.maps.LatLng(dataPoint.lat,dataPoint.long);
                var marker = new google.maps.Marker({
                        position: latLng
                });
                markersArray.push(marker);
            }           

            markerCluster = new MarkerClusterer(map, markersArray);
        } 


    });

} }

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

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