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.

Steps:
  1. Add a new class within your application solution.
  2. Inherit the class from ESRI.ArcGIS.Mobile.WinForms.MapGraphicLayer. Add constructors and supporting members as necessary.

    public class myCustomClass:MapGraphicLayer
     {
     }
    

  3. 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);
    

  4. 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

For an example of these tips, see Sample: Custom Graphic Layer.

1/7/2015