如何在图形层中按ID删除图形

时间:2019-10-09 09:18:20

标签: esri arcgis-js-api

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

1 个答案:

答案 0 :(得分:0)

假设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);
});