This project is read-only.

AddLabel Problem

Apr 27, 2012 at 2:16 PM

Hi all,

I use the following code to show labels on points. I draw many points on layer, but it only shows label on the first point, not the others. How can I solve this ?

Best Regards

void CreateLabelLayer()
        {
            PointScheme MyPointScheme = new PointScheme();
            MyPointScheme.ClearCategories();
            MyPointScheme.AppearsInLegend = false;

            FeatureSet MyFeatureSet = new FeatureSet(FeatureType.Point);
            MyFeatureSet.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;
            MyFeatureSet.DataTable.Columns.Add("label", typeof(string));

            //LabelPoints contains the list of points to be used for label coordinates
            for (int i = 0; i < LabelPoints.Count; i++)
            {
                IFeature MyFeature = MyFeatureSet.AddFeature(LabelPoints.ElementAt(i));
                MyFeature.DataRow["label"] = "Point #" + i;

                PointSymbolizer MyPointSymbolizer = new PointSymbolizer(Color.Transparent, DotSpatial.Symbology.PointShape.Rectangle, 4);
                MyPointSymbolizer.Smoothing = true;
                MyPointSymbolizer.IsVisible = true;

                PointCategory MyPointCategory = new PointCategory(MyPointSymbolizer);
                MyPointCategory.LegendItemVisible = false;
                MyPointCategory.FilterExpression = "[FID]=" + i;
                MyPointScheme.AddCategory(MyPointCategory);
            }
            IMapPointLayer MyMapPointLayer = new MapPointLayer(MyFeatureSet);
            MyMapPointLayer.Symbology = MyPointScheme;
            MyMapPointLayer.LegendText = "Label";
            MyMapPointLayer.ShowLabels = true;
            MapLabelLayer MyMapLabelLayer = new MapLabelLayer(MyMapPointLayer);
            //PB : cannot specify the font for each label
            MyMapLabelLayer.Symbology.Categories[0].Expression = "[label]";
            MyMapPointLayer.LabelLayer = MyMapLabelLayer;
            map1.Layers.Add(MyMapPointLayer);
        }

Apr 27, 2012 at 6:21 PM

You may find this tutorial useful: http://www.codeplex.com/download?ProjectName=dotspatial&DownloadId=226074

May 3, 2012 at 11:04 PM

I also come across this bit of code, but haven't tested it.

 

private void AddLabels()
{
    IFeatureLayer fl = geoMap1.AddFeatureLayer();
    string expression = "[" + fl.DataSet.DataTable.Columns[0].ColumnName + "]";
    LabelSymbolizer ls = new LabelSymbolizer();
    ls.Orientation = ContentAlignment.MiddleCenter;
    geoMap1.AddLabels(fl, expression, "", ls);
}
May 6, 2012 at 12:20 PM

Dear Mudnug,

Both methods you sent worked. I preferred the second one as it lets orientation. Thanks a lot for your help.

Baris