在MapXtreme 2005中,查找图元提供了非常多的方法,也非常容易实现,这里总结了三种方法。
(1)Search方法是非常强大的,可以实现几乎所有的查找,这也是最常用的查找方式。示例代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 通过Search方法查找图元
/// Design by Glacier
/// 2008年8月6日
/// <param name="tableName">查找的表名</param>
/// <param name="columnName">查找的列名</param>
/// <param name="strKey">查找的关键字</param>
/// </summary>
public static void SearchWithSearch(string tableName, string columnName, string strKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere(columnName + " like '%" + strKey + "%'");
IResultSetFeatureCollection ifs = MapInfo.Engine.Session.Current.Catalog.Search(tableName, si);
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
if (ifs.Count <= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lbSearch.Text = "Cannot find the point";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//高亮显示
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
lbSearch.Text = "";
if (ifs.Count == 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
map.Center = new DPoint(ifs[0].Geometry.Centroid.x, ifs[0].Geometry.Centroid.y);
MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(0.5, map.Zoom.Unit);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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 (Feature feature in ifs)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ListBox1.Items.Add(feature["name"].ToString());
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(2)通过构造Find对象,进行查找。示例代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 通过Find查找图元
/// Design by Glacier
/// 2008年8月6日
/// <param name="layerName">查找的图层名</param>
/// <param name="columnName">查找的列名</param>
/// <param name="strKey">查找的关键字</param>
/// </summary>
public static void SearchWithFind(string layerName, string columnName, string strKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Find find = null;
MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Do the find
MapInfo.Mapping.FeatureLayer findLayer = (MapInfo.Mapping.FeatureLayer)map.Layers[PointLayerName];
find = new Find(findLayer.Table, findLayer.Table.TableInfo.Columns[columnName]);
FindResult findResult = find.Search(strFind);
if (findResult.ExactMatch)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Set the map's center and zoom
map.Center = new DPoint(findResult.FoundPoint.X, findResult.FoundPoint.Y);
MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(2, map.Zoom.Unit);
map.Zoom = d;
lbSearch.Text = "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lbSearch.Text = "Cannot find the Point";
}
find.Dispose();
}
(3)能过构造Sql语句进行查找,示例代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 通过Sql语句查找图元
/// Design by Glacier
/// 2008年8月6日
/// <param name="tableName">查找的表名</param>
/// <param name="columnName">查找的列名</param>
/// <param name="strKey">查找的关键字</param>
/// </summary>
public static void SearchWithSql(string tableName, string columnName, string strKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MapInfo.Data.MIConnection miConnection = new MIConnection();
miConnection.Open();
MapInfo.Data.MICommand miCommand = miConnection.CreateCommand();
miCommand.CommandText = "select * from " + tableName + " where " + columnName + " like '%'+@name+'%'";
miCommand.Parameters.Add("@name", strKey);
IResultSetFeatureCollection ifs = miCommand.ExecuteFeatureCollection();
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
if (ifs.Count <= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lbSearch.Text = "Cannot find the point";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//高亮显示
lbSearch.Text = "";
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
if (ifs.Count == 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myMap.Center = new DPoint(ifs[0].Geometry.Centroid.x, ifs[0].Geometry.Centroid.y);
MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(0.5, myMap.Zoom.Unit);
myMap.Zoom = d;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myMap.SetView(ifs.Envelope);
}
}
}