一、删除点
:::success
1、添加交互式的点画笔获取点的坐标
2、通过点的坐标,调用Point类的query方法查询fid
3、查询到要素的fid,调用删除服务
4、回调函数中更新图层
:::
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./dist/include-openlayers-local.js"></script>
<script src="./js/TIAN.js"></script>
<script src="./js/Draw.js"></script>
<script src="./edit/Point.js"></script>
</head>
<body>
<button onclick="active()">激活画笔</button>
<div id="map_container">
</div>
<script>
var docLayer = new Zondy.Map.Doc('', 'city', {
ip: 'localhost',
port: 6163
})
var map = new ol.Map({
target: "map_container",
layers: [gaodeMapLayer, docLayer],
view: new ol.View({
projection: 'EPSG:4326',
center: [114.30, 30.50],
zoom: 4
})
})
/*
1、基于点的交互式查询 坐标
*/
//1-1 创建画布
var source = new ol.source.Vector({
wrapX: false
});
var layer = new ol.layer.Vector({
source
})
map.addLayer(layer);
let draw;
function active() {
draw = createDraw({
type: "Point",
source,
callback: handleDraw
})
map.addInteraction(draw)
}
function handleDraw(e) {
//2、根据坐标调用Point类的query方法,查询到fid
var position = e.feature.getGeometry().getCoordinates();
Point.query({
position,
service: {
name: "city",
layerId: 0,
},
querySuccess
})
}
/* 3、查询到FID调用删除服务 */
function querySuccess(result) {
try {
var FID = result.SFEleArray[0].FID
console.log(FID)
if (confirm("你确认要删除吗")) {
const deleteService = new Zondy.Service.EditDocFeature(
'city',
0, {
ip: "localhost",
port: 6163
}
)
deleteService.deletes(FID, deleteSuccess)
}
} catch (err) {
console.log("查询不到要素")
}
}
/* 4、删除成功的回调 */
function deleteSuccess(data) {
if (data.succeed) {
alert('删除成功')
docLayer.refresh(); //重新加载地图文档
} else {
alert('删除失败')
}
}
</script>
</body>
</html>
二、封装删除
因为删除具备通用性,可以删除点要素。也可以删除线,面要素。所以可以单独封装 DeleteFeature.js
function delFeature({
FID,
service,
layer
}) {
const deleteService = new Zondy.Service.EditDocFeature(
service.name,
service.layerId, {
ip: "localhost",
port: 6163
}
)
deleteService.deletes(FID, deleteSuccess(layer))
}
function deleteSuccess(layer) {
return function (data) {
if (data.succeed) {
alert('删除要素成功')
layer.refresh(); //重新加载地图文档
} else {
alert('删除要素失败')
}
}
}