我很难在JSON对象的临时数组中更新我的对象。
主要目标是:
这是我的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小提琴都会有所帮助。
谢谢!