【问题标题】:Leaflet array of markers that need to move not recreate需要移动而不是重新创建的传单标记数组
【发布时间】:2023-01-13 11:57:04
【问题描述】:

首先 - 我是一个新手,在过去的几年里,我一直在挑选对我有用的东西。我下面的代码基本上可以工作——它很复杂,我相信一定有更好的方法。寻找不同方向的踢球。

我正在使用 javascript 和传单来显示一组移动标记(玩家)。标记上的数据在表格中出现——列出每个标记的 ID、位置和其他不需要的信息。数据表将具有不断变化的标记数量(即:只有一个(MYID),或者加上另一个,两个或更多“其他”),并且每个标记都有多个条目(相同的 ID 和位置,只是其他数据不同)。

其中一个标记 id 与“MYID”相同,然后我对其进行不同的处理,但对于其他标记(如果有的话),我有一个函数可以将它们放在我的地图上。每次调用该函数时,它都应该将它们移动到它们的新位置数据,但我目前不知道如何优雅地做到这一点。所以我目前正在寻找删除所有并在每次调用函数时重新创建它们(每次发生任何变化都会触发)。不太好编码。

function updatemap(displaytable,MYID) {  // Update other player locations on map    
   OPN=[]; //reset OPN array
   OPX=[]; //reset OPX array
   OPY=[]; //reset OPY array
   for (var r=0;r<OPLoc.length;r++){
       // Need to remove all OPloc off map first and then re-create them?
   };
   OPLoc=[];
   var q=0;
   for (var p=0; p<displaytable.length; p++){ // for every line of the displaytable 
      if ((!OPN.includes(displaytable[p].id)) && (displaytable[p].id != MYID)){ // ... create a unique other player entry, once only
          OPN.push(displaytable[p].id);
          OPX.push(displaytable[p].lat);
          OPY.push(displaytable[p].lon);
          OPLoc[q] = new L.marker([displaytable[p].lat,displaytable[p].lon], {icon: oplayericon})
             .addTo(mymap)
             .bindPopup(displaytable[p].id + "<br>" + displaytable[p].lat + "," + displaytable[p].lon);
          q++;
      };
   };
//...other code not relevant
};

函数中的任何变量/数组在其他任何地方都不需要 - 我刚刚创建了所有这些混乱来做这件事。数组是全局创建的,以便下次调用时可用。

我假设数组命令有一些我不知道的功能/能力。谁能提出更好的方法?请?

【问题讨论】:

    标签: javascript arrays leaflet


    【解决方案1】:

    好吧,仍然不确定它是否优雅——但它很实用。不确定是否有我没有涵盖的边缘情况 - 但这似乎有效并且可以满足我的需要。我现在只使用两个数组(一个用于标记,一个用于 id,这样我就可以跟踪活动的内容)。我尝试制作一个二维数组,但这让我很伤心,所以现在是两个数组。

       for (var s=0;s<displaytable.length;s++) { // display all other players
        if (OPid.includes(displaytable[s].id)) { //if the player is already on the map, just update location
            OPLoc[OPid.indexOf(displaytable[s].id)].setLatLng([displaytable[s].lat,displaytable[s].lon]);
        } else { // if the player is not MYID and not already on the map, create it
            if (displaytable[s].id != MYID) {
                OPid.push(displaytable[s].id);
                OPLoc[OPid.indexOf(displaytable[s].id)] = new L.marker([displaytable[s].lat,displaytable[s].lon], {icon: oplayericon})
                .addTo(mymap)
                .bindPopup(displaytable[s].id);
                };
        };
       }; 
       for (var r=0;r<OPid.length;r++) { //check if the OPlayer is still active
            var found = false;
            for(var q=0;q<displaytable.length;q++) {
                 if (displaytable[q].id == OPid[r]) {
                    found = true;
                 };
            }; 
            if (!found) { //if the OPlayer id is no longer in the displaytable
                mymap.removeLayer(OPLoc[r]); //remove it from the map
                OPLoc.splice(r,1); //remove it from the OPLoc array
                OPid.splice(r,1); //remove it form the OPid array
            };
       };
        ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多