如何在图形层中按ID删除图形。我正在使用esri-javascript-api。希望任何答案。
答案 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);
});