arcgis api for js入門開發系列十九圖層在線編輯
本篇主要講述的是利用arcgis api實現圖層在線編輯功能模塊,效果圖如下:
實現思路:
1.arcgis server發布的FeatureServer服務提供的圖層在線編輯能力:
2.實現的在線編輯(增刪改),主要是通過前端ajax請求後台FeatureServer服務來實現的
http://120.199.78.138:6080/arcgis/rest/services/dlsde/FeatureServer/0/updateFeatures
public void ProcessRequest (HttpContext context) { string featureserverurl = context.Request.Params["featureserverurl"]; string features = context.Request.Params["features"]; string param = "features=" + features + "&f=json"; string url = featureserverurl + "/updateFeatures"; string ret = PostDataToUrl(param, url, "application/x-www-form-urlencoded"); context.Response.Write(ret);}
http://120.199.78.138:6080/arcgis/rest/services/dlsde/FeatureServer/0/addFeatures
public void ProcessRequest (HttpContext context) { string featureserverurl = context.Request.Params["featureserverurl"]; string features = context.Request.Params["features"]; string param = "features=" + features + "&f=json"; string url = featureserverurl + "/addFeatures"; string ret = PostDataToUrl(param, url, "application/x-www-form-urlencoded"); context.Response.Write(ret); }
http://120.199.78.138:6080/arcgis/rest/services/dlsde/FeatureServer/0/deleteFeatures
public void ProcessRequest (HttpContext context) { string featureserverurl = context.Request.Params["featureserverurl"]; string id = context.Request.Params["OBJECTID"]; string param = "where=OBJECTID=" + id + "&f=json"; string url = featureserverurl + "/deleteFeatures"; string ret = PostDataToUrl(param, url, "application/x-www-form-urlencoded"); context.Response.Write(ret); context.Response.End(); }
3.前端ajax請求
(1)添加一條記錄:
var features = []; var rec = {}; rec.attributes = {}; rec.geometry = DCI.editLayers.geometry; rec.attributes["NAME"] = $("#update_name").val(); rec.attributes["KIND"] = $("#update_kind").val(); features.push(rec); var feats = JSON.stringify(features); /*[{ "attributes": { "NAME": "112", "KIND": "4080" }, "geometry": { "type": "point", "x": 121.29894825018249, "y": 39.72910692098025, "spatialReference": { "wkid": 4326 } } }]*/ var params = { features: feats, f: "pjson", featureserverurl: MapConfig.sdeURL }; $.ajax({ type: "post", //dataType: "json", url: getRootPath() + "handler/AddFeatureHandler.ashx", data: params, //async: false,//同步 success: function (response, textStatus) { var ret = JSON.parse(response); if (ret.addResults && ret.addResults[0].success) { promptdialog("提示", "添加成功!"); DCI.editLayers.InitSde(""); DCI.editLayers.map.infoWindow.hide(); } else { promptdialog("提示", "添加失敗!"); } }, error: function (e) { var error = e; promptdialog("提示", "響應超時!"); } });
(2)修改一條記錄:
var features = []; var rec = {}; rec.attributes = {}; rec.attributes["OBJECTID"] = parseInt(objectid); rec.attributes["NAME"] = $("#update_name").val(); rec.attributes["KIND"] = $("#update_kind").val(); features.push(rec); var feats = JSON.stringify(features); /*[{ "attributes": { "NAME": "112", "KIND": "4080" }, "geometry": { "type": "point", "x": 121.29894825018249, "y": 39.72910692098025, "spatialReference": { "wkid": 4326 } } }]*/ var params = { features: feats, f: "pjson", featureserverurl: MapConfig.sdeURL }; $.ajax({ type: "post", //dataType: "json", url: getRootPath() + "handler/UpdateFeatureHandler.ashx", data: params, //async: false,//同步 success: function (response, textStatus) { var ret = JSON.parse(response); if (ret.updateResults[0].success) { promptdialog("提示", "更新成功!"); DCI.editLayers.InitSde(""); DCI.editLayers.map.infoWindow.hide(); } else { promptdialog("提示", "更新失敗!"); } }, error: function (e) { var error = e; promptdialog("提示", "響應超時!"); } });
(3)刪除一條記錄:
var objectid = parseInt(id); var params = { OBJECTID: objectid, featureserverurl: MapConfig.sdeURL }; $.ajax({ type: "post", //dataType: "json", url: getRootPath() + "handler/DeleteFeatureHandler.ashx", data: params, //async: false,//同步 success: function (response, textStatus) { var ret = JSON.parse(response); if (ret.success) { promptdialog("提示", "刪除成功!"); DCI.editLayers.InitSde(""); DCI.editLayers.map.infoWindow.hide(); } else { promptdialog("提示", "刪除失敗!"); } }, error: function (e) { var error = e; promptdialog("提示", "響應超時!"); } });
備註:團隊承接webgis/gis畢業設計以及webgis項目等業務,歡迎有相關需求的客戶來諮詢
GIS之家接受webgis開發遇到的技術疑點難點在線諮詢收費模式,有需要的加QQ:406503412,具體詳情見:諮詢模式
GIS作品:GIS之家
GIS之家知乎專欄:GIS之家知乎專欄
GIS之家交流群一:432512093(已滿)
GIS之家交流群二:296438295(已滿)
GIS之家交流群三:632467934
推薦閱讀:
※arcgis api for js入門開發系列七圖層控制
※詳細的介紹如何用arcgis計算全局自相關係數?最好有截圖
※用arcgis可以做哪些好玩的事?
※開篇——GIS的再定義
※利用C#製作ArcMap插件