Developing custom layers

Custom layers provide the ability to draw data in the map control from data sources other than ArcGIS Server. For example, you may wish to display data from a text file or spreadsheet that is being updated from another application. To display this information in the client you could 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.

Understanding custom layers

All layers in a map control are derived from the Layer class. Layers that are sourced from ArcGIS Server are stored within the MapCache and are represented as a CacheLayer, a kind of Layer. Custom layers are simply classes that inherit from the Layer class, the key difference of course is the data source is not within the MapCache. Once defined, the custom layer is added to a map collection for a map control where it can be drawn when the map is refreshed.

Normal layers know how to draw themselves from the MapCache using symbology defined from the original map document published via ArcGIS Server. For custom layers however 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, which allows you to read and display the data from your data source.

You are responsible for creating geometry from the datasource, defining the symbology from pens and brushes, and drawing the features to the display. The Draw method on the custom layer is called each time the Map is refreshed.

Implementing custom layers

The following steps show you how to implement a custom layer to an existing mobile application.

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

    public class myCustomClass:ESRI.ArcGIS.Mobile.Layer
     {
     }

  3. Override the Draw Method. Add your code to extract and create geometry from your datasource. Draw the geometry.

    // Draw geometry only if display object's DrawEnabled variable is set to true. 
    if 
     (display.DrawEnabled) 
    display.DrawGeometry(m_pen, m_brush, m_pointSize, geometry);

  4. Add the custom layer to the maps layer collection. This could either be on form load or some other event as required.

    myCustomClass newLayer = new myCustomClass();
    // Insert layer on top 
    map.MapLayers.Insert(0,newLayer);

Sample application

For a full implementation, see the Sample Custom Graphic Layer sample.

This developer sample creates a custom layer from an excel spreadsheet. The rows contain lat/long point information. As each row is read, a geometry point is created then drawn on the map display using a fixed symbology. If your data contained information for lines or polygons you are responsible for creating the mobile geometries and drawing them with appropriate symbology.

Custom layer tips

An example of these tips can be seen in the Sample Custom Graphic Layer sample.


9/20/2011