Programatically add feature to Feature Layer with ArcGIS JavaScript API

7128
2
Jump to solution
01-27-2015 08:18 AM
DanielGray
Deactivated User

I've so far been able to add a feature layer from an ArcGIS server, and I've also been able to add an editor widget which allows me to draw to feature with the mouse. The added features are being correctly committed to the server, and are visible from other viewers as well as in the SDE database. I have also been able to add the Editor toolbar and edit the existing polygon features.

However, I'm now looking for a way to add a feature without using the widget, just using JavaScript code. The closest thing I found was http://forums.esri.com/Thread.asp?c=206&f=2274&t=271592 but it discusses .NET and doesn't look similar to the way something like that would be achieved in Javascript. Where would the best starting point be for doing something like this?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You can use the applyEdits method of the FeatureLayer to do this. I am writing an application where I am just changing the attributes of selected features. Since I'm allowing the user to make changes to the attributes of multiple features (featureSet), I'm not using the AttributeInspector, but a custom dialog where the user can select specific attributes from comboBoxes.

array.forEach(featureSet, function (feature) {
    feature.attributes.Priority = registry.byId('cboPriority').get("value");
    feature.attributes.Management = registry.byId('cboManagement').get("value");
    feature.attributes.Criteria = registry.byId('cboCriteria').get("value");
});

layerFeatureLayer.applyEdits(null, featureSet, null, function () { console.log("Features updated!"); }, function (error) { console.log("Features not updated! ", error); });

In your case, you would use the first parameter of the applyEdits method to add a new feature. You would have to handle the geometry of the feature and make sure its attributes are set.

View solution in original post

2 Replies
BrentPorter
Occasional Contributor

If you want to simulate the full functionality of the widget, then you are in for a long haul. But it is possible to add in access to the feature layer by using graphics layers or a feature layer (to build the geometry) and then using the REST API's feature service (this is NOT the javascript API's feature layer) to do the addition.

Here is a link to the REST API Feature Service info on adding features.

ArcGIS REST API

This takes in JSON and then returns a response. I've done it and it works pretty well. You will most likely be interested in setting up authentication on the service too - and the link above has other references to it for using OAUTH 2 and other methods for authenticating access to REST endpoints.

Cheers

Brent

KenBuja
MVP Esteemed Contributor

You can use the applyEdits method of the FeatureLayer to do this. I am writing an application where I am just changing the attributes of selected features. Since I'm allowing the user to make changes to the attributes of multiple features (featureSet), I'm not using the AttributeInspector, but a custom dialog where the user can select specific attributes from comboBoxes.

array.forEach(featureSet, function (feature) {
    feature.attributes.Priority = registry.byId('cboPriority').get("value");
    feature.attributes.Management = registry.byId('cboManagement').get("value");
    feature.attributes.Criteria = registry.byId('cboCriteria').get("value");
});

layerFeatureLayer.applyEdits(null, featureSet, null, function () { console.log("Features updated!"); }, function (error) { console.log("Features not updated! ", error); });

In your case, you would use the first parameter of the applyEdits method to add a new feature. You would have to handle the geometry of the feature and make sure its attributes are set.