Label usage

May 21, 2011 at 12:28 PM

How can I use labels? I tried the following without success:

var map = new Map();
map.Layers.Add(new TileLayer(osmTileSource) { LayerName = "OSM" });
var countries = CreateCountryLayer(); // same as demo
map.Layers.Add(countries);

var countryLabels = new LabelLayer("Country labels");
//countries.DataSource.Open();
countryLabels.DataSource = countries.DataSource;
countryLabels.MaxVisible = 0.18;
countryLabels.MinVisible = 0.054;
//countryLabels.MaxVisible = double.MaxValue;
countryLabels.Enabled = true;
countryLabels.SRID = 3785;
countryLabels.LabelColumn = "NAME";

var labelStyle = new LabelStyle();
labelStyle.ForeColor = Color.Black;
labelStyle.Font = new Font { FontFamily = "GenericSerif", Size = 12 };
labelStyle.BackColor = new Brush { Color = new Color { A = 128, R = 255, G = 0, B = 0 } };
countryLabels.Styles.Add(labelStyle);
map.Layers.Add(countryLabels);
mapControl.Map = map;
mapControl.ZoomToExtent();

If I change the MaxVisible into the MaxValue I get the following error: "An attempt was made to read from a closed datasource".

at SharpMap.Data.Providers.ShapeFile.GetObjectIDsInView(BoundingBox bbox)

in trunk\SharpMap.Providers\Shapefile\ShapeFile.cs:line 476

Thanks for your help,

Frank

Coordinator
May 29, 2011 at 8:39 PM

label rendering for gdi is not implemented yet. I should throw a NotImplementedException. There is some labeling we use in Silverlight which should work for WPF as well, but I have no sample to show you. 

Paul

Jan 10, 2013 at 8:23 AM

Hi,

Is the label rendering for gdi now implemented? I tried the piece of code written in the post above, it didn't work, any hints?

Regards,

Poonam