Javascript 如何使用JSON数据同时更新多个标记位置?传单JS

Javascript 如何使用JSON数据同时更新多个标记位置?传单JS,javascript,json,dictionary,leaflet,Javascript,Json,Dictionary,Leaflet,我正在使用它构建一个交互式地图查看器,可以跟踪多个设备的移动。我使用JSON数据为每个标记设置纬度和经度 JSON数据如下所示: {"BMS":[{"id":"PR01","type":"prajurit","lat":"-6.248008","long":"107.004158"},{"id":"PR02","type":"prajurit","lat":"-6.224084","long":"106.653069"},{"id":"PR03","type":"kendaraan","lat"

我正在使用它构建一个交互式地图查看器,可以跟踪多个设备的移动。我使用JSON数据为每个标记设置纬度和经度

JSON数据如下所示:

{"BMS":[{"id":"PR01","type":"prajurit","lat":"-6.248008","long":"107.004158"},{"id":"PR02","type":"prajurit","lat":"-6.224084","long":"106.653069"},{"id":"PR03","type":"kendaraan","lat":"-6.244316","long":"106.649734"}]}
数据包含来自3个设备的位置信息

我已经成功地基于设备的纬度和经度设置了标记,并使用
for
循环。但我无法使用
setLatLng
for
循环更新标记的移动。我不想在每次获得位置更新时使用“清除层”功能添加新标记,因为我需要保存每个设备的移动历史,以备以后使用。我该怎么做才能让它工作?如有任何建议,将不胜感激

这是我的密码
setInterval
用于连续运行代码。我尝试使用数组变量作为标记变量,并标记每个标记(因为使用windows[variable_name]是一种糟糕的做法)

window.setInterval(函数(){
++z;
//警报(z);
功能剂量测量(数据){
jsonBmsData=JSON.parse(数据);
}
$.get('get_jsondata.php',doSomething);
如果(z==1){
对于(变量i=0;i 1){

对于(var j=0;j我在调用
setLatLng
方法时没有看到任何错误,但我认为,在

var newLatLng = new L.latLng(marker[j][2],marker[j][3]); 

当您想要创建新的长lat对象时,您应该调用
new L.LatLng()
(用大L字母)或者只调用
L.LatLng()
,这是一个相同的快捷方法,它调用
new L.LatLong()
最后。检查您的属性
newLatLng
是否为正确的对象。

您可以使用一个对象来存储标记,标记id为propertyname,标记为属性值。这将使事情变得更容易。要存储以前的位置,您可以向每个标记添加一个数组来存储它们。这里是带有注释的代码解释:

// The object to store markers
var markers = {};

// Function for setting/updating markers
function setMarkers(data) {

  // Loop over the BMS array and handle each object
  data.BMS.forEach(function (obj) {

    // Check if there is already a marker with that id in the markers object
    if (!markers.hasOwnProperty(obj.id)) {
      // No marker with that id found in the markers object

      // Create it, and add it to the map, store into markers object
      markers[obj.id] = new L.Marker([obj.lat, obj.long]).addTo(map);

      // Add array to the marker instance to store the previous latlngs
      markers[obj.id].previousLatLngs = [];

    } else {
      // There is already a marker with that id in the markers object

      // Store the previous latlng
      markers[obj.id].previousLatLngs.push(markers[obj.id].getLatLng());

      // Set new latlng on marker
      markers[obj.id].setLatLng([obj.lat, obj.long]);
    }
  });
}
这里有一个关于Plunker的工作示例

传单有一个非常酷的插件,名为Realtime。它完全满足您的需要,而且更多,但它需要一个GeoJSON集合作为数据,因此它无法与您的数据集一起工作。如果您能够将数据转换为GeoJSON集合,那么值得一看:

GeoJSON:

演示:


Repo:

Ehm,问题出在哪里?您的第二个循环不工作?调用
markerMove[j].setLatLng(newLatLng).update()
不起作用?我看到在第二个循环中,您初始化了
j=0
,条件是您使用
I
,但是
j
变量从未更新。@xxxmatko,谢谢您的更正。我编辑了代码。是的,
markerMove[j]。setLatLng(newLatLng)。update();
没有给我任何结果。我做错了吗?需要你的帮助。谢谢你的帮助。是的,我已经更正了,但仍然没有给我任何结果。然后,我尝试用一个简单的代码捕捉错误,它说“markerMove[j]未定义”。我不明白为什么它是未定义的。或者可能我使用数组的方式不正确?你能通过更正的代码吗?你的
标记器
标记移动
在哪里定义?以下是我用来检查错误的简单代码:。请检查它。谢谢。非常感谢!它很漂亮!我不认为这是工作可以用object来完成,你启发了我!是的,也许使用数组来完成这项工作是一个坏主意,因为我一直遇到未定义的数组错误。这让我很沮丧,因为没有教程或示例来使用object来完成这项工作。我将尝试在我的代码中应用它。谢谢!你救了我!不,谢谢,你总是受欢迎的。数组对象它只是一个奇特的对象,可以跟踪它的长度,并具有一些额外的功能,如forEach方法来循环它的属性等等。请参阅:普通对象可以使用字符串作为键,而数组不能。因此,在这种情况下,它非常方便。当使用数组并要查找标记时,需要执行以下操作:
markers.forEach(function(marker){if(marker.id==obj.id){//found}}}
但是现在你可以简单地做:
markers[obj.id]
刚刚记住有一个很好的插件,在我的答案中添加了它。
// The object to store markers
var markers = {};

// Function for setting/updating markers
function setMarkers(data) {

  // Loop over the BMS array and handle each object
  data.BMS.forEach(function (obj) {

    // Check if there is already a marker with that id in the markers object
    if (!markers.hasOwnProperty(obj.id)) {
      // No marker with that id found in the markers object

      // Create it, and add it to the map, store into markers object
      markers[obj.id] = new L.Marker([obj.lat, obj.long]).addTo(map);

      // Add array to the marker instance to store the previous latlngs
      markers[obj.id].previousLatLngs = [];

    } else {
      // There is already a marker with that id in the markers object

      // Store the previous latlng
      markers[obj.id].previousLatLngs.push(markers[obj.id].getLatLng());

      // Set new latlng on marker
      markers[obj.id].setLatLng([obj.lat, obj.long]);
    }
  });
}