Data collection in ArcGIS Mobile
Data collection involves an editlayer, the feature layer that receives the new feature, and a sketch, the drawing of the new feature on the map display. The code below shows how to set an editlayer, change the mapaction to addvertexsketchtool, and create an empty geometry based on the type of the editlayer.
// sets map action to add vertex sketch
map1.CurrentMapAction = addVertexSketchTool1;
// selects the Construction layer to add feature
editlayer = mobileCache1.Layers["Valve Inspections"] as FeatureLayer;
if (editlayer == null)
{
throw new NullReferenceException("Could not find the specified feature layer");
}
GeometryType geometryType = editlayer.GeometryType;
// creates a new empty instance of a geometry based on geometry type of the layer
switch (geometryType)
{
case GeometryType.Polygon:
sketchGraphicLayer1.Geometry = new Polygon();
break;
case GeometryType.Polyline:
sketchGraphicLayer1.Geometry = new Polyline();
break;
case GeometryType.Point:
sketchGraphicLayer1.Geometry = new ESRI.ArcGIS.Mobile.Geometries.Point();
break;
case
GeometryType.Multipoint:
sketchGraphicLayer1.Geometry = new Multipoint();
break;
}
When the code above is executed, the mouse cursor can be used on the display to create a new feature.
The code below is used to signal that the datacollection of the feature's geometry is completed. The process involves creating a new row for the featuredatatable, adding it to the table, assigning the geometry, and saving the table changes.
//used to signal geometry has been created for new feature
private void map1_DoubleClick(object sender, EventArgs e)
{
try
{
if (map1.CurrentMapAction != addVertexSketchTool1)
return;
FeatureDataTable fTable = editlayer.GetDataTable();
// creates a new row
DataRow editedFeature = fTable.NewRow();
//sets the new geometry to the feature layer data table
fTable.Rows.Add(editedFeature);
//sets the new geometry to the geometry field
editedFeature[editlayer.GeometryColumnIndex] = sketchGraphicLayer1.Geometry;
// updates the feature layer data table
fTable.SaveInFeatureLayer();
// clean the sketch
sketchGraphicLayer1.Geometry = null;
//now use selection to select new feature and modify
map1.CurrentMapAction = selectionMapAction1;
}
catch (Exception ex)
{
MessageBox.Show("Invalid geometry. " + ex.ToString());
// clean the invalid geometry
sketchGraphicLayer1.Geometry = null;
}
}
To add attributes to a new feature, use the featuredatatable in the same way as when adding the geometry; it just involves different columns or fields in the row.
Where to get the sample
The sample is available for download from here.