Defining the Renderer |
The renderer we are going to define will store a list of points that are to be marked on the map.
Create a new class called PointRenderer and have it implement the IMapRenderer interface.
public class PointRenderer : IMapRenderer { private ArrayList points; public PointRenderer() { points = new ArrayList(); } // This implements the IMapRenderer interface public void Render(Graphics graphics, RenderContext rc) { } // Required for the IMapRenderer interface -- describes when this renderer draws public RenderMode RequiredRendermodes { } // This method is used to add a point to the renderer public void AddPoint(LatLon point) { } }
Here we have the skeleton of the class we are going to create. The Render method is required to implement the IMapRenderer. The AddPoint method is our own method to allow us to specify points to draw on the map.
Now we will complete these methods. First the AddPoint method:
public void AddPoint(LatLon point) { points.Add(point); }
Now the Render method. This method is passed two parameters by the map to make rendering simple.
public void Render(Graphics graphics, RenderContext rc) { IMap aMap = rc.Map; // If we are zoomed a long way out, don't draw anything if (aMap.Zoom > 2000) { return; } // Draw at middle priority: before labels, but after PreLabelling if (rc.Mode == RenderMode.Labelling) { int x, y; Brush brush = new SolidBrush(Color.FromArgb(150, Color.Blue)); // Iterate over the collection of points foreach (object p in points) { LatLon point = (LatLon)p; // Convert the coordinate from lat/lon to screen coordinates aMap.LatLontoXY(out x, out y, point); // Create a Rectangle containing the area to be drawn in System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x-5, y-5, 10, 10); // Reserve space on the map via RenderContext rc.Place(rect, 0, 0); // Draw a marker at the right location, a circle of radius 5 pixels graphics.FillEllipse(brush, rect); } } // fi }
Finally the RequiredRendermodes property. This controls in which stage of Rendering our renderer draws.
public RenderMode RequiredRendermodes { get { return RenderMode.Labelling; } }
We have defined our renderer, and in the previous step we created the interface. Now, proceed to the final step of Linking the Two.