尝试一下在线预览此示例演示如何使用 FeatureLayer.applyEdits() 创建新要素、更新现有要素的属性或删除现有要素。此示例使用 FeatureTemplates 微件来显示来自可编辑要素图层的模板。微件侦听最终用户以在小部件中选择特定模板。它的 select
事件被触发并返回结果模板信息。这用于创建新要素。
除了 FeatureTemplates 微件,此示例还使用 FeatureForm 微件来更新新要素或现有要素的属性。
在此示例中,applyEdits
函数在用户出现以下情况时被调用:
- 单击以创建新要素。
- 选择视图上的要素并更新其属性。
- 选择视图上的要素并将其删除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Call FeatureLayer.applyEdits() with specified params.
function applyEdits(params) {
addFeatureBtnDiv.style.display = "none";
addTemplatesDiv.style.display = "none";
unselectFeature();
featureLayer
.applyEdits(params)
.then((editsResult) => {
// Get the objectId of the newly added feature.
// Call selectFeature function to highlight the new feature.
if (editsResult.addFeatureResults.length > 0) {
const objectId = editsResult.addFeatureResults[0].objectId;
selectFeature(objectId);
}
})
.catch((error) => {
console.log("error = ", error);
});
}
FeatureLayer.applyEdits() 解析为包含编辑结果的对象。如果编辑失败,则编辑结果包含错误。