MapXtreme实用技巧
MapXtreme实用技巧与源码10例
1 设置图层可选状态
/**//// <summary>
/// 改变层的可选择状态
/// </summary>
/// <param name="selectableStatus"></param>
/// <returns></returns>
public bool LayerSelectableStatusUpdate(string tableAlias,bool selectableStatus)
{
if(mapControl1.Map.Layers[tableAlias]==null)
return false;
MapInfo.Mapping.LayerHelper.SetSelectable(mapControl1.Map.Layers[tableAlias],selectableStatu
s);
return true;
}
2 设置层的可用状态
/**//// <summary>
/// 改变层的可用状态为status
/// </summary>
/// <param name="layerName"></param>
/// <param name="status"></param>
public void LayerEnableStatusUpdate(string layerName,bool status)
{
if(mapControl1.Map.Layers[layerName]!=null && mapControl1.Map.Layers[layerName].Enabled!
=status)
{
mapControl1.Map.Layers[layerName].Enabled=status;
}
}
3 层居中,看全图
/**//// <summary>
/// 使指定层全部呈现在地图的可见范围中
/// </summary>
/// <param name="tableAlias">层别名</param>
public void LayerCenter(string layerName)
{
MapInfo.Data.Table[] tables=new MapInfo.Data.Table[1];
tables[0] = MapInfo.Engine.Session.Current.Catalog.GetTable(layerName);
if(tables[0]==null)
return;
if(mapControl1.Map.Layers[layerName]==null)
return;
if(mapControl1.Map.Layers[layerName].Enabled == false)
mapControl1.Map.Layers[layerName].Enabled = true;
MapInfo.Mapping.IMapLayerFilter iMapLayerFilter =
MapInfo.Mapping.MapLayerFilterFactory.FilterByTable(tables);
MapInfo.Mapping.MapLayerEnumerator mapLayerEnumerator =
mapControl1.Map.Layers.GetMapLayerEnumerator(iMapLayerFilter);
mapControl1.Map.SetView(mapLayerEnumerator);
OnFeatureUnclick();
}
4 放大缩小地图
/**//// <summary>
/// 放大地图
/// </summary>
/// <param name="times">放大倍数,有效值1-10</param>
public void ZoomIn(uint times)
{
if(times<1 || times>10) return;
MapInfo.Geometry.Distance previousZoom=this.mapControl1.Map.Zoom;
mapControl1.Map.Zoom=new MapInfo.Geometry.Distance(previousZoom.Value/
(2*times),previousZoom.Unit);
}
/**//// <summary>
/// 缩小地图
/// </summary>
/// <param name="times">缩小倍数,有效值1-10</param>
public void ZoomOut(uint times)
{
if(times<1 || times>10) return;
MapInfo.Geometry.Distance previousZoom=this.mapControl1.Map.Zoom;
mapControl1.Map.Zoom=new MapInfo.Geometry.Distance(previousZoom.Value*
(2*times),previousZoom.Unit);
}
5 移动层的顺序
mapControl1.Map.Layers.Move(index1,index2);
6 图元/图层透明
/**//// <summary>
/// 设置层的透明与否
/// </summary>
/// <param name="layerName">层名</param>
/// <param name="opaqueType">不透明类型 ALL 全部不透明 BORDER 只有边界不透明(内部透明)
NONE 全部透明</param>
/// <param name="borderColor">如果是边界不透明,此处设置边界颜色</param>
public void LayerTransparent(string layerName,OpaqueType opaqueType,System.Drawing.Color
borderColor)
{
MapInfo.Styles.CompositeStyle compositeStyle = GetOpaqueStyle(opaqueType,borderColor);
//创建连接和命令来更新table中的数据
MapInfo.Data.MIConnection connection=new MapInfo.Data.MIConnection();
connection.Open();
MapInfo.Data.MICommand command=connection.CreateCommand();
command.CommandText = "update " + layerName + " set obj=obj,MI_Style=@style";
command.Parameters.Add("@style",compositeStyle);
command.Prepare();
command.ExecuteNonQuery();
//关闭连接
command.Cancel();
command.Dispose();
connection.Close();
connection.Dispose();
}
/**//// <summary>
/// 创建style
/// </summary>
/// <param name="opaqueType">不透明类型:ALL全部不透明(白色实心);BORDER边界不透明(填充部
分透明);NONE全透明</param>
/// <param name="borderColor">如果opaqueType=BORDER,此处设定边界颜色</param>
/// <returns>组合style</returns>
private MapInfo.Styles.CompositeStyle GetOpaqueStyle(OpaqueType
opaqueType,System.Drawing.Color borderColor)
{
MapInfo.Styles.SimpleInterior simpleInterior;
if(opaqueType==OpaqueType.ALL)
simpleInterior= new MapInfo.Styles.SimpleInterior(); //缺省构造函数是白色实心
else
simpleInterior= new MapInfo.Styles.SimpleInterior(1); //0是线透明,1是面透明
MapInfo.Styles.LineWidth lineWidth = new
MapInfo.Styles.LineWidth(1,MapInfo.Styles.LineWidthUnit.Point);
MapInfo.Styles.SimpleLineStyle simpleLineStyle;
if(opaqueType==OpaqueType.ALL)
simpleLineStyle = new MapInfo.Styles.SimpleLineStyle(lineWidth);
else if(opaqueType==OpaqueType.BORDER)
simpleLineStyle = new MapInfo.Styles.SimpleLineStyle(lineWidth,2,borderColor); //2表示填充
透明,即能够显示轮廓
else
simpleLineStyle = new MapInfo.Styles.SimpleLineStyle(lineWidth,0); //0表示全部透明,即连轮
廓都看不到
MapInfo.Styles.AreaStyle areaStyle = new
MapInfo.Styles.AreaStyle(simpleLineStyle,simpleInterior);
MapInfo.Styles.CompositeStyle compositeStyle = new
MapInfo.Styles.CompositeStyle(areaStyle,null,null,null);
return compositeStyle;
}
7 选择全部图元
MapInfo.Engine.Session.Current.Catalog.Search(
table,
MapInfo.Data.SearchInfoFactory.SearchAll(),
MapInfo.Engine.Session.Current.Selections.DefaultSelection,
MapInfo.Data.ResultSetCombineMode.Replace);
8 设置坐标系
缺省情况下,MapXtreme使用的CoordSys是经纬度投影(LongLat)和WGS84基准面。我想修改投影类型为
CoordSysType.TransverseMercator ,基准面为DatumID.Pulkovo1942
MapInfo.Geometry.CoordSysFactory
coordSysFactory=MapInfo.Engine.Session.Current.CoordSysFactory;
mapControl1.Map.SetDisplayCoordSys(coordSysFactory.CreateCoordSys("mapinfo:coordsys
8,1001,7,117,0,1,20500000,0"));
coordSysFactory.CreateCoordSys("mapinfo:coordsys 8,1001,7,117,0,1,20500000,0") 默认的原点
是(B=0,L=117),如果要把原点设在(23,117)应该怎么写这个字符串呢?
coordSysFactory.CreateCoordSys("mapinfo:coordsys 8,1001,7,114,23,1,20500000,25000000")
9 保存新画的层为tab文件
下面的源码是新建一个永久表,然后在表中添加feature,然后保存为硬盘上的tab文件。
private MapInfo.Data.Table CreateNewMapDataTable(string tableName)
{
//以下代码是建立永久表
MapInfo.Data.TableInfoNative
tableInfoNative=newMapInfo.Data.TableInfoNative(tableName);
tableInfoNative.TablePath=@"D:\DATA\"+tableName+".TAB";
tableInfoNative.Columns.Add(MapInfo.Data.ColumnFactory.CreateIntColumn("ID"));
tableInfoNative.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn());
MapInfo.Geometry.CoordSys coordSys =mapControl1.Map.GetDisplayCoordSys();
tableInfoNative.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(coordSys)
);
MapInfo.Data.Table table
=MapInfo.Engine.Session.Current.Catalog.CreateTable(tableInfoNative);
//以下代码是建立临时表
// MapInfo.Data.TableInfo tableInfo
=MapInfo.Data.TableInfoFactory.CreateTemp(tableName);
// tableInfo.Columns.Add(MapInfo.Data.ColumnFactory.CreateIntColumn("ID"));
// MapInfo.Data.Table table
=MapInfo.Engine.Session.Current.Catalog.CreateTable(tableInfo);
MapInfo.Mapping.FeatureLayer featureLayer =
newMapInfo.Mapping.FeatureLayer(table);
this.mapControl1.Map.Layers.Add(featureLayer);
return table;
}
private void AddFeaturesAndSave()
{
MapInfo.Styles.SimpleLineStyle simpleLineStyle =
newMapInfo.Styles.SimpleLineStyle(new
MapInfo.Styles.LineWidth(1,MapInfo.Styles.LineWidthUnit.Point));
MapInfo.Styles.CompositeStyle compositeStyle =
newMapInfo.Styles.CompositeStyle(null,simpleLineStyle, null, null);
MapInfo.Geometry.CoordSys coordSys =mapControl1.Map.GetDisplayCoordSys();
MapInfo.Data.Table table = CreateNewMapDataTable("NewTable");
MapInfo.Data.TableInfo tableInfo = table.TableInfo;
while(……)
{
MapInfo.Data.Feature feature = newMapInfo.Data.Feature(tableInfo.Columns);
feature.Geometry = ……
feature.Style = ……
feature["ID"] = ……
table.InsertFeature(feature);
}
tableInfo.WriteTabFile(); //保存为.tab文件
mapControl1.Refresh();
}
10 计算缩放比例
/**//// <summary>
/// 重画控件时计算缩放比例
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void mapControl1_Paint(object sender,PaintEventArgs e)
{
MapInfo.Geometry.Distance zoomDistance=this.mapControl1.Map.Zoom;
double zoom=Convert.ToDouble(Convert.ToInt32(zoomDistance.Value*16.09))/10;
this.statusBar1.Text="缩放比例:"+zoom.ToString()+" 千米";
}