網上看到的基本上代碼都大同小異,經過本人小小修改或未修改的代碼如下:
一:先建立圖層
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
/// 添加點
/// <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 (地圖符号)
2:建立線函數代碼:addline
/// <summary>
/// 添加線[代碼和建立點的相差無幾]
/// <param name="startpoint">線段起點坐标</param>
/// <param name="endpoint">線段終點坐标</param>
/// <param name="shortcode">線的shortcode(線的型狀也有多種,比如單箭頭,雙箭頭等)</param>
/// <param name="color">線的顔色</param>
public static void addline(string layername, dpoint startpoint, dpoint endpoint, int shortcode, color color)
//建立線
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
/// 顯示标注
/// <param name="tablename">标注的表名</param>
/// <param name="columnname">标注的列名</param>
public static void showvalue(string tablename, string columnname)
//建立标注圖層并綁定資料(整個過程有點像datagrid控件指定資料源控件sqldatasource,而資料源控件又綁定了datatable)
labellayer labellayer = new labellayer();
mymap.layers.add(labellayer);
//指定要标注的資料表
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;
版權聲明:本文原創發表于部落格園,作者為路過秋天,原文連結:
http://www.cnblogs.com/cyq1162/archive/2009/11/02/1594505.html