2

I am working on a web app where I am making calls to a database api and it returns a JSON Object. I've managed to do so successfully as well as any manipulations that need be done.

Is there a way to add new fields to a FeatureLayer and the subsequently populate with the relevant data?

I do not necessarily want to edit the feature layer, just attach data on the fly so that I can then use it for analysis such as heat maps and what not. Have not really found anything concrete in the api documentation.

Ive tried the following code that results in an error. Fairly certain it is because i am trying to set the value before I add the Field.

featureLayer = new FeatureLayer(....);
fieldName = x;
value = y; 
featureLayer.graphics.attributes.fieldName = value;
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 6, 2016 at 22:49
1
  • ive also tried layer.graphics.setAttribute(fieldName,value); as well as layer.graphics[0].setAttribute(fieldName,value); Commented Jan 6, 2016 at 23:04

2 Answers 2

1

See the JS API documentation for Graphic attr() method:

https://developers.arcgis.com/javascript/jsapi/graphic-amd.html#attr

You would do this for each feature Graphic in the layer, after loaded:

for (var i=0; i< featureLayer.graphics.length; i++) {
 var lookupId = featureLayer.graphics[i].attributes.myLookupFieldId;
 ... perhaps get values for x, y using lookupId ...
 featureLayer.graphics[i].attr(x,y);
}

Note that this only adds the field to the members of the graphics collection in the map layer (on the fly, as you suggest). If the features are re-loaded, you would have to re-iterate and attach the data again.

You get the values from each graphic by referencing the esri attributes property:

var thisValue = thisGraphic.attributes[x];
 or, assuming x == "newFieldName"
var thisValue = thisGraphic.attributes.newFieldName; 

Best, Bill B

answered Jan 7, 2016 at 9:33
2
  • Thanks for your input @Bill B. This makes sense. Could you clarify this line of Code < var lookup = featureLayer.graphics[i].attributes.myLookupField>. Specificly the "mylookupField" portion? Commented Jan 7, 2016 at 15:17
  • It was my assumption that the value you are assigning (y) to each feature was unique for that feature. This was my attempt to suggest you use a field from the each feature (e.g. record id) to lookup/get the appropriate y value from your db/JSON data that you mentioned. In this case replace 'myLookupField' with '.id', or '.recordId' as appropriate from the existing feature fields. Commented Jan 8, 2016 at 16:50
1

Here is code that I found worked for this situation. Essentially, the code (not shown here) grabs the response of a POST method and parses it into javaScript variables. The bellow code injects the desired data into a feature layer.

addResponseData = function addResponseData (layer,fieldname,value) {
 for (feature in layer.graphics) {
 var attributes = layer.graphics[feature].attributes;
 attributes[fieldName] = value;
 }
}

I can then call this function and plug in the desired map layer, new field name, and its value.

addResponseData(FeatureLayer,"FieldName", Value);
answered Jan 12, 2016 at 18:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.