MapxTreme测试:绘制图标和文字标注

代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MapInfo.Mapping;
using MapInfo.WebControls;
using ApplicationStateManager;
using MapInfo.Data;
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
ViewEntireMap();
// If the StateManager doesn't exist in the session put it else get it.
if (StateManager.GetStateManagerFromSession() == null)
StateManager.PutStateManagerInSession(new AppStateManager());
// Now Restore State
StateManager.GetStateManagerFromSession().ParamsDictionary[StateManager.ActiveMapAliasKey] = MapControl1.MapAlias;
StateManager.GetStateManagerFromSession().RestoreState();
}
// At the time of unloading the page, save the state
private void Page_UnLoad(object sender, System.EventArgs e)
{
StateManager.GetStateManagerFromSession().SaveState();
}
protected void ButtonViewEntire_Click(object sender, EventArgs e)
{
ViewEntireMap();
}

private void ViewEntireMap()
{
//try {
Map myMap = GetMapObj();
myMap.Clear();
MapInfo.Mapping.MapGeosetLoader gl = new MapGeosetLoader(Server.MapPath("App_Data\\海洋测试.gst"));
myMap.Load(gl);
double x = 111.0;
double y = 22.0;
myMap.Center = new MapInfo.Geometry.DPoint(x,y);
mpxDrawImg(myMap,x,y,"truckImg");
// mpxDrawText(myMap, "truckText", "A13431");
mpxDrawText(myMap, x, y, "truckText", "桂A99999");
//}
//catch (Exception e) {

//}
}

