This project is read-only.

add label to a point

Mar 21, 2013 at 2:19 PM
I've researched this and tried many ways for a couple of days and it either does nothing or blows up.

I can easily add a point to a map using:
Coordinate coord = MainForm.GeographicToWebMercator ( equipLat, equipLng );
            DotSpatial.Topology.Point point = new DotSpatial.Topology.Point ( coord );
            feature = this.dbMarkers.AddFeature ( point );
Now I want to be able to add a text label on the map at that point.

There have been many questions and some proposals but if someone has a sample of real code that does this it would sure be appreciated.
Mar 24, 2013 at 11:56 AM
Success, and is it ever easy.

In your main class: (I am using the DemoMap example)
public FeatureSet dbMarkers = null;
public MapPointLayer dbMarkerLayer;
public MapLabelLayer dbLabelLayer;

In your housekeeping or config section:
        ProjectionInfo wgs = map1.Projection;

        // The FeatureSet starts with no data; be sure to set it to the point featuretype
        this.dbMarkers = new FeatureSet ( FeatureType.Point );
        this.dbMarkers.Projection = wgs;

        this.dbMarkers.DataTable.Columns.Add("label").DataType = System.Type.GetType("System.String");

        // The MapPointLayer controls the drawing of the marker features
        this.dbMarkerLayer = new MapPointLayer ( this.dbMarkers );
        this.dbLabelLayer = new MapLabelLayer ( dbMarkerLayer );
        dbMarkerLayer.LabelLayer = this.dbLabelLayer;

        // this is the expression for the label drawing
        this.dbLabelLayer.Symbology.Categories [ 0 ].Expression = "[label]";

        // The Symbolizer controls what the points look like
        this.dbMarkerLayer.Symbolizer = new PointSymbolizer ( Color.Blue, DotSpatial.Symbology.PointShape.Star, 15 );
        this.dbMarkerLayer.Symbolizer.IsVisible = true;
        this.dbLabelLayer.Symbolizer.FontSize = 9F;

        //add feature set to map
        map1.Layers.Add ( this.dbMarkerLayer );
        this.dbMarkerLayer.LegendText = "Points From Sqlite";
        this.dbMarkerLayer.ShowLabels = true;

        this.dbLabelLayer.LegendText = "Point Labels";

In your database routine, probably the loop where you extract the queried items:
            coord = MainForm.GeographicToWebMercator ( equipLat, equipLng );
            point = new DotSpatial.Topology.Point ( coord );

            feature = this.dbMarkers.AddFeature ( point );
            label = "[" + equipDescription + "]";
            feature.DataRow [ "label" ] = label;

It does not require a Label layer. This method attaches to the marker layer. Deselecting the Marker layer in the legend hides the point graphic and the label. Precisely what I wanted.