arcgis for javascript api feature class applyedits 将自定义坐标系新增到图层
其中遇到的坑:
1.未开启feature server
2.arcgis默认为地理坐标系统(我们需要将地理坐标系转换成投影坐标系)即SpatialReference wkid=4326需要改成与sde中新建feature class中坐标系一致
发布featureserver服务
1、在ARCCatalog中连接sde数据库在数据库中新建featureclass
2、右键新建featureclass加载shp数据
3、在ARCMap中新建空白模板,拖动ARCCatalog新建的featureclass到图层layers中
4、修改图层属性
5、发布服务
6、编辑要素
arcgis for javascript代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
<script src="https://js.arcgis.com/4.9/"></script>
<script>
function addFeature(){
var polygon={
type: "polygon", // autocasts as new Polygon()
rings: [
[465373.67283938953, 4574685.605095486],
[465448.7368793035, 4574662.143328166],
[465442.06908027834, 4574640.809887505],
[465367.0048759384, 4574664.271654826],
[465373.67283938953, 4574685.605095486]
]
};
var url="http://localhost:6080/arcgis/rest/services/BP/BP/FeatureServer/0";
applyEdits(polygon,url);
}
function applyEdits(polygon,url){
require(['esri/layers/FeatureLayer',
"esri/geometry/Point",
"esri/geometry/Polygon",
"esri/geometry/SpatialReference",
'esri/Graphic']
, function(FeatureLayer,Point,Polygon,SpatialReference,Graphic){
var _layer = new FeatureLayer(url,{
mode: FeatureLayer.MODE_ONDEMAND,
outFields: ["*"]
});
//102100
polygon.spatialReference = new SpatialReference({ wkid: 102100 });
var graphics = new Graphic({geometry: polygon});
const addFeature = {
addFeatures: [graphics]
};
_layer.applyEdits(addFeature).then(function(editsResult) {
if (editsResult.addFeatureResults.length > 0) {
const objectId = editsResult.addFeatureResults[0].objectId;
}
}).catch(function(error) {
console.error("[ applyEdits ] FAILURE: ", error.code, error.name,
error.message);
console.log("error = ", error);
});
})
}
</script>
</head>
<body>
<a href="#" onclick="addFeature()">添加图元</a>
</body>
</html>
arcgis java中请求,注意设置 request contenttype为application/x-www-form-urlencoded
String params = "f=json&adds=[{\"geometry\":{\"spatialReference\":{\"wkid\":"+wkid+"},\"rings\":["+ gson.toJson(ring) + "]},\"attributes\":null}]";
String rep = HttpUtils.post(url, params, "application/x-www-form-urlencoded");
最后效果: