Jquery,更新数组

时间:2016-03-15 20:51:09

标签: javascript jquery arrays json object

我很难在JSON对象的临时数组中更新我的对象。

主要目标是:

  1. 添加不常用的对象。已经在map_Markers数组中。
  2. 如果具有相同ID的对象,则更新位置
  3. 如果JSON没有,则从map_Marker数组中删除标记。
  4. 这是我的JSON输出及其变化很多,具体取决于用户输入

    {
        "markers": [{
            "ID": "1",
            "Area": "SomeArea1",
            "Status": "on",
            "Latitude": 58.96674,
            "Longitude": 16.204277,
            "Class": "A",
            "Time": "2016-03-12 00:21"
        }, {
            "ID": "3",
            "Area": "SomeArea",
            "Status": "off",
            "Latitude": 59.000351,
            "Longitude": 16.227819,
            "Class": "A",
            "Time": "2016-03-12 00:22"
        }]
    }
    

    JS

    var loadMarkers = function () {
        $.getJSON('map/map_json.php', function (data) {
            $.each(data.markers, function (i, index) {
    
                console.log("i =" + i + " Index =" + index.ID);
    
                if (map_Markers[i].ID === index.ID) {
                    console.log("Updating marker position!");
                    map_Markers[i].setPosition(new google.maps.LatLng(index.Latitude, index.Longitude));
    
                } else if (map_Markers[i].ID != index.ID) {
                    console.log("Remove Marker!");
                    map_Markers.splice(i, 1);
    
                } else {
                    console.log("Creating new Marker!");
                    var myLatlng = new google.maps.LatLng(index.Latitude, index.Longitude);
    
                    var marker = new google.maps.Marker({
                        position: myLatlng,
                        map: map,
                        title: index.Area,
                        id: index.ID
                    });
                    console.log(index.ID);
                    map_Markers.push(marker);
                } //If
    
    
            }); //each loop
            console.log("map_Marker Array : " + map_Markers.length + " Object");
            console.log(map_Markers);
        }); //getJSON
    }
    

    任何帮助和maby小提琴都会有所帮助。

    谢谢!

0 个答案:

没有答案