转自成艳校友空间
1.asp.net中如何用MapControl 动态加载地图
MapControl1.Map.Clear();
MapGeosetLoader gl=new MapGeosetLoader(@"F:/test.gst");
MapControl1.Map.Load(gl);
其实根据不同的格式加载地图的办法也有多种,MapWorkSpaceLoader
2.创建MSW文件
MapInfo.Persistence.WorkSpacePersistence wsp = new MapInfo.Persistence.WorkSpacePersistence();
wsp.Save ("c:/temp/newwork.mws"); //保存文件
3.创建InfoTool的事件
InfoTool的事件与button不一样,而是 InfoRetrieved。
创建后将是这样:
private void InfoToolControl1_InfoRetrieved(object sender, MapInfo.Web.UI.WebControls.InfoToolEventArgs e)
{
string Info = "";
foreach(MapInfo.Data.ITableFeatureCollection fc in e.Features)
{
foreach(MapInfo.Data.Feature f in fc)
{
Info= string.Format(Info + f[1].ToString() + " ");
}
fc.Table.Close();
}
}
4.如何通过web.config来加载地图
在web.config中,在下添加的Key
In the HTML view of the form under the mapinfowebuiwebcontrols:MapControl tag, set the following properties:
在HTML视图中,更改 mapinfowebuiwebcontrols:MapControl的属性MapAlias必须以.MSW结尾
UseDesigntimeWorkspace="fale" ,这一句要删除WorkspaceFileName="WebForm1MapControl1.mws"
5.在添加了新层后如何更新地图让InfoTool可以获取他的信息
MapInfo.Data.Table tab = MapInfo.Engine.Session.Current.Catalog.OpenTable(@"C:/Program Files/MapInfo/MapXtreme2004/Maps/World/world.tab");
MapInfo.Mapping.FeatureLayer fl = new MapInfo.Mapping.FeatureLayer(tab);
this.MapControl1.Map.Layers.Add(fl);
this.MapControl1.SelectableLayers.Add(fl.Name);
6.MapXtreme2004应用问答- - by jerry429
问:如何在桌面程序中更改地图的坐标系?
问:在C#应用中如何读取存在ORACLE(或SQL Server)中的MapInfo表?
问:在C#桌面与Web应用中读取硬盘上MapInfo表一法
问:如何在MapXtreme2004桌面应用程序中的任务栏上显示出鼠标的坐标?
问:如何使用代码修改桌面应用程序的坐标系统?
问:如何在桌面程序中更改地图的坐标系?
答:代码如下:
using MapInfo.Geometry;
//要使用到MapInfo.Geometry命名空间
Map map = mapControl1.Map;
MapInfo.Geometry.CoordSys coordSys = Session.Current.CoordSysFactory.CreateLongLat(DatumID.NAD83);
//DatumID为枚举类型,其中列出了经纬度坐标系统的大量枚举类型,参阅帮助可获取更多信息。
map.SetDisplayCoordSys(coordSys);
问:在C#应用中如何读取存在ORACLE(或SQL Server)中的MapInfo表?
答:读取ORACLE中表的方法如下代码:
using MapInfo.Data; //这里要添加对MapInfo数据的引用
MIConnection Connection=new MIConnection();
Connection.Open();
MapInfo.Data.Table [] tables=new MapInfo.Data.Table[4];
TableInfoServer tis1=new TableInfoServer("WORLD","SVR=MYORACLE;UID=system;PWD=manager","select * from world",MapInfo.Data.ServerToolkit.Oci);
tables[0]=Connection.Catalog.OpenTable(tis1);
TableInfoServer tis2=new TableInfoServer("WORLDCAP","SVR=MYORACLE;UID=system;PWD=manager","select * from worldcap",MapInfo.Data.ServerToolkit.Oci);
tables[1]=Connection.Catalog.OpenTable(tis2);
TableInfoServer tis3=new TableInfoServer("wldcty25","SVR=MYORACLE;UID=system;PWD=manager","select * from wldcty25",MapInfo.Data.ServerToolkit.Oci);
tables[2]=Connection.Catalog.OpenTable(tis3);
TableInfoServer tis4=new TableInfoServer("OCEAN","SVR=MYORACLE;UID=system;PWD=manager","select * from OCEAN",MapInfo.Data.ServerToolkit.Oci);
tables[3]=Connection.Catalog.OpenTable(tis4);
MapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
Connection.Close();
而读取存放在SQL Server2000中的表时,应当使用如下修改过的代码:
MIConnection Connection=new MIConnection();
Connection.Open();
MapInfo.Data.Table [] tables=new MapInfo.Data.Table[2];
TableInfoServer tis1=new TableInfoServer("CH_SHENGHUI","DRIVER={SQL Server};SERVER=YC31;DATABASE=MYWEBGIS;Trusted_Connection=Yes","select * from CH_SHENGHUI",MapInfo.Data.ServerToolkit.Odbc);//注意这里使用的是Odbc,且区分大小写。
tables[0]=Connection.Catalog.OpenTable(tis1);
TableInfoServer tis2=new TableInfoServer("CH_SHENGJIE_P","DRIVER={SQL Server};SERVER=YC31;DATABASE=MYWEBGIS;Trusted_Connection=Yes","select * from CH_SHENGJIE_P",MapInfo.Data.ServerToolkit.Odbc);//注意这里使用的是Odbc,且区分大小写。
tables[1]=Connection.Catalog.OpenTable(tis2);
mapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
Connection.Close();
问:在C#桌面与Web应用中读取硬盘上MapInfo表一法
答:二者皆可使用如下代码:
using MapInfo.Data;
MIConnection Connection=new MIConnection();
Connection.Open();
tables[0]=Connection.Catalog.OpenTable(@"G:/Ch_shenghui.TAB");
tables[1]=Connection.Catalog.OpenTable(@"G:/Shengjie_p.TAB");
mapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
问:如何在MapXtreme2004桌面应用程序中的任务栏上显示出鼠标的坐标?
答:
1、添加mapControl1的MouseMove事件;
2、该事件及其中代码如下:
public void MapControl1_MouseMove(object sender, MouseEventArgs e)
{
System.Drawing.PointF DisplayPoint = new PointF(e.X,e.Y);
MapInfo.Geometry.DPoint MapPoint = new MapInfo.Geometry.DPoint();
MapInfo.Geometry.DisplayTransform converter = this.mapControl1.Map.DisplayTransform;
converter.FromDisplay(DisplayPoint, out MapPoint);
this.statusBar1.Text = "Cursor Location: " + MapPoint.x.ToString() + ", " + MapPoint.y.ToString();
}
问:如何使用代码修改桌面应用程序的坐标系统
答:代码如下:
Map map = mapControl1.Map;
MapInfo.Geometry.CoordSys coordSys = Session.Current.CoordSysFactory.CreateLongLat(DatumID.WGS84);
map.SetDisplayCoordSys(coordSys);
7.在一个程序中要找到固定点附近最近的图元,于是想用SearchNearst。
但是总是出现异常,说"不能对NonEarth坐标系应用Spherical操作"
所以想问一下,MXT04中的SearchNearst方法是不是不支持NonEarth坐标系?
//这个问题在MapXtreme2004的6.0和6.1版本都会有。以后版本会改正。
//暂时解决方案:在经纬度投影坐标系中操作。
try
{
MapInfo.Mapping.Map _map=mapControl1.Map;
MapInfo.Data.Catalog _catalog=Session.Current.Catalog;
//打开非地球坐标系的图层,并设置地图坐标系
mapControl1.Map.Load(new MapTableLoader(@"E:/MapInfo/培训材料/技术培训教材与PPT/MapXtreme2004/示例/OpenTable/MAP_AREA.TAB"));
Cursor.Current = Cursors.WaitCursor;
MapInfo.Geometry.CoordSysFactory cf=new MapInfo.Geometry.CoordSysFactory();
MapInfo.Geometry.CoordSys Nonearth=cf.CreateFromMapBasicString("CoordSys NonEarth Units /"m/" Bounds (-2000000, -2000000) (2000000, 2000000)");
_map.SetDisplayCoordSys (Nonearth);
//确定查找点-为非地球坐标的中心点
MapInfo.Geometry.DPoint pt=new MapInfo.Geometry.DPoint(_map.Center.x,_map.Center.y);
//将地图坐标系转换为经纬度投影
MapInfo.Geometry.CoordSys LonLat=cf.CreateFromMapBasicString("CoordSys Earth Projection 1, 0");
_map.SetDisplayCoordSys (LonLat);
//将非地球坐标下的查找点转换我经纬度投影下的点
MapInfo.Geometry.CoordinateTransform Transformer=cf.CreateCoordinateTransform(LonLat,Nonearth);
MapInfo.Geometry.DPoint dpt=Transformer.CoordSys2ToCoordSys1(pt);
//设置查找半径
Distance d = MapInfo.Mapping.SearchInfoFactory.ScreenToMapDistance(_map, 3);
SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchNearest(dpt, LonLat, d);
IResultSetFeatureCollection fc = _catalog.Search("MAP_AREA", si);
Session.Current.Selections.DefaultSelection.Add(fc);
}
finally
{
Cursor.Current = Cursors.Default;
}
nonearth问题在6.2中已经完全解决.
8.有两个MapControls,怎样使其中一个保存为.MWS
MapInfo.Persistence.WorkSpacePersistence wsp = new MapInfo.Persistence.WorkSpacePersistence();
System.IO.FileStream file = new System.IO.FileStream("C:/temp/Test.mws", System.IO.FileMode.CreateNew);
wsp.Save(this.mapControl2.Map.Clone() as MapInfo.Mapping.Map,file);
file.Close();
9.查看特定几个图层
假定让lyr1和lyr2两个图层充满整个地图窗口:
Table[] ts= new Table[2];
ts[0] = lyr1.Table;
ts[1] = lyr2.Table;
IMapLayerFilter iml = MapLayerFilterFactory.FilterByTable(ts);
MapLayerEnumerator mle = mapControl1.Map.Layers.GetMapLayerEnumerator(iml);
mapControl1.Map.SetView(mle);
10.//创建临时层
TableInfoMemTable tblInfoTemp = new TableInfoMemTable("Animation");
MapInfo.Data.Table tblTemp = cat.GetTable("Animation");
if (tblTemp != null) //Table exists close it
{
cat.CloseTable("Animation");
}
tblInfoTemp.Columns.Add(ColumnFactory.CreateFeatureGeometryColumn(MapControl1.Map.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")); //TableInfoMemTable是TableInfo类继承来的
tblTemp = cat.CreateTable(tblInfoTemp);
FeatureLayer lyr = new FeatureLayer(tblTemp);
MapControl1.Map.Layers.Add(lyr);
1.从ORACLE表中加载图层的问题。。。
MIConnection Connection=new MIConnection();
Connection.Open();
TableInfoServer tis1=new TableInfoServer("WORLD",
"SRVR=ora8i;UID=wzz;PWD=11111111",
"select * from world",MapInfo.Data.ServerToolkit.Oci);
MapInfo.Data.Table [] tables=new MapInfo.Data.Table[2];
tables[0]=Connection.Catalog.OpenTable(tis1);
TableInfoServer tis2=new TableInfoServer("WORLDCAP",
"SRVR=ora8i;UID=wzz;PWD=11111111",
"select * from worldcap",MapInfo.Data.ServerToolkit.Oci);
tables[1]=Connection.Catalog.OpenTable(tis2);
MapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
2.存在SqlSever中XY坐标点的绑定代码
//XY绑定:含有x,y坐标点的图层放置在Sql Server数据库中,按xy坐标生成点图层
//1.要在存在该表的数据库中建立MAPINFO_MAPCATALOG表,否则会报错:
//"An unhandled exception of type 'MapInfo.Data.TableException' occurred in mapinfo.coreengine.dll.
//Additional information: 不能打开表。 ODBC 错误: ODBC RC=-1, ODBC SQLState=S0002, DBMS RC=208, DBMS Msg=[Microsoft][ODBC SQL Server Driver][SQL Server]对象名 'MAPINFO.MAPINFO_MAPCATALOG' 无效。
//2.要含xy坐标点的表建立主键,否则会报错:
//"Additional information: 不能打开表。因为不能识别唯一键,不能访问远程数据。"
//建立XY空间方案
MapInfo.Data.SpatialSchemaXY xy = new MapInfo.Data.SpatialSchemaXY();
xy.XColumn = "X";
xy.YColumn = "Y";
xy.NullPoint = "0.0, 0.0";
xy.StyleType = MapInfo.Data.StyleType.None;
xy.DefaultStyle = new MapInfo.Styles.CompositeStyle();
xy.CoordSys = MapInfo.Engine.Session.Current.CoordSysFactory.CreateLongLat(MapInfo.Geometry.DatumID.WGS84);
//建立TableInfo
MapInfo.Data.TableInfoServer ti = new MapInfo.Data.TableInfoServer("Customers","Driver={SQL Server};Server=LIXIAOHUI;Database=test;Uid=sa;Pwd=sa;","select * from Us_Cust",MapInfo.Data.ServerToolkit.Odbc);
ti.SpatialSchema = xy;
MapInfo.Data.Table miCustomers = MapInfo.Engine.Session.Current.Catalog.OpenTable (ti);
MapInfo.Mapping.FeatureLayer featureLayer = new MapInfo.Mapping.FeatureLayer(miCustomers);
mapControl1.Map.Layers.Add(featureLayer);
mapControl1.Map.SetView(featureLayer);
3.如何查看整个地图
Map map = mapControl1.Map;
IMapLayerFilter lyrFilter = MapLayerFilterFactory.FilterByType(typeof(FeatureLayer));
MapLayerEnumerator lyrEnum= map.Layers.GetMapLayerEnumerator(lyrFilter);
map.SetView(lyrEnum);
或者:
Map map = mapControl1.Map;
IMapLayerFilter lyrFilter = new FilterByLayerType(LayerType.Normal);
MapLayerEnumerator lyrEnum= map.Layers.GetMapLayerEnumerator(lyrFilter);
map.SetView(lyrEnum);