在MapXtreme 2005中,查找图元提供了非常多的方法,也非常容易实现,这里总结了三种方法。
(1)Search方法是非常强大的,可以实现几乎所有的查找,这也是最常用的查找方式。mapxtrememapxtreme示例代码如下:
/**////<summary>
///通过Search方法查找图元
///DesignbyGlacier
///2008年8月6日
///<paramname="tableName">查找的表名</param>
///<paramname="columnName">查找的列名</param>
///<paramname="strKey">查找的关键字</param>
///</summary>
publicstaticvoidSearchWithSearch(stringtableName,stringcolumnName,stringstrKey)
{
MapInfo.Mapping.Mapmap=MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
SearchInfosi=MapInfo.Data.SearchInfoFactory.SearchWhere(columnName+"like'%"+strKey+"%'");
IResultSetFeatureCollectionifs=MapInfo.Engine.Session.Current.Catalog.Search(tableName,si);
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
if(ifs.Count<=0)
{
lbSearch.Text="Cannotfindthepoint";
}
else
{
//高亮显示
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
lbSearch.Text="";
if(ifs.Count==1)
{
map.Center=newDPoint(ifs[0].Geometry.Centroid.x,ifs[0].Geometry.Centroid.y);
MapInfo.Geometry.Distanced=newMapInfo.Geometry.Distance(0.5,map.Zoom.Unit);
}
else
{
map.SetView(ifs.Envelope);
}
//设置高亮显示的样式
//((SimpleInterior)MapInfo.Engine.Session.Current.Selections.DefaultSelection.Style.AreaStyle.Interior).BackColor=System.Drawing.Color.Red;
//((SimpleInterior)MapInfo.Engine.Session.Current.Selections.DefaultSelection.Style.AreaStyle.Interior).ForeColor=System.Drawing.Color.Green;

//输出查询信息
ListBox1.Items.Clear();
foreach(Featurefeatureinifs)
{
ListBox1.Items.Add(feature["name"].ToString());
}
}
}
(2)通过构造Find对象,进行查找。示例代码如下:
/**////<summary>
///通过Find查找图元
///DesignbyGlacier
///2008年8月6日
///<paramname="layerName">查找的图层名</param>
///<paramname="columnName">查找的列名</param>
///<paramname="strKey">查找的关键字</param>
///</summary>
publicstaticvoidSearchWithFind(stringlayerName,stringcolumnName,stringstrKey)
{
Findfind=null;
MapInfo.Mapping.Mapmap=MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
//Dothefind
MapInfo.Mapping.FeatureLayerfindLayer=(MapInfo.Mapping.FeatureLayer)map.Layers[PointLayerName];
find=newFind(findLayer.Table,findLayer.Table.TableInfo.Columns[columnName]);
FindResultfindResult=find.Search(strFind);
if(findResult.ExactMatch)
{
//Setthemap'scenterandzoom
map.Center=newDPoint(findResult.FoundPoint.X,findResult.FoundPoint.Y);
MapInfo.Geometry.Distanced=newMapInfo.Geometry.Distance(2,map.Zoom.Unit);
map.Zoom=d;
lbSearch.Text="";
}
else
{
lbSearch.Text="CannotfindthePoint";
}

find.Dispose();
}
(3)能过构造Sql语句进行查找,示例代码如下:
/**////<summary>
///通过Sql语句查找图元
///DesignbyGlacier
///2008年8月6日
///<paramname="tableName">查找的表名</param>
///<paramname="columnName">查找的列名</param>
///<paramname="strKey">查找的关键字</param>
///</summary>
publicstaticvoidSearchWithSql(stringtableName,stringcolumnName,stringstrKey)
{
MapInfo.Data.MIConnectionmiConnection=newMIConnection();
miConnection.Open();
MapInfo.Data.MICommandmiCommand=miConnection.CreateCommand();
miCommand.CommandText="select*from"+tableName+"where"+columnName+"like'%'+@name+'%'";
miCommand.Parameters.Add("@name",strKey);
IResultSetFeatureCollectionifs=miCommand.ExecuteFeatureCollection();
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
MapInfo.Mapping.MapmyMap=MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
if(ifs.Count<=0)
{
lbSearch.Text="Cannotfindthepoint";
}
else
{
//高亮显示
lbSearch.Text="";
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
if(ifs.Count==1)
{
myMap.Center=newDPoint(ifs[0].Geometry.Centroid.x,ifs[0].Geometry.Centroid.y);
MapInfo.Geometry.Distanced=newMapInfo.Geometry.Distance(0.5,myMap.Zoom.Unit);
myMap.Zoom=d;
}
else
{
myMap.SetView(ifs.Envelope);
}
}
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-28344-1.html
这些人怎么不先反思反思自己