• MapXtreme 2005学习(6):两种方法实现动态轨迹


    在GIS中,动态轨迹的实现是非常有用的,可用GPS定位,热点跟踪等。在本例中,先创建一个用于呈现动态轨迹的临时图层,并在图层上添加一个点表示位体的位置。代码如下:

        /// <summary>
        
    /// 创建动态轨迹图层
        
    /// Glacier
        
    /// 2008年8月7日
        
    /// <param name="trackLayerTableName">图层表名</param>
        
    /// <param name="trackLayerName">图层名</param>
        
    /// <param name="firstPoint">点初始坐标</param>
        
    /// </summary>

        protected void CreateTrackLayer(string trackLayerTableName, string trackLayerName, DPoint firstPoint)
        
    {
            MapInfo.Mapping.Map myMap 
    = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];

            
    //创建临时图层
            MapInfo.Data.TableInfoMemTable tblInfoTemp = new MapInfo.Data.TableInfoMemTable(trackLayerTableName);
            MapInfo.Data.Table tblTemp 
    = MapInfo.Engine.Session.Current.Catalog.GetTable(trackLayerTableName);
            
    if (tblTemp != null)
            
    {
                MapInfo.Engine.Session.Current.Catalog.CloseTable(trackLayerTableName);
            }


            tblInfoTemp.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(myMap.GetDisplayCoordSys()));
            tblInfoTemp.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn());
            tblTemp 
    = MapInfo.Engine.Session.Current.Catalog.CreateTable(tblInfoTemp);

            FeatureLayer workLayer 
    = new FeatureLayer(tblTemp, AnimationLayerName, AnimationLayerName);
            myMap.Layers.Add(workLayer);

            
    //向临时图层中添加初始点
            FeatureGeometry pfg = new MapInfo.Geometry.Point(workLayer.CoordSys, firstPoint.x, firstPoint.y) as FeatureGeometry;
            MapInfo.Styles.CompositeStyle cstyle 
    = new MapInfo.Styles.CompositeStyle(new MapInfo.Styles.SimpleVectorPointStyle(52, System.Drawing.Color.Blue, 30));

            MapInfo.Data.Feature pft 
    = new MapInfo.Data.Feature(tblTemp.TableInfo.Columns);
            pft.Geometry 
    = pfg;
            pft.Style 
    = cstyle;
            workLayer.Table.InsertFeature(pft);
        }

    实现动态轨迹的第一种方法是,对原来点的坐标的位置进行偏移,从实现位置的更新。代码如下:

      /// <summary>
        
    /// 把原来的点偏移到新的位置以实现动态轨迹
        
    /// Glacier
        
    /// 2008年8月7日
        
    /// <param name="trackLayerTableName">图层表名</param>
        
    /// <param name="newPoint">点的新坐标</param>
        
    /// </summary>

        protected void UpdateTrack(string trackLayerTableName, DPoint newPoint)
        
    {
            MapInfo.Data.Table altb 
    = MapInfo.Engine.Session.Current.Catalog.GetTable(trackLayerTableName);
            
    if (altb == null)
            
    {
                
    return;
            }


            
    //Change the positon of the existed feature.
            SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere("");
            Feature ftr 
    = MapInfo.Engine.Session.Current.Catalog.SearchForFeature(altb, si);
            
    if (ftr == null)
            
    {
                
    return;
            }

            DPoint offsetPoint 
    = new DPoint(newPoint.x - ftr.Geometry.Centroid.x, newPoint.y - ftr.Geometry.Centroid.y);
            ftr.Geometry.GetGeometryEditor().OffsetByXY(offsetPoint.x, offsetPoint.y, DistanceUnit.Degree, DistanceType.Spherical);
            ftr.Geometry.EditingComplete();
            ftr.Update();
        }

    实现动态轨迹的第二种方法是,先删除原有的点,再在新的位置添加一个新点。因为第一种方法求偏移过程中可能会产生误差,并且这种误差是会积累的。而这种方法相对来说会比较精确一点。代码如下:

       /// <summary>
        
    /// 把删除原有点并向图层中添加新点以实现动态轨迹
        
    /// Glacier
        
    /// 2008年8月7日
        
    /// <param name="trackLayerTableName">图层表名</param>
        
    /// <param name="newPoint">点的新坐标</param>
        
    /// </summary>

        protected void UpdateTrack(string trackLayerTableName, DPoint newPoint)
        
    {
            MapInfo.Data.Table altb 
    = MapInfo.Engine.Session.Current.Catalog.GetTable(trackLayerTableName);
            
    if (altb == null)
            
    {
                
    return;
            }


            
    //Delete the existed feature and create a new one.
            SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere("");
            Feature dftr 
    = MapInfo.Engine.Session.Current.Catalog.SearchForFeature(altb, si);
            
    if (dftr == null)
            
    {
                
    return;
            }

            altb.DeleteFeature(dftr);
            MapInfo.Mapping.Map myMap 
    = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
            FeatureGeometry pfg 
    = new MapInfo.Geometry.Point(myMap.GetDisplayCoordSys(), new DPoint(newPoint.x, newPoint.y)) as FeatureGeometry;
            MapInfo.Styles.CompositeStyle cstyle 
    = new MapInfo.Styles.CompositeStyle(new MapInfo.Styles.SimpleVectorPointStyle(52, System.Drawing.Color.Blue, 30));

            MapInfo.Data.Feature pft 
    = new MapInfo.Data.Feature(altb.TableInfo.Columns);
            pft.Geometry 
    = pfg;
            pft.Style 
    = cstyle;
            altb.InsertFeature(pft);
        }
  • 相关阅读:
    IBatisNet不常用到的配置(Dao.config ConnectionTimeout),居然不起作用(前辈留给我们的坑)
    随机数 字母 数字
    证书文件(pfx)读取时报 “指定的网络密码不正确”
    SQL多结果集导出Excel
    Uva514
    PAT乙级1012
    栈-41
    位运算-89
    PAT乙级1028
    PAT乙级1029
  • 原文地址:https://www.cnblogs.com/glacierh/p/1263044.html
Copyright © 2020-2023  润新知