网上看到的基本上代码都大同小异,经过本人小小修改或未修改的代码如下:
一:先创建图层
1:创建图层函数代码:CreateLayer
/// <summary>
/// 创建临时图层
/// by 路过秋天
/// <param name="tableName">表名</param>
/// <param name="layerName">图层名</param>
/// </summary>
public static void CreateLayer(string tableName, string layerName)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[0];//取得当前地图
//建立内存表信息(TableInfo中的一种,所以当然还有其它很多种表类型)
MapInfo.Data.TableInfoMemTable tblInfo = new MapInfo.Data.TableInfoMemTable(tableName);
//向表信息中添加可绘图列(必备的列)
tblInfo.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(myMap.GetDisplayCoordSys()));
tblInfo.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn());
//向表信息中添加其它数据列(可选的列)
tblInfo.Columns.Add(MapInfo.Data.ColumnFactory.CreateIntColumn("index"));//创建整形的列,当然还有其它日期型的,doule型的等等
tblInfo.Columns.Add(MapInfo.Data.ColumnFactory.CreateStringColumn("value", 20));//创建字符串型的列,并指定长度
//确保当前目录下不存在同名表
MapInfo.Data.Table table = MapInfo.Engine.Session.Current.Catalog.GetTable(tableName);
if (table != null)
{
MapInfo.Engine.Session.Current.Catalog.CloseTable(tableName);
}
//根据表信息创建临时表
table = MapInfo.Engine.Session.Current.Catalog.CreateTable(tblInfo);
//创建图层(并关联表)
FeatureLayer tempLayer = new FeatureLayer(table, layerName, layerName);
myMap.Layers.Add(tempLayer);
}
二:在图层的基础上,创建点,线,或其它图型
1:创建点函数代码:AddPoint
/// <summary>
/// 添加点
/// by 路过秋天
/// </summary>
/// <param name="layerName">图层名称</param>
/// <param name="dPoint">点的坐标</param>
/// <param name="shortCode">点的代码,不同的数字有不同的形状(圆型,三角型,正方型等)</param>
/// <param name="color">点的颜色</param>
public static void AddPoint(string layerName, DPoint dPoint, short shortCode, Color color)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[0];
//获取图层和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[layerName];
MapInfo.Data.Table table = workLayer.Table;
//创建点
FeatureGeometry point = new MapInfo.Geometry.Point(workLayer.CoordSys, dPoint);
//以下两行是图形的样式
MapInfo.Styles.SimpleVectorPointStyle spsPoint = new MapInfo.Styles.SimpleVectorPointStyle(shortCode, color, 20);
MapInfo.Styles.CompositeStyle pointStyle = new MapInfo.Styles.CompositeStyle(spsPoint);
//接下来创建一行数据
MapInfo.Data.Feature pointRow = new MapInfo.Data.Feature(table.TableInfo.Columns);
pointRow.Geometry = point;//必备列[图形]
pointRow.Style = pointStyle;//必备列[图形样式]
pointRow["index"] = new Random().Next(999);
pointRow["value"] = "this is a point";
//将一行数据放入表中
table.InsertFeature(pointRow);
}
说明:
关于shortCode:
参考C:\Program Files\MapInfo\MapXtreme\6.7.1\Documentation\PDF\MapXtreme2005_DevGuide.pdf
下的Appendix G:Style Lookups(附录G,样式查找)下的Vector Symbols(矢量符号)->Map Symbols (地图符号)
参考C:\Program Files\MapInfo\MapXtreme\6.7.1\Documentation\PDF\MapXtreme2005_DevGuide.pdf
下的Appendix G:Style Lookups(附录G,样式查找)下的Vector Symbols(矢量符号)->Map Symbols (地图符号)
2:创建线函数代码:AddLine
/// <summary>
/// 添加线[代码和创建点的相差无几]
/// by 路过秋天
/// <param name="layerName">图层名</param>
/// <param name="startPoint">线段起点坐标</param>
/// <param name="endPoint">线段终点坐标</param>
/// <param name="shortCode">线的shortCode(线的型状也有多种,比如单箭头,双箭头等)</param>
/// <param name="color">线的颜色</param>
/// </summary>
public static void AddLine(string layerName, DPoint startPoint, DPoint endPoint, int shortCode, Color color)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[0];
//获取图层和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[layerName];
MapInfo.Data.Table table = workLayer.Table;
//创建线
FeatureGeometry line = MultiCurve.CreateLine(workLayer.CoordSys, startPoint, endPoint);
//以下两行是图形的样式
MapInfo.Styles.SimpleLineStyle slsLine = new MapInfo.Styles.SimpleLineStyle(new LineWidth(3, LineWidthUnit.Pixel), shortCode, color);
MapInfo.Styles.CompositeStyle lineStyle = new MapInfo.Styles.CompositeStyle(slsLine);
//接下来创建一行数据
MapInfo.Data.Feature ptLine = new MapInfo.Data.Feature(table.TableInfo.Columns);
ptLine.Geometry = line;
ptLine.Style = lineStyle;
ptLine["index"] = new Random().Next(999); ;
ptLine["value"] = "this is a line";
//将线图元加入图层
table.InsertFeature(ptLine);
}
三:显示标注文本
1:显示标注文本函数代码:ShowValue
/// <summary>
/// 显示标注
/// by 路过秋天
/// <param name="tableName">标注的表名</param>
/// <param name="columnName">标注的列名</param>
/// </summary>
public static void ShowValue(string tableName, string columnName)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[0];
//新建标注图层并绑定数据(整个过程有点像DataGrid控件指定数据源控件SqlDataSource,而数据源控件又绑定了DataTable)
LabelLayer labelLayer = new LabelLayer();
myMap.Layers.Add(labelLayer);
//指定要标注的数据表
MapInfo.Data.Table table = MapInfo.Engine.Session.Current.Catalog.GetTable(tableName);
LabelSource source = new LabelSource(table);//绑定Table
labelLayer.Sources.Append(source);//加载指定数据
//指定哪个字段作为显示标注(在非必备的自定义列里挑一个,比如我们就挑"value"列)
source.DefaultLabelProperties.Caption = columnName;
//标注样式等属性,注意这段注释的代码,是指在一定的缩放比例范围内才显示文本,要是不注释掉,可能折腾半天也看不到为啥显示不出来文本
//source.DefaultLabelProperties.Visibility.Enabled = true;
//source.DefaultLabelProperties.Visibility.VisibleRangeEnabled = true;
//source.DefaultLabelProperties.Visibility.VisibleRange = new VisibleRange(0.01, 10, MapInfo.Geometry.DistanceUnit.Mile);
source.DefaultLabelProperties.Visibility.AllowDuplicates = true;
source.DefaultLabelProperties.Visibility.AllowOverlap = true;
source.DefaultLabelProperties.Visibility.AllowOutOfView = true;
source.Maximum = 50;
source.DefaultLabelProperties.Layout.UseRelativeOrientation = true;
source.DefaultLabelProperties.Layout.RelativeOrientation = MapInfo.Text.RelativeOrientation.FollowPath;
source.DefaultLabelProperties.Layout.Angle = 33.0;
source.DefaultLabelProperties.Layout.Offset = 7;
source.DefaultLabelProperties.Layout.Alignment = MapInfo.Text.Alignment.CenterCenter;
MapInfo.Styles.Font font = new MapInfo.Styles.Font("黑体", 12);
font.ForeColor = System.Drawing.Color.Red;
source.DefaultLabelProperties.Style.Font = font;
}
先上这四个最基本的函数,如果把这几个函数放一个类中,别忘了加名称空间
using MapInfo.Geometry;
using MapInfo.Mapping;
using MapInfo.Styles;
using MapInfo.Data;
using MapInfo.Text;
using System.Drawing;
using MapInfo.Mapping;
using MapInfo.Styles;
using MapInfo.Data;
using MapInfo.Text;
using System.Drawing;