GenerateLabelsManually.cs

This example demonstrates how to Adds labels to the layer without using automated procedures like Labels.Generate. Here is a screenshot with the results of the code execution.

using System;
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Adds labels to the layer without using automated procedures like Labels.Generate.
// </summary>
public void GenerateLabelsManually(AxMap axMap1, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
string filename = dataPath + "buildings.shp";
if (!File.Exists(filename))
{
MessageBox.Show("Failed to open file: " + filename);
return;
}
var sf = new Shapefile();
if (!sf.Open(filename, null))
{
MessageBox.Show("Failed to open shapefile: " + sf.ErrorMsg[sf.LastErrorCode]);
}
else
{
int layerHandle = axMap1.AddLayer(sf, true);
sf = axMap1.get_Shapefile(layerHandle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
int fieldIndex = 0;
for (int i = 0; i < sf.NumShapes; i++)
{
Shape shp = sf.Shape[i];
string text = sf.CellValue[fieldIndex, i].ToString();
Point pnt = shp.Centroid;
sf.Labels.AddLabel(text, pnt.x, pnt.y, 0.0, -1);
// the old method should be used like this
//axMap1.AddLabel(layerHandle, text, 0, pnt.x, pnt.y, tkHJustification.hjCenter);
}
sf.Labels.Synchronized = true;
}
}
}
}
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
A point object represents a point with x, y, Z, and M values. Shapes created by adding point objects ...
Definition: PointClass.cs:38
double y
Gets or sets the y value of the point.
Definition: PointClass.cs:122
double x
Gets or sets the x value of the point.
Definition: PointClass.cs:113
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
Point Centroid
Calculates a centroid (center of mass) of the shape.
Definition: Shape.cs:92
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
Shapefile get_Shapefile(int layerHandle)
Gets shapefile object associated with the layer.
Definition: AxMap.cs:1546