arcgis for javascript api 新增要素

 arcgis for javascript api feature class applyedits 将自定义坐标系新增到图层


其中遇到的坑:

1.未开启feature server
2.arcgis默认为地理坐标系统(我们需要将地理坐标系转换成投影坐标系)即SpatialReference wkid=4326需要改成与sde中新建feature class中坐标系一致


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");


最后效果: