Select to view content in your preferred language

Adding a point to a featurelayer using coordinates

667
3
11-18-2011 09:41 AM
jonataspovoas
Regular Contributor
Hi,

I'm making a silverlight application with the ArcGIS API for Silverlight, and in it i need to do online edition to mark constructions, and i'm having a lot of trouble with a specific case.

I need to insert a mark by receiving the X,Y coordinates. I tryed adding at the graphics list on the feature layer, with it auto and manual saving, and didn't work. My second attempt was with the Editor class, but the add command requires that the user click on the map, and I can't bypass it, no matter what i try.

I need to know if there's any other way to add map points at the feature layers and save it.
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
Subscribe to FeatureLayer.EndSaveEdits and SaveEditsFailed event, it would be useful to know if the save is successful. You can also monitor web requests using Fiddler.

To add a graphic in code-behind, you can set Geometry and Attributes this way. Be sure that layer has initialized and update is completed. You may want to include null checks here.
//Grab the FeatureLayer of interest
            var l = MyMap.Layers["MyFeatureLayerID"] as FeatureLayer;

//Create the graphic, set its Geometry with your X,Y values
            var g = new Graphic()
            {
                Geometry = new MapPoint(x, y, MyMap.SpatialReference)
            };

//Set graphic.Attributes
//One way of setting graphic.Attributes is to give them default values. 
//GetDefaultValueForFieldType() is something you need to write
            foreach (var f in l.LayerInfo.Fields)
            {
                if (f.Editable)
                    g.Attributes[f.Name] = f.Nullable ? null : GetDefaultValueForFieldType(f.Type);
            }

//Another way is to get the PrototypeAttributes that your sevice have defined.
//You can get this from either FeatureTypes or Templates (use one of the two following lines)
            var prototypeAttributes = l.LayerInfo.FeatureTypes.FirstOrDefault().Value.Templates.FirstOrDefault().Value.PrototypeAttributes;
            var prototypeAttributes = l.LayerInfo.Templates.FirstOrDefault().Value.PrototypeAttributes;

            foreach (var a in prototypeAttributes)
                g.Attributes[a.Key] = a.Value;

//Finally, add the graphic to your GraphicCollection. On AutoSave=True (which is default), 
//this will fire a save request. Otherwise, you need to call l.SaveEdits();
            l.Graphics.Add(g);
0 Kudos
jonataspovoas
Regular Contributor
Hi,

I tried to implement your sugestion, but it didn't save.

Here's the current code I'm working on:
private void InsertByCoordinates(object parameter)
{
 Map map = parameter as Map;
 if (map != null)
 {
  FeatureLayer editableLayer = map.Layers["Editable Layer"] as FeatureLayer;

  Graphic graphic = new Graphic()
  {
   Geometry = new MapPoint(double.Parse(X), double.Parse(Y), map.SpatialReference)
  };

  var atributes = editableLayer.LayerInfo.Templates.FirstOrDefault().Value.PrototypeAttributes;

  foreach (var atribute in atributes)
  {
   graphic.Attributes[atribute.Key] = atribute.Value;
  }

  graphic.Attributes.Remove("CODItemIMustPlace");
  graphic.Attributes.Add("CODItemIMustPlace", InfoObject.CodItemIMustPlace);

  editableLayer.Graphics.Add(graphic);

  editableLayer.SaveEdits();
 }
}


It executes and place the graphic on the right place, but it doesn't save...
0 Kudos
jonataspovoas
Regular Contributor
hi again,

It actually worked, I was my mistake... I was cleaning the value of the "InfoObject" just before trying to get it... when i discovered it, I almost had a heart attack...

Well, thanks a lot for the help!
0 Kudos