Hello Everyone,
Can someone clarify if it is possible to draw features using SketchViewModel like in this sample Sketch temporary geometries | ArcGIS API for JavaScript 4.7 and then using applyEdits function to save the polygons as a feature layer?
Thank you,
Deepanjana
Solved! Go to Solution.
Deepanjana,
There should be no issue using the draw-complete event to create a graphic and add it to a layer using the applyEdits method of a FeatureLayer.
Deepanjana,
There should be no issue using the draw-complete event to create a graphic and add it to a layer using the applyEdits method of a FeatureLayer.
Robert,
Thank you - your suggestion worked. I have a follow-up question. I am now trying to update the geometry of the feature when it is clicked using sketchViewModel.update():
view.on("click", function(evt) {
view.hitTest(evt).then(function(response) {
if (response.results.length > 0 && response.results[0].graphic) {
var feature = response.results[0].graphic;
selectFeature(feature.attributes[featureLayer.objectIdField]);
inputDescription.value = feature.attributes["TITLE"];
inputAddress.value = feature.attributes["ADDRESS"];
attributeEditing.style.display = "block";
updateInstructionDiv.style.display = "none";
sketchViewModel.update(feature.geometry);
}
});
In the corresponding update-start and update-complete events, I am trying to updateFeatures of applyEdits method:
sketchViewModel.on("update-complete", modifyGraphic);
sketchViewModel.on("update-start", modifyGraphic);
function modifyGraphic(evt) {
var geometry = evt.geometry;
var modifyPoly = new Graphic({
geometry: geometry,
symbol: polygonSymbol,
attributes: {}
});
var edits = {
updateFeatures: [modifyPoly]
};
applyEdits(edits);
}
Deepanjana,
You are capturing the graphic that the user clicks on before you do sketchViewModel.update
so you should update the geometry of that feature/graphic using the evt.geometry and then pass that graphic to the applyEdits not a new graphic.
Thanks Robert.
My understanding is that in:
sketchViewModel.update(feature.geometry);
feature.geometry already has the geometry of the selected feature obtained from evt click before the update-start and update-complete events are triggered. I have changed modifyGraphic() to:
sketchViewModel.on("update-complete", modifyGraphic);
sketchViewModel.on("update-start", modifyGraphic);
function modifyGraphic(evt) {
editFeature.geometry = evt.geometry;
var edits = {
updateFeatures: [editFeature]
};
applyEdits(edits);
}
where editFeature is a global var. However, the behavior of resizing the graphic still remains the same --- I can click and drag the vertices but when I click outside the graphic, the changes are gone (not registered in the featureLayer). Is there a code snippet/example illustrating how applyEdits updateFeatures work with sketchViewModel update?
Deepanjana,
I do not know of any snippet/example for this. If editFeature is a global var set to the graphic that sketchViewModel.update is using then your code should be correct. The applyEdits documentation for updateFeatures states this:
Array of features to be updated. Each feature must have valid objectId. Values of non nullable fields must be provided when updating features. Date fields must have numeric values representing universal time.