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