在GIS中,動态軌迹的實作是非常有用的,可用GPS定位,熱點跟蹤等。在本例中,先建立一個用于呈現動态軌迹的臨時圖層,并在圖層上添加一個點表示位體的位置。代碼如下:
複制内容到剪貼闆
代碼:
/** <summary>
/// 建立動态軌迹圖層
/// 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);
}