Select to view content in your preferred language

Graphic Attribute Methods

2758
2
05-07-2012 08:28 AM
LukeCatania
Occasional Contributor
What happened to the setAttributeValue and removeAttribute methods on the Class Graphic?  The only way I see to set attributes is to include them when instantiating the graphic.  In my app, I draw a polygon in a graphics layer and then have a popup dialog box asking the user to enter info which will populate the attributes.   So my graphic exist before having attributes.  How do I populate the attributes after I already instantiated the graphic?

I tried updateGraphic.   I used the constructor that allow you to assign an ID.  I then created another graphic with attributes and used the updateGraphic method, but itdid not work.  I found that my first graphic did did not contain the graphic ID I assigned, but some random one.  How else can I do this?

Why was setAttributeValue and removeAttribute methods removed?  Seems to make sense to be able to have such methods.
0 Kudos
2 Replies
LukeCatania
Occasional Contributor
What happened to the setAttributeValue and removeAttribute methods on the Class Graphic?  The only way I see to set attributes is to include them when instantiating the graphic.  In my app, I draw a polygon in a graphics layer and then have a popup dialog box asking the user to enter info which will populate the attributes.   So my graphic exist before having attributes.  How do I populate the attributes after I already instantiated the graphic?

I tried updateGraphic.   I used the constructor that allow you to assign an ID.  I then created another graphic with attributes and used the updateGraphic method, but itdid not work.  I found that my first graphic did did not contain the graphic ID I assigned, but some random one.  How else can I do this?

Why was setAttributeValue and removeAttribute methods removed?  Seems to make sense to be able to have such methods.


Actually the graphic ID is being assigned.  The issue is when I query the graphics layer and use getGraphicIDs to get the ID of the queried graphic, that ID is not the one I assigned.  I only have one graphic drawn and only one is returned from the query.  Why is the number assigned not returned?
0 Kudos
DanO_Neill
Deactivated User
You can populate the attributes of a graphic after it has been created using the GraphicsLayer.updateGraphic() method.  Once a Graphic is added to a GraphicsLayer it is assigned a UID.  This is currently by design so you cannot specify a UID on a new Graphic.  The overloaded Graphic constructor that takes a UID parameter is an issue we are working on.  As you have observed, when you assign a UID with that constructor it will not be honored with a returned Graphic from GraphicsLayer.  We are looking to address this issue.  If I understand your scenario correctly, you need to update a graphic with attributes based on user clicking on the graphic.  You can use the screen coords x, y from the MapView listener to identify which Graphic you want to update.  Below is an example:   

mMapView.setOnLongPressListener(new OnLongPressListener() {

 private static final long serialVersionUID = 1L;

 @Override
 public void onLongPress(float x, float y) {
  int[] guids = gLayer.getGraphicIDs(x, y, 10);
    
  if(guids.length != 0){
   Graphic gr = gLayer.getGraphic(guids[0]);
   int uid = gr.getUid();
   Map<String,Object> attr = gr.getAttributes();
   attr.put("name", "Test");
   gLayer.updateGraphic(uid, attr);
   Log.d("Test", "Graphic attr update name = "+ gr.getAttributeValue("name"));     
  }
}
0 Kudos