一、删除点

:::success 1、添加交互式的点画笔获取点的坐标
2、通过点的坐标,调用Point类的query方法查询fid
3、查询到要素的fid,调用删除服务
4、回调函数中更新图层 :::

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./dist/include-openlayers-local.js"></script>
  8. <script src="./js/TIAN.js"></script>
  9. <script src="./js/Draw.js"></script>
  10. <script src="./edit/Point.js"></script>
  11. </head>
  12. <body>
  13. <button onclick="active()">激活画笔</button>
  14. <div id="map_container">
  15. </div>
  16. <script>
  17. var docLayer = new Zondy.Map.Doc('', 'city', {
  18. ip: 'localhost',
  19. port: 6163
  20. })
  21. var map = new ol.Map({
  22. target: "map_container",
  23. layers: [gaodeMapLayer, docLayer],
  24. view: new ol.View({
  25. projection: 'EPSG:4326',
  26. center: [114.30, 30.50],
  27. zoom: 4
  28. })
  29. })
  30. /*
  31. 1、基于点的交互式查询 坐标
  32. */
  33. //1-1 创建画布
  34. var source = new ol.source.Vector({
  35. wrapX: false
  36. });
  37. var layer = new ol.layer.Vector({
  38. source
  39. })
  40. map.addLayer(layer);
  41. let draw;
  42. function active() {
  43. draw = createDraw({
  44. type: "Point",
  45. source,
  46. callback: handleDraw
  47. })
  48. map.addInteraction(draw)
  49. }
  50. function handleDraw(e) {
  51. //2、根据坐标调用Point类的query方法,查询到fid
  52. var position = e.feature.getGeometry().getCoordinates();
  53. Point.query({
  54. position,
  55. service: {
  56. name: "city",
  57. layerId: 0,
  58. },
  59. querySuccess
  60. })
  61. }
  62. /* 3、查询到FID调用删除服务 */
  63. function querySuccess(result) {
  64. try {
  65. var FID = result.SFEleArray[0].FID
  66. console.log(FID)
  67. if (confirm("你确认要删除吗")) {
  68. const deleteService = new Zondy.Service.EditDocFeature(
  69. 'city',
  70. 0, {
  71. ip: "localhost",
  72. port: 6163
  73. }
  74. )
  75. deleteService.deletes(FID, deleteSuccess)
  76. }
  77. } catch (err) {
  78. console.log("查询不到要素")
  79. }
  80. }
  81. /* 4、删除成功的回调 */
  82. function deleteSuccess(data) {
  83. if (data.succeed) {
  84. alert('删除成功')
  85. docLayer.refresh(); //重新加载地图文档
  86. } else {
  87. alert('删除失败')
  88. }
  89. }
  90. </script>
  91. </body>
  92. </html>

二、封装删除

因为删除具备通用性,可以删除点要素。也可以删除线,面要素。所以可以单独封装 DeleteFeature.js

  1. function delFeature({
  2. FID,
  3. service,
  4. layer
  5. }) {
  6. const deleteService = new Zondy.Service.EditDocFeature(
  7. service.name,
  8. service.layerId, {
  9. ip: "localhost",
  10. port: 6163
  11. }
  12. )
  13. deleteService.deletes(FID, deleteSuccess(layer))
  14. }
  15. function deleteSuccess(layer) {
  16. return function (data) {
  17. if (data.succeed) {
  18. alert('删除要素成功')
  19. layer.refresh(); //重新加载地图文档
  20. } else {
  21. alert('删除要素失败')
  22. }
  23. }
  24. }