Can I draw simple geometries and save them as features on my feature layer in JS API 4.x?

2231
5
03-16-2018 01:11 AM
DeepanjanaMajumdar2
New Contributor II

I found this sample for JS API 3.x Edit without editor widget | ArcGIS API for JavaScript 3.23  and I want to do something similar in JS 4.x, I only found this sample for now https://developers.arcgis.com/javascript/latest/sample-code/editing-applyedits/index.html, but here it's just adding point data. Is there something similar like the sample in v3 available in v4 where I can draw, edit and delete a polygon and have it saved as a feature layer? If yes, it would be helpful if someone could point me to some documentation or samples.

Thanks!

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Deepanjana,

   In the current release 4.6 there is no sample or editing tools. But you can combine the SketchViewModel and the editing sample you provided above to do what you want.

SketchViewModel | API Reference | ArcGIS API for JavaScript 4.6 

Sketch temporary geometries | ArcGIS API for JavaScript 4.6 

DeepanjanaMajumdar2
New Contributor II

Robert, 

Thanks a lot, will try it out.

0 Kudos
SimonWebster
Occasional Contributor

Deepanjana, Did you ever get this working? If so, can you share your code, please?

0 Kudos
DeepanjanaMajumdar2
New Contributor II

Simon, I did get it to work, I used Update FeatureLayer using applyEdits() | ArcGIS API for JavaScript 4.9.  I used sketchviewmodel to draw the features and then do applyEdits()

something like this

var sketchViewModel = new SketchViewModel({
    view: view,
    layer: featureLayer,
    polygonSymbol: polygonSymbol
  });

    // ************************************************************
    // Get the completed graphic from the event and add it to view.
    // This event fires when user presses
    //  * "C" key to finish sketching point, polygon or polyline.
    //  * Double-clicks to finish sketching polyline or polygon.
    //  * Clicks to finish sketching a point geometry.
    // ***********************************************************
    sketchViewModel.on("draw-complete", addGraphic);
    sketchViewModel.on("update-complete", modifyGraphic);
    //sketchViewModel.on("update-cancel", modifyGraphic);
    //sketchViewModel.on("update-start", modifyGraphic);

    function addGraphic(evt) {
      var geometry = evt.geometry;
      newIncident = new Graphic({
        geometry: geometry,
        symbol: polygonSymbol,
        attributes: {}
      });

      var edits = {
        addFeatures: [newIncident]
      };

      applyEdits(edits);

    }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
SimonWebster
Occasional Contributor

Deepanjana,

Thank you. That’s great.

For anyone finding this in the future, Deepanjana and Robert Scheitlins replies are combined in to a working 1 page solution here: View the full discussion<https://community.esri.com/message/811443-re-how-to-create-a-polygon-js-api-49?commentID=811443&et=watches.email.thread#comment-811443>

Simon

0 Kudos