How to add features to a feature Layer

7618
5
04-13-2012 10:52 AM
lanceweaver
New Contributor III
You would think this would be easy.     I want to manually add features  (from results of a FindTask) to my feature layer.

All examples in the help add such results as graphics directly to the Map [with map.graphics.add() ].  But I would like to add them to one of my FeatureLayers.  [esri.layers.FeatureLayer[myFeatureLayer].graphics]

This is what the help does (which I dont want to do)

                var items = dojo.forEach(results,function(rslt){
                    var desc = "<p>Name :"+rslt.attributes.TITLE+"<br>State:</p>";
                    var graphic = rslt;
                    graphic.setSymbol(defaultSymbol);
                    graphic.setInfoTemplate(infoTemplate);
                    map.graphics.add(graphic);
                    return rslt.attributes;
                });


This is what I want to do.  The features are added, but they do not display on the map even if I call .refresh() or readd the featureLayer.

                var items = dojo.map(results,function(rslt){
                    return rslt.feature;
                });

              myFeatureLayer.graphics = items;


Is this not possible?  It seems to me that adding features/graphics to a featureLayer is more flexible than just adding them roguely to the map.  Can they only be added with featureLayer.selectFeatures() or at creation of the new FeatureLayer?    I would like to add my featureLayer with MODE_SELECTION so nothing is fetched from the server, and then query the server with a FindTask and then add the results to my feature Layer.  I know how to do it with a .selectFeatues(query)  but I want to do it with a findTask not a query.
0 Kudos
5 Replies
derekswingley1
Frequent Contributor
How about creating your feature layer from a feature collection and then using applyEdits to push your features from the find task into your feature layer?

I'm suggesting the feature collection route because if you were to use applyEdits with a feature layer created from a URL, the feature layer will try to push those features back to the server.
0 Kudos
MihkelOviir
New Contributor
Basically, you can do as example shows:

featureLayer.add(graphic);

So if items in your code is array of esri graphics, then:

for(var i in items){
myFeatureLayer.add(items);
}
0 Kudos
lanceweaver
New Contributor III
.add() is not a supported method for the featureLayer Class.   see http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/featurelayer.htm

The response above is the correct one.   The feature collection is exactly for this purpose, it is a featureLayer designed to add features to manually. I did not realize this when i made the post.   It has essentially all the same methods, properties and events as a featureLayer.
0 Kudos
MihkelOviir
New Contributor
Where it says, that add() is not supported? Look at featureLayer hierarchy. Do you want to say, that hide() and show() and all others useful parents layers methods is not supported also? Maybe, but I still use them...
0 Kudos
lanceweaver
New Contributor III
my bad.  .add() can be used on all graphic layer classes.
0 Kudos