private MapInfo.Mapping.Map GetMapObj()
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
if (myMap == null)
{
myMap = MapInfo.Engine.Session.Current.MapFactory[0];
}
return myMap;
}
/// <summary>
/// 创建mapx表
/// </summary>
/// <param name="pMap">地图</param>
/// <param name="pTabName"></param>
/// <param name="pColumnName"></param>
/// <returns></returns>
private MapInfo.Data.Table mpxMkTable(MapInfo.Geometry.CoordSys pCoordSys, string pTabName,string pColumnName){
MapInfo.Data.Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
MapInfo.Data.Table tblTemp = Cat.GetTable(pTabName);
if (tblTemp != null) Cat.CloseTable(pTabName);
MapInfo.Data.TableInfoMemTable imt = new MapInfo.Data.TableInfoMemTable(pTabName);
imt.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(pCoordSys));
imt.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn());
imt.Columns.Add(MapInfo.Data.ColumnFactory.CreateStringColumn(pColumnName, 40));
return Cat.CreateTable(imt);
}
/// <summary>
/// 往表里增加一行
/// </summary>
/// <param name="pTable">表名</param>
/// <param name="pGeometry">坐标</param>
/// <param name="pStyle">样式</param>
/// <param name="pColumnName">列名</param>
/// <param name="pColumnValue">列值</param>
private void mpxTableAddRow(MapInfo.Data.Table pTable,MapInfo.Geometry.FeatureGeometry pGeometry,MapInfo.Styles.Style pStyle,string pColumnName,string pColumnValue) {
Feature ftr = new Feature(pTable.TableInfo.Columns);
ftr.Geometry = pGeometry;
ftr.Style = pStyle;
ftr[pColumnName] = pColumnValue;
pTable.InsertFeature(ftr);
}
/// <summary>
/// 创建文本样式
/// </summary>
/// <param name="pColor">文本颜色</param>
/// <param name="pSize">字体大小</param>
/// <param name="pFontName">字体名称</param>
/// <returns></returns>
private MapInfo.Styles.TextStyle mpxMkTextStyle(System.Drawing.Color pColor, double pSize, string pFontName) {
MapInfo.Styles.TextStyle ts = new MapInfo.Styles.TextStyle();
ts.Font.ForeColor = pColor;
ts.Font.Size = pSize;
ts.Font.Name = pFontName;
ts.ApplyStyle(ts);
return ts;
}
/// <summary>
/// 创建文本样式
/// </summary>
/// <param name="pSize">字体大小</param>
/// <param name="pFontName">字体名称</param>
/// <returns></returns>
private MapInfo.Styles.TextStyle mpxMkTextStyle(double pSize, string pFontName) {
MapInfo.Styles.TextStyle ts = new MapInfo.Styles.TextStyle();
ts.Font.ForeColor = System.Drawing.Color.Black;
ts.Font.Size = pSize;
ts.Font.FontWeight = MapInfo.Styles.FontWeight.Bold;
ts.Font.Name = pFontName;
return ts;
}
/// <summary>
/// 创建图层点坐标
/// </summary>
/// <param name="pSys"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private MapInfo.Geometry.FeatureGeometry mpxMkFeaturePoint(MapInfo.Geometry.CoordSys pSys,double x,double y) {
MapInfo.Geometry.Point pt = new MapInfo.Geometry.Point(pSys, x, y);
MapInfo.Geometry.FeatureGeometry geo = pt as MapInfo.Geometry.FeatureGeometry;
return geo;
}
/// <summary>
/// 绘制文本
/// </summary>
/// <param name="pSys">坐标系统,如:myMap.GetDisplayCoordSys()</param>
/// <param name="x">经度</param>
/// <param name="y">纬度</param>
/// <param name="pTabName">用于创建图层的表名</param>
/// <param name="pText">要绘制的文本</param>
private void mpxDrawText(MapInfo.Mapping.Map pMap,double x,double y,string pTabName,string pText) {
//MapInfo.Styles.TextStyle sty2 = new MapInfo.Styles.TextStyle();
//sty2.Font.ForeColor = System.Drawing.Color.Red;
//sty2.Font.TextEffect = MapInfo.Styles.TextEffect.Halo;
//MapInfo.Geometry.DRect rt = new MapInfo.Geometry.DRect(x, y, x + 30, y + 12);
//MapInfo.Geometry.LegacyText lt =
// new MapInfo.Geometry.LegacyText(pMap.GetDisplayCoordSys(), rt, pText);
//MapInfo.Data.Feature ft = new MapInfo.Data.Feature(lt, sty2);
//MapInfo.Data.Table dt = mpxMkTable(pMap.GetDisplayCoordSys(), pTabName, "name");
//dt.InsertFeature(ft);
//LabelSource source = new LabelSource(dt); //创建数据源
//source.DefaultLabelProperties.Caption = "name";
//LabelLayer lyr = new LabelLayer(); //创建图层
//lyr.Sources.Append(source);
//pMap.Layers.Add(lyr);
// DRect drect = new DRect(ftr.Geometry.Centroid.x + 2, ftr.Geometry.Centroid.y, ftr.Geometry.Centroid.x + 30, ftr.Geometry.Centroid.y + 12);
string sColumnName = "name"; //创建表
MapInfo.Data.Table dt = mpxMkTable(pMap.GetDisplayCoordSys(), pTabName, sColumnName);
MapInfo.Styles.TextStyle ts = mpxMkTextStyle(System.Drawing.Color.Blue, 8.0, "宋体");
MapInfo.Geometry.FeatureGeometry pt = mpxMkFeaturePoint(pMap.GetDisplayCoordSys(), x, y);
mpxTableAddRow(dt, pt, ts, sColumnName, pText); //往表里增加记录
LabelSource source = new LabelSource(dt); //创建数据源
source.DefaultLabelProperties.Caption = "name";
source.DefaultLabelProperties.Style.Font.ForeColor = System.Drawing.Color.Blue;
source.DefaultLabelProperties.Layout.Offset = 23;
LabelLayer lyr = new LabelLayer(); //创建图层
lyr.Sources.Append(source);
pMap.Layers.Add(lyr); //将图层加到地图
创建表
//MapInfo.Data.Table mpxTable = mkMapxTable(pMap, layerName);
创建文字样式
//MapInfo.Styles.TextStyle ts=new MapInfo.Styles.TextStyle();
//ts.Font.ForeColor = System.Drawing.Color.Black;
//ts.Font.Size = 9;
//ts.Font.Name = "宋体";
创建标源
//LabelSource ls = new LabelSource(mpxTable);
//ls.DefaultLabelProperties.Caption = pText;
//ls.DefaultLabelProperties.Style = ts;
创建标注层
//LabelLayer lly = new LabelLayer();//pText);
//lly.Sources.Append(ls);
//try {
// pMap.Layers.Add(lly);
//}
//catch (Exception e) {
//}
}
private void mpxDrawImg(MapInfo.Mapping.Map pMap, double x, double y, string pTabName) {

string sColName = "name";
MapInfo.Data.Table dt = mpxMkTable(pMap.GetDisplayCoordSys(), pTabName, sColName); //创建表
MapInfo.Geometry.FeatureGeometry pt = mpxMkFeaturePoint(pMap.GetDisplayCoordSys(), x, y); //创建点和样式
MapInfo.Styles.BitmapPointStyle cs =
new MapInfo.Styles.BitmapPointStyle("v13.BMP", MapInfo.Styles.BitmapStyles.None, System.Drawing.Color.White, 24);
mpxTableAddRow(dt, pt, cs, "name", "truck"); //往表增加记录
FeatureLayer lyr = new FeatureLayer(dt);
pMap.Layers.Add(lyr);
//FeatureLayer lyr = new FeatureLayer(tblTemp);
// new Feature(tblTemp.TableInfo.Columns);
//ftr.Geometry = pt;
//ftr.Style = cs;
//ftr[sColName] = "车号";
//tblTemp.InsertFeature(ftr);

//MapInfo.Data.Catalog Cat = MapInfo.Engine.Session.Current.Catalog;
创建临时层
//MapInfo.Data.TableInfoMemTable tblInfoTemp = new MapInfo.Data.TableInfoMemTable("Point");
//MapInfo.Data.Table tblTemp = Cat.GetTable("Point");
//if (tblTemp != null) Cat.CloseTable("Point");
//tblInfoTemp.Columns.Add(ColumnFactory.CreateFeatureGeometryColumn(pMap.GetDisplayCoordSys()));
//tblInfoTemp.Columns.Add(ColumnFactory.CreateStyleColumn());
//tblInfoTemp.Columns.Add(ColumnFactory.CreateStringColumn("Name", 40));
//tblInfoTemp.Columns.Add(ColumnFactory.CreateStringColumn("Dept", 15));
//tblInfoTemp.Columns.Add(ColumnFactory.CreateIntColumn("Level"));
//tblTemp = Cat.CreateTable(tblInfoTemp);
//FeatureLayer lyr = new FeatureLayer(tblTemp);
//pMap.Layers.Add(lyr);
//MapInfo.Geometry.FeatureGeometry pt = mpxMkFeaturePoint(pMap.GetDisplayCoordSys(), x, y);
MapInfo.Geometry.FeatureGeometry pt =
new MapInfo.Geometry.Point(
lyr.CoordSys, pMap.Center //pMap.Center//new MapInfo.Geometry.DPoint(1, 1) // //
) as MapInfo.Geometry.FeatureGeometry;
MapInfo.Styles.SimpleVectorPointStyle cs=
new MapInfo.Styles.SimpleVectorPointStyle(35, System.Drawing.Color.Red, 13);
//MapInfo.Styles.BitmapPointStyle cs =
// new MapInfo.Styles.BitmapPointStyle("v13.BMP", MapInfo.Styles.BitmapStyles.None, System.Drawing.Color.White, 24);
MapInfo.Styles.CompositeStyle cs = new MapInfo.Styles.CompositeStyle(new MapInfo.Styles.BitmapPointStyle(
//Feature ftr = new Feature(tblTemp.TableInfo.Columns);
//ftr.Geometry = pt;
//ftr.Style = cs;
//ftr["Name"] = "Kelly";
ftr["Dept"] = "Sales";
ftr["Level"] = 3;
//tblTemp.InsertFeature(ftr);
//----------------//
//MapInfo.Mapping.FeatureLayer oLayer;
//MapInfo.Data.Table otable;
//MapInfo.Data.Feature oftr;
//MapInfo.Styles.SimpleVectorPointStyle ostyle1;
//MapInfo.Styles.SimpleLineStyle ostyle2;
//MapInfo.Styles.AreaStyle ostyle3;
//MapInfo.Styles.TextStyle ostyle4;
//MapInfo.Styles.FontPointStyle ostyle = new MapInfo.Styles.FontPointStyle() ;
MapInfo.Styles.SimpleVectorPointStyle ostyle;
//MapInfo.Geometry.Point opoint;
//MapInfo.Data.Key k;
//oLayer = (MapInfo.Mapping.FeatureLayer)pMap.Layers[pLayerName];
//otable = oLayer.Table;
//opoint = new MapInfo.Geometry.Point(pMap.GetDisplayCoordSys(), pMap.Center.x, pMap.Center.y);
//ostyle1 = new MapInfo.Styles.CompositeStyle(ostyle3, ostyle2, ostyle4, ostyle1,);
//ostyle.Font.Name = "宋体";
//ostyle.PointSize = 30;
//ostyle.Color = System.Drawing.Color.Red;
//oftr = new MapInfo.Data.Feature(opoint, ostyle);
//k = otable.InsertFeature(oftr) ;
//MapInfo.Data.MIConnection cnn = new MapInfo.Data.MIConnection();
//cnn.Open();
//MapInfo.Data.MICommand cmm = new MapInfo.Data.MICommand();
//cmm.Connection = cnn;
//cmm.CommandText = "insert into ASIA(Id,Name,Obj,MI_Style) values(@id,@name,@obj,@style)";
//MapInfo.Styles.FontPointStyle sty = new MapInfo.Styles.FontPointStyle(66, new MapInfo.Styles.Font("Arial", 13), 0, System.Drawing.Color.Red, 18);
//cmm.Parameters.Add("@id", 431431);
//cmm.Parameters.Add("@name", "bsbs");
//cmm.Parameters.Add("@obj", new MapInfo.Geometry.Point(pMap.GetDisplayCoordSys(),0.00,0.00));
//cmm.Parameters.Add("@style", sty);
//cmm.ExecuteNonQuery();
//cnn.Close();
//----------------//
//----------------//
//MapInfo.Data.MIDbType.FeatureGeometry pt = new MapInfo.Geometry.Point(
//System.Collections.ArrayList arStyles = new System.Collections.ArrayList();
//MapInfo.Styles.BitmapPointStyle style2 = new MapInfo.Styles.BitmapPointStyle();
//style2.ThemeValue = "A";
//style2.BitmapName = "ecumenic.bmp";
//style2.Size = 10;
//style2.Style = MapInfo.Styles.All;
//style2.ShowWhiteBackground = false;
style2._color=Color.Blue; //这句可设置位图颜色,但是怎么让位图不设置颜色..默认为红色
//arStyles.Add(style2);
//FeatureLayer lyr = (FeatureLayer)pMap.Layers[pLayerName];
//lyr.Modifiers.Clear();
//MapInfo.Mapping.Thematics.IndividualValueTheme thm =
// new MapInfo.Mapping.Thematics.IndividualValueTheme(lyr, "i", "i");
//thm.ApplyStylePart = MapInfo.Mapping.Thematics.StylePart.All;
//for (int i = 0; i < thm.Bins.Count; i++) {
// thm.Bins[i].Style.SymbolStyle.PointSize = 30;
//}
//mapUtils.CreateIndividualValueTheme(MapControl1.Map, arStyles, "flBTSPoint", LegendControl1, "i", "i");
}
protected void ButtonZoomIn_Click(object sender, EventArgs e)
{
Map myMap = GetMapObj();
if (myMap == null) return;
myMap.Zoom = new MapInfo.Geometry.Distance(myMap.Zoom.Value / 2, myMap.Zoom.Unit);
}
protected void ButtonZoomOut_Click(object sender, EventArgs e)
{
Map myMap = GetMapObj();
if (myMap == null) return;
myMap.Zoom = new MapInfo.Geometry.Distance(myMap.Zoom.Value * 2, myMap.Zoom.Unit);


}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值