ThinkGeo.com    |     Documentation    |     Premium Support

Simple example to add a label

Could someone please provide an example to add a label to the map. I’ve found some examples that are very verbose with a number of unnecessary string literal inter-dependencies. Surely it must be possible to add a label with a style to a feature and then to a overlay. This example works but surely it can be simplified.

    private void AddLabel()
    {
        labelOverlay = new EditInteractiveOverlay();
        labelOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.IsActive = false;
        labelOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.IsActive = false;
        labelOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.IsActive = false;
        labelOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.IsActive = false;


        TextStyle unitTextStyle = new TextStyle("UnitLabel", new GeoFont("Arial", 18), new GeoSolidBrush(GeoColor.SimpleColors.Red));

        ValueStyle labelValueStyle = new ValueStyle();
        labelValueStyle.ColumnName = "UnitLabel";
        labelValueStyle.ValueItems.Add(new ValueItem("WTF", unitTextStyle));

        labelOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(labelValueStyle);
        labelOverlay.EditShapesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

        labelOverlay.EditShapesLayer.Open();
        labelOverlay.EditShapesLayer.Columns.Add(new FeatureSourceColumn("UnitLabel"));
        labelOverlay.EditShapesLayer.Close();

        PointShape pointShape = new PointShape(424047, 568503);

        Feature feature = new Feature(pointShape);
        feature.ColumnValues["UnitLabel"] = "WTF";
        labelOverlay.EditShapesLayer.InternalFeatures.Add("testFeature", feature);


        wpfMap.InteractiveOverlays.Add("Labels", labelOverlay);

        wpfMap.Refresh();
    }

Hi Clive,

You can download our HowDoISamples project from product center or our wiki, which contains many samples tell user how to use our APIs.

And if you only want to shows the label, that’s very simple, you can only set the default text style like this:

    private void AddLabel()
{
    ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"..\..\SampleData\Data\USStates.shp");
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;  // Set style for shape
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Country1("STATE_NAME");           // Set style for label  
        worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

LayerOverlay staticOverlay = new LayerOverlay();
staticOverlay.Layers.Add(“WorldLayer”, worldLayer); // Add layer to overlay
wpfMap1.Overlays.Add(staticOverlay); // Add overlay to map
wpfMap1.Refresh();
}

Regards,

Don