两种方式:
第一种
要素服务的增删改操作,在ArcGIS API for JS中给我们提供了三个类用于要素的增Add
,删Delete
,改Update
添加draw和要素服务
//用于操作的要素图层,注意我们是操作的宿舍楼图层 var featureLayer = new FeatureLayer("http://localhost:6080/arcgis/rest/services/Test/ft/FeatureServer/1", { mode:FeatureLayer.MODE_SNAPSHOT, outFields: ["*"] }); //在添加要素时,利用Draw工具获得geometry对象 var d = new Draw(map, { showTooltips: true }); //要操作的graphic var g;
query("button").on("click",function(event){ //获得按钮的文本 var value=this.innerHTML; //根据文本绑定不同的事件 switch(value){ case "添加要素": d.activate(Draw.POLYGON); break; case "删除要素": //创建Graphic对象,删除OBJECTID为34的元素, //因为OBJECTID是主键,所以只需要指定主键即可 g=new Graphic("","",{ "OBJECTID":34 }); //创建删除对象 var de=new Delete({ "featureLayer":featureLayer, "deletedGraphics":[g] }); //执行删除结果 de.performRedo(); //刷新图层 layer.refresh(); break; case "修改要素": //用于修改要素的函数,查询更新为36的要素 updateFeature(36); break; case "查询要素": //用于查询要素的函数,查询主键为36的要素 searchFeather(36); break; } });
修改、更新:
var oldUpdateGrapgics; var updateGrapgics; var selectid = selectArray[0]["OBJECTID"]; var queryTask, query; queryTask = new esri.tasks.QueryTask(featureURL); //dojo.connect(queryTask, "onComplete", showResults); var queryWhere = "OBJECTID=" + selectid; //build query filter query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["*"]; query.where = queryWhere; queryTask.execute(query, queryUpdateResults); function queryUpdateResults(results) { var features = results.features; oldUpdateGrapgics = features[0]; updateGrapgics = new esri.Graphic(oldUpdateGrapgics.toJson()); //featureAttribution["OBJECTID"] = ; $("input[name=‘OBJECTID‘]").val(updateGrapgics.attributes["OBJECTID"]); $("input[name=‘ID‘]").val(updateGrapgics.attributes["ID"]); $("input[name=‘要素代码‘]").val(updateGrapgics.attributes["要素代码"]); $("input[name=‘图斑编号‘]").val(updateGrapgics.attributes["图斑编号"]); $("input[name=‘地类编码‘]").val(updateGrapgics.attributes["地类编码"]); $("input[name=‘城镇用地‘]").val(updateGrapgics.attributes["城镇用地"]); } function saveUpdateClick() { require(["esri/map", "dojo/on", "esri/layers/FeatureLayer", "esri/graphic", "esri/dijit/editing/Add", "esri/dijit/editing/Delete", "esri/dijit/editing/Update", "esri/tasks/query", "dojo/json", "dojo/domReady!" ], function(Map, on, FeatureLayer, Graphic, Add, Delete, Update, Query, jsonUtil) { //console.log($("#attrForm").serialize()); // attributes =$("#attrForm").serializeObject(); //将表单序列化为JSON对象 //console.log(attributes); // updateGrapgics.setAttributes(attributes); // editFeatureLayer.applyEdits(null, [updateGrapgics], null); updateGrapgics.attributes[‘要素代码‘] = getValueOrNULL($("input[name=‘要素代码‘]").val()); updateGrapgics.attributes[‘图斑编号‘] = getValueOrNULL($("input[name=‘图斑编号‘]").val()); updateGrapgics.attributes[‘城镇用地‘] = getValueOrNULL($("input[name=‘城镇用地‘]").val()); var update = new Update({ "featureLayer": editFeatureLayer, "postUpdatedGraphics": [updateGrapgics], //修改之后的要素 "preUpdatedGraphics": [oldUpdateGrapgics] //修改之前的要素 }); update.performRedo(); //刷新视图 editFeatureLayer.refresh(); alert("修改成功"); }); }
删除
var selectid = selectArray[0].OBJECTID; var selectids = []; selectids.push(selectid); $("#table").bootstrapTable(‘remove‘, { field: ‘OBJECTID‘, values: selectids }); //queryFeaturesByWhere(); var queryTask, query; queryTask = new esri.tasks.QueryTask(featureURL); //dojo.connect(queryTask, "onComplete", showResults); var queryWhere = "OBJECTID=" + selectid; //build query filter query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["*"]; query.where = queryWhere; queryTask.execute(query, deleteResults); function deleteResults(results) { var gs = results.features; editFeatureLayer.applyEdits(null, null, gs, function(evt) { console.log(evt); // alert("删除成功"); }); // return features; }
第二种:
applyEdits函数是FeatureLayer类中的一个方法,主要作用是修改featurelayer的要素,但仅对要素服务(feature service)
函数形式:applyEdits(adds?, updates?, deletes?, callback?, errback?)
返回类型:dojo.Deferred
参数表:
1)<Graphic[]> adds 可选项 要被添加到要素服务中的图层中的要素数组。新要素通常由Draw工具条创建。
2)<Graphic[]> updates 可选项 几何对象和属性有所改变的要素数组。要素必须有一个有效的OBJECTID。要素的几何对象通常由Edit工具条修改。属性使用Attribute Inspector修改。
3)<Graphic[]> deletes 可选项 待删除的要素数组,这些要素必须有有效的OBJECTID。
4)<Function> callback 可选项 当操作完成时本函数会被调用。传给本函数的参数和onEditsComplete事件中的一样。
5)<Function> callback 可选项 如果有错误发生将会返回一个错误对象。
例子:
//更新
require([ "esri/layers/FeatureLayer", ... ], function(FeatureLayer, ... ) { var firePerimeterFL = new FeatureLayer( ... ); var targetGraphic = firePerimeterFL.getSelectedFeatures()[0].setGeometry(reshapedGeometry); firePerimeterFL.applyEdits(null, [targetGraphic], null); ... });
//删除
function initSelectToolbar(evt) { var petroFieldsFL = evt.layers[0].layer; petroFieldsFL.on("dbl-click", function(evt) { event.stop(evt); petroFieldsFL.applyEdits(null, null, [evt.graphic]); }); }
原文:https://www.cnblogs.com/zhaoyanhaoBlog/p/9510547.html