不在ajax调用中从数组中删除

时间:2017-04-17 14:09:19

标签: javascript php jquery ajax google-maps

我有一个通过AJAX从PHP文件调用GPS坐标的功能,可以在地图上显示它们,这一切都很好。

该函数检查它获取的标记是否已经在地图上,如果它是否更新了它的位置,如果它不在地图上,它也将它添加到地图中。

我现在需要的东西似乎无法正常工作,我需要删除以前使用过的不再更新的标记。

这是我的代码:

setInterval(
            $.ajax({
                type: "POST", 
                url: 'markers.php',
                success: function(data){
                    var json_obj = jQuery.parseJSON(JSON.stringify(data));
                    for (var i = 0; i < json_obj.length; i++) {
                        newcoordinate = new google.maps.LatLng(json_obj[i].lat, json_obj[i].lng);
                        if (markersArray[json_obj[i].name] && markersArray[json_obj[i].name].setPosition){
                            markersArray[json_obj[i].name].setPosition(newcoordinate);
                            if(json_obj[i].type == 'onderweg') {
                                markersArray[json_obj[i].name].setIcon(onderweg);
                                var label = markersArray[json_obj[i].name].getLabel();
                                label.color="black";
                                markersArray[json_obj[i].name].setLabel(label);
                            }
                            if(json_obj[i].type == 'leeg') {
                                markersArray[json_obj[i].name].setIcon(leeg);
                                var label = markersArray[json_obj[i].name].getLabel();
                                label.color="black";
                                markersArray[json_obj[i].name].setLabel(label);
                            }
                            if(json_obj[i].type == 'bezet') {
                                markersArray[json_obj[i].name].setIcon(bezet);
                                var label = markersArray[json_obj[i].name].getLabel();
                                label.color="white";
                                markersArray[json_obj[i].name].setLabel(label);
                            }
                            if(json_obj[i].type == 'afwachten') {
                                markersArray[json_obj[i].name].setIcon(afwachten);
                                var label = markersArray[json_obj[i].name].getLabel();
                                label.color="black";
                                markersArray[json_obj[i].name].setLabel(label);
                            }
                            if(json_obj[i].type == 'pauze') {
                                markersArray[json_obj[i].name].setIcon(pauze);
                                var label = markersArray[json_obj[i].name].getLabel();
                                label.color="black";
                                markersArray[json_obj[i].name].setLabel(label);
                            }
                            if(json_obj[i].type == 'geendienst') {
                                markersArray[json_obj[i].name].setIcon(geendienst);
                                var label = markersArray[json_obj[i].name].getLabel();
                                label.color="black";
                                markersArray[json_obj[i].name].setLabel(label);
                            }
                        } else {
                            addMarker(json_obj[i].lat, json_obj[i].lng, json_obj[i].name, json_obj[i].afkorting, json_obj[i].type);
                        }

                    }
                },
                dataType: "json"//set to JSON    
            })    
        }
    ,3000);

有人可以指出我正确的方向来解决这个问题吗?

Json数据:

[{"id":"1505108","0":"1505108","name":"Benny","1":"Benny","afkorting":"Be","2":"Be","address":"","3":"","speed":"0","4":"0","lat":"53.198181","5":"53.198181","lng":"6.563437","6":"6.563437","type":"onderweg","7":"onderweg","ts":"2017-04-17 18:26:34","8":"2017-04-17 18:26:34","ritid":"483648","9":"483648","bedrijf":"1","10":"1","ipadres":"188.207.111.80","11":"188.207.111.80","versie":"10","12":"10","inactief":"0","13":"0","beta":"0","14":"0","onzichtbaar":"0","15":"0"},{"id":"1505110","0":"1505110","name":"Rene","1":"Rene","afkorting":"Re","2":"Re","address":"","3":"","speed":"0","4":"0","lat":"53.182274","5":"53.182274","lng":"6.577085","6":"6.577085","type":"leeg","7":"leeg","ts":"2017-04-17 18:26:37","8":"2017-04-17 18:26:37","ritid":"0","9":"0","bedrijf":"1","10":"1","ipadres":"188.207.126.119","11":"188.207.126.119","versie":"10","12":"10","inactief":"0","13":"0","beta":"0","14":"0","onzichtbaar":"0","15":"0"},{"id":"1505112","0":"1505112","name":"Peter","1":"Peter","afkorting":"Pr","2":"Pr","address":"","3":"","speed":"0","4":"0","lat":"53.196129","5":"53.196129","lng":"6.581492","6":"6.581492","type":"bezet","7":"bezet","ts":"2017-04-17 18:26:39","8":"2017-04-17 18:26:39","ritid":"483650","9":"483650","bedrijf":"1","10":"1","ipadres":"92.69.203.230","11":"92.69.203.230","versie":"10","12":"10","inactief":"0","13":"0","beta":"0","14":"0","onzichtbaar":"0","15":"0"},{"id":"1505113","0":"1505113","name":"Ivar","1":"Ivar","afkorting":"Iv","2":"Iv","address":"","3":"","speed":"0","4":"0","lat":"53.360897","5":"53.360897","lng":"6.414318","6":"6.414318","type":"onderweg","7":"onderweg","ts":"2017-04-17 18:26:39","8":"2017-04-17 18:26:39","ritid":"483649","9":"483649","bedrijf":"1","10":"1","ipadres":"188.207.122.118","11":"188.207.122.118","versie":"10","12":"10","inactief":"0","13":"0","beta":"0","14":"0","onzichtbaar":"0","15":"0"},{"id":"1505114","0":"1505114","name":"Niels","1":"Niels","afkorting":"Ni","2":"Ni","address":"","3":"","speed":"0","4":"0","lat":"53.205700","5":"53.205700","lng":"6.586080","6":"6.586080","type":"bezet","7":"bezet","ts":"2017-04-17 18:26:40","8":"2017-04-17 18:26:40","ritid":"483633","9":"483633","bedrijf":"1","10":"1","ipadres":"92.69.223.185","11":"92.69.223.185","versie":"9","12":"9","inactief":"0","13":"0","beta":"0","14":"0","onzichtbaar":"0","15":"0"},{"id":"1505109","0":"1505109","name":"John","1":"John","afkorting":"Jo","2":"Jo","address":"","3":"","speed":"0","4":"0","lat":"53.200897","5":"53.200897","lng":"6.596990","6":"6.596990","type":"leeg","7":"leeg","ts":"2017-04-17 18:26:36","8":"2017-04-17 18:26:36","ritid":"0","9":"0","bedrijf":"1","10":"1","ipadres":"92.69.230.150","11":"92.69.230.150","versie":"10","12":"10","inactief":"0","13":"0","beta":"0","14":"0","onzichtbaar":"0","15":"0"}]

数组回声:

[_.Ne, _.Ne, _.Ne, _.Ne, _.Ne, _.Ne, Benny: _.Ne, Rene: _.Ne, Peter: _.Ne, Ivar: _.Ne, Niels: _.Ne…]

这就是我得到的......但是每个对象都有自己的值,脚本确实有效..

1 个答案:

答案 0 :(得分:0)

我没有机会对此进行全面测试,但你可以尝试一下。我会说这不是很有效率。循环遍历两个数组,但至少循环不是嵌套的。

首先稍微更改您的成功处理程序(添加空行以突出显示)。

            success: function(data){

                var idArray = [];

                var json_obj = jQuery.parseJSON(JSON.stringify(data));
                for (var i = 0; i < json_obj.length; i++) {

                    idArray.push(json_obj[i].id);

                    newcoordinate = new google.maps.LatLng(json_obj[i].lat, json_obj[i].lng);
                    ...

然后循环遍历标记数组(此处称为数组,使用数组名称)并拼接新idArray中不存在的标记数组。

for (var i = array.length - 1; i >= 0; i--)
{
    if (idArray.indexOf(array[i].id) < 0) array.splice(i, -1);
}

通过反向工作,你不会搞砸早期数组成员的索引。

我在地图应用程序中有一系列点,我必须动态添加和删除点。在那里我们替换数组,然后重新渲染地图。

有关拼接的更多信息 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice