【问题标题】:How can i remove a graphic by id in the graphic layer如何在图形层中通过 id 删除图形
【发布时间】:2020-02-06 14:21:10
【问题描述】:

如何在图形层中按 id 删除图形。我正在使用 esri-javascript-api。希望任何答案。

【问题讨论】:

标签: esri arcgis-js-api


【解决方案1】:

假设graphicLayer 是地图图形的图层,id 属性命名为"id",您想删除id 为0 的图形;

var graphicLayer = map.graphics;
var idAttribute = "id";
var idValue = 0;

var toBeRemoved = graphicLayer.graphics.filter(function(graphic) {
  return graphic.attributes[idAttribute] == idValue;
})[0];

graphicLayer.remove(toBeRemoved);

您可以创建一个函数来批量执行此操作

function removeGraphicById(graphicLayer, idAttribute, idValue) {
    var toBeRemoved = graphicLayer.graphics.filter(function(graphic) {
      return graphic.attributes[idAttribute] == idValue;
    })[0];

    graphicLayer.remove(toBeRemoved);
};

var idsToDelete = [0, 1, 2, 3, 4];

idsToDelete.forEach(function(id) {
  //using map.graphics layer for instance
  removeGraphicById(map.graphics, "id", id);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2017-03-02
    • 1970-01-01
    • 2013-05-10
    • 2020-03-28
    • 2016-12-18
    相关资源
    最近更新 更多