目录
一、引言
前两篇文章介绍了以openlayers为工具通过WFS添加和修改要素,最后我们将介绍如何删除一个要素,修改的话比较简单,主要是id对应就可以。
二、WFS要素删除实现
本代码在使用interaction中的select操作,在“select”之后获取到feature,进行删除。
select.on('select', function (evt) {
var feature = evt.selected[0];
//feature.set('id_',feature.attr.id);
var format = new ol.format.WFS();
var xml = format.writeTransaction(null, null, [feature], {
featureNS: 'http://geoserver.org/nyc',
featurePrefix: "xcy",//工作空间名称
featureType: "polygon"//图层名称
});
var serializer = new XMLSerializer();
var featString = serializer.serializeToString(xml);
$.ajax({
url: "http://localhost:8080/geoserver/xcy/wfs",
type: "POST",
data: featString,
contentType: 'text/xml',
success: function (req) {
console.log(req);
//window.location.reload();
}
});
});
本例子中需要注意的地方,在上面代码确实没啥需要注意的,直接使用select就能获取到feature,向transaction中一放就行了,注意的点应该在另外的地方:这些选中的feature是如何加载上去的。
当你使用下面加载矢量数据作为数据源可以放心使用上面的代码
var polygonVectorSource = new ol.source.Vector({