Developing custom layers
Custom layers provide the ability to draw data in the Map control from data sources other than mobile cache, or be a dynamic drawing of information as a graphic layer. For example, you may want to display data from a text file or spreadsheet that is being updated from another application. To display this information in the client, you can write custom code on the server that reads the information into a geodatabase layer, then displays the information in a Map control as a regular layer, providing the client has connectivity. Alternatively, the client can read the data directly from the source and display the information as a custom layer in a client application. A custom graphic layer may be used to draw symbols or make notes on the map display interactively, or by reading an Internet feed containing location information. Note that the custom graphic layers are drawn on top of all the map data layers as part of the graphic layers collection.
Understanding custom layers
All layers in a Map control are derived from the Layer class. MapLayers that are sourced from ArcGIS Server or created from a map document through GP tools are stored within the MobileCache and are represented as a MobileCacheMapLayer. Custom layers are classes that inherit from the MapLayer class. The key difference of course, is the data source is not within the MobileCache. Once defined, the custom layer is added to a MapLayer collection for a Map control where it can be drawn when the map refreshes.
Normal layers know how to draw from the MobileCache using symbology defined from the original map document (from which mobile cache is generated from). However, for custom layers, you control how the data is read from the underlying data source and how it is drawn. The layer implementation provides an override Draw member, passing the Map control Display element, which allows you to read and display the data from your data source.
You are responsible for creating geometry from the data source, defining symbology from pens and brushes, and drawing features to the display. The Draw method on the custom layer is called each time the Map control is refreshed.
Implementing custom layers
The following steps show you how to implement a custom MapGraphicLayer in a mobile application.
- Add a new class within your application solution.
- Inherit the class from ESRI.ArcGIS.Mobile.WinForms.MapGraphicLayer. Add
constructors and supporting members as necessary.
public class myCustomClass:MapGraphicLayer { }
- Override the Draw Method. Add the code to extract and create
geometry from your data source. Draw the geometry.
// Draw geometry only if display object's DrawEnabled variable is set to true. if (display.DrawEnabled) mapSurface.DrawGeometry(m_pen, m_brush, m_pointSize, geometry);
- Add the custom layer to the maps layer
collection. This can be on a form load or some other event
as required.
myCustomClass newLayer = new myCustomClass(); // Insert layer on top. map.MapGraphicLayers.Insert(0,newLayer);
Sample application
For a full implementation, see Sample: Custom Graphic Layer.
This developer sample creates a custom layer by interactively adding geometries on the map. By clicking the map, a geometry point is created, then drawn on the map display using the selected symbology. If your data contains information for lines or polygons, you are responsible for creating the mobile geometries and drawing them with the appropriate symbology.
Custom layer tips
- Your data source may contain data that is outside the current display extent for the Map control. Add additional logic during the drawing phase to draw only the data that is within the current extent.
- Dispose of any objects that you consume within the class that implements IDisposeable. This typically includes any brushes or pens you define to draw the geometry.
For an example of these tips, see Sample: Custom Graphic Layer.