zoukankan      html  css  js  c++  java
  • MapXtreme 2005学习(5):总结查找图元的三种方法

    在MapXtreme 2005中,查找图元提供了非常多的方法,也非常容易实现,这里总结了三种方法。

    (1)Search方法是非常强大的,可以实现几乎所有的查找,这也是最常用的查找方式。示例代码如下:

        /// <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)
        
    {
            MapInfo.Mapping.Map map 
    = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];

            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)
            
    {
                lbSearch.Text 
    = "Cannot find the point";
            }

            
    else
            
    {
                
    //高亮显示
                MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
                lbSearch.Text 
    = "";
                
    if (ifs.Count == 1)
                
    {
                    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
                
    {
                    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)
                
    {
                    ListBox1.Items.Add(feature[
    "name"].ToString());
                }

            }

        }

    (2)通过构造Find对象,进行查找。示例代码如下:

        /// <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)
        
    {
            Find find 
    = null;
            MapInfo.Mapping.Map map 
    = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];

            
    // 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)
            
    {
                
    // 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
            
    {
                lbSearch.Text 
    = "Cannot find the Point";
            }

            find.Dispose();
        }

    (3)能过构造Sql语句进行查找,示例代码如下:

       /// <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)
        
    {
            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)
            
    {
                lbSearch.Text 
    = "Cannot find the point";
            }

            
    else
            
    {
                
    //高亮显示
                lbSearch.Text = "";
                MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
                
    if (ifs.Count == 1)
                
    {
                    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
                
    {
                    myMap.SetView(ifs.Envelope);
                }

            }

        }
  • 相关阅读:
    深入浅出WPF-04.x名称空间详解
    深入浅出WPF-03.XAML语法
    深入浅出WPF-02.WPF系列目录
    深入浅出WPF-01.WPF缘起
    IdentityServer4[3]:使用客户端认证控制API访问(客户端授权模式)
    IdentityServer4[2]:启动一个新的IdentityServer项目
    基于UDP实现多媒体即时通信机制
    顺丰快递单号查询api接口快递鸟免费对接参数demo
    常用电子面单接口API demo下载(菜鸟&快递鸟)
    Solution -「YunoOI 2016」镜中的昆虫
  • 原文地址:https://www.cnblogs.com/glacierh/p/1262291.html
Copyright © 2011-2022 走看看