1.引言
在現實需求中,我們不僅僅需要編輯客戶端圖層,也有可能去編輯服務器端圖層,要想編輯服務器端的圖層,必須要將地圖發佈爲:要素服務。
如何發佈要素服務請看:ArcGIS Server發佈要素服務
2.需求(服務器端圖層編輯)
關於服務器端圖層的操作,基本就是增刪改查操作,要素服務的查詢前面已經介紹過,在此篇博客中主要記錄一下關於要素服務的增刪改操作,在ArcGIS API for JS中給我們提供了三個類用於要素的增Add
,刪Delete
,改Update
,接下來就用這三個類來實現我們的功能。
- 我們操作的是要素服務的sushelou圖層
- 我們的服務:
注意:宿舍樓圖層編號爲1
- 圖層的屬性信息
2.1實現
- 首先在頁面添加三個按鈕(用於實現用戶的增刪改操作)
<button class="btn">添加要素</button>
<button class="btn">刪除要素</button>
<button class="btn">修改要素</button>
<button class="btn">查詢要素</button>
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 添加地圖(省略)
- 創建通用的對象,例如
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;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 給相應的按鈕添加綁定事件
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;
}
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 如果是添加操作(利用draw添加geometry屬性)
//當畫圖完畢時,添加要素
on(d, "draw-complete", function (result) {
//要素只賦予了geometry,屬性信息爲空
var graphic = new Graphic(result.geometry, null,{});
var add=new Add({
"featureLayer":featureLayer,//給哪一個要素圖層添加要素
"addedGraphics":[graphic]//用於添加的要素
})
//執行添加函數
add.performRedo();
//刷新視圖
layer.refresh();
//關閉繪圖對象
d.deactivate();
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 如果是查詢操作
//根據id查詢要素
function searchFeather(id,callback){
//得到要素圖層的主鍵屬性
var idProperty = featureLayer.objectIdField;
//定義查詢參數
var query = new Query();
//是否返回幾何形狀
query.returnGeometry = false;
//圖層的主鍵名稱(根據自己要求修改)
query.objectIds = [id];
//查詢條件1=1意思是:只根據主鍵查詢,忽略where子句
query.where = "1=1";
//進行查詢
featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW,function(result){
//如果callback不存在,說明僅僅是查詢操作
if(!callback){
//因爲我們根據主鍵查詢,一定只有一個元素
var graphic=result[0];
//獲得屬性
var attributes=graphic.attributes;
//得到該屬性信息轉換成字符串
var result=jsonUtil.stringify(attributes)
//將結果彈出一下
alert(result);
}else//如果callback存在,說明是更新操作
{
callback(result);
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 如果是更新操作
//根據id修改要素
function updateFeature(id){
//注意:(第二個參數)這裏傳入一個回調函數,用於處理查詢出來的數據
searchFeather(id,function(result){
//獲得舊的要素
var oldgraphic=result[0];
//新的要素
var newgraphic=new Graphic(oldgraphic.toJson());
//將alias屬性修改爲:修改後的C區
newgraphic.attributes.alias="修改後的C區";
//創建更新對象
var update=new Update({
"featureLayer":featureLayer,
"postUpdatedGraphics":[newgraphic],//修改之後的要素
"preUpdatedGraphics":[oldgraphic]//修改之前的要素
})
//執行刷新操作
update.performRedo();
//刷新視圖
layer.refresh();
alert("修改成功");
})
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
2.2實現結果
2.2.1添加操作
- 添加要素前:
- 添加要素後:
2.2.2刪除要素操作
- 刪除要素前:
- 刪除要素後:
2.2.3更新要素(可以看到objectid=1的alias屬性爲“A區宿舍“)
2.2.4當修改之後點擊查詢按鈕結果:
2.3全部代碼
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>WebGIS</title>
<link rel="stylesheet" type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/dijit/themes/tundra/tundra.css"/>
<link rel="stylesheet" type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/esri/css/esri.css" />
<script type="text/Javascript" src="http://localhost/arcgis_js_api/library/3.17/3.17/init.js"></script>
<style type="text/css">
.MapClass{
width:100%;
height:500px;
border:1px solid #000;
}
</style>
<script type="text/javascript">
require(["esri/map",
"dojo/on",
"dojo/query",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/toolbars/draw",
"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,query, ArcGISDynamicMapServiceLayer, Draw,FeatureLayer,Graphic,Add,Delete,Update,Query,jsonUtil) {
var map = new esri.Map("MyMapDiv");
var layer = new ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/Test/ft/MapServer");
map.addLayer(layer);
//用於操作的要素圖層,注意我們是操作的宿舍樓圖層
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(1);
break;
case "查詢要素":
//用於查詢要素的函數,查詢主鍵爲36的要素
searchFeather(1);
break;
}
});
//當畫圖完畢時,添加要素
on(d, "draw-complete", function (result) {
//要素只賦予了geometry,屬性信息爲空
var graphic = new Graphic(result.geometry, null,{});
var add=new Add({
"featureLayer":featureLayer,//給哪一個要素圖層添加要素
"addedGraphics":[graphic]//用於添加的要素
})
//執行添加函數
add.performRedo();
//刷新視圖
layer.refresh();
//關閉繪圖對象
d.deactivate();
});
//根據id修改要素
function updateFeature(id){
//注意:(第二個參數)這裏傳入一個回調函數,用於處理查詢出來的數據
searchFeather(id,function(result){
//獲得舊的要素
var oldgraphic=result[0];
//新的要素
var newgraphic=new Graphic(oldgraphic.toJson());
//將alias屬性修改爲:修改後的A區宿舍
newgraphic.attributes.alias="修改後的A區宿舍";
//創建更新對象
var update=new Update({
"featureLayer":featureLayer,
"postUpdatedGraphics":[newgraphic],//修改之後的要素
"preUpdatedGraphics":[oldgraphic]//修改之前的要素
})
//執行刷新操作
update.performRedo();
//刷新視圖
layer.refresh();
alert("修改成功");
})
}
//根據id查詢要素
function searchFeather(id,callback){
//得到要素圖層的主鍵屬性
var idProperty = featureLayer.objectIdField;
//定義查詢參數
var query = new Query();
//是否返回幾何形狀
query.returnGeometry = false;
//圖層的主鍵名稱(根據自己要求修改)
query.objectIds = [id];
//查詢條件1=1意思是:只根據主鍵查詢,忽略where子句
query.where = "1=1";
//進行查詢
featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW,function(result){
//如果callback不存在,說明僅僅是查詢操作
if(!callback){
//因爲我們根據主鍵查詢,一定只有一個元素
var graphic=result[0];
//獲得屬性
var attributes=graphic.attributes;
//得到該屬性信息轉換成字符串
var result=jsonUtil.stringify(attributes)
//將結果彈出一下
alert(result);
}else//如果callback存在,說明是更新操作
{
callback(result);
}
});
}
});
</script>
</head>
<body>
<div id="MyMapDiv" class="MapClass"></div>
<button class="btn">添加要素</button>
<button class="btn">刪除要素</button>
<button class="btn">修改要素</button>
<button class="btn">查詢要素</button>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
(function () { ('pre.prettyprint code').each(function () {
var lines =
for (i = 1; i <= lines; i++) {
};
$numbering.fadeIn(1700);
});
});