1开启动态显示功能;
2 创建自定义的动态图层并重写相关的绘图方法(上面提到的第一种方式);
3在Map中添加自定义的动态图层;
4 实时数据的传递.
1.7.1 开启动态地图功能
void InitMap()
{
IAGSServerObjectName pServerObjectName = GetMapServer("http://services.arcgisonline.com/ArcGIS/services", "ESRI_Imagery_World_2D", false);
IName pName = (IName)pServerObjectName;
IAGSServerObject pServerObject = (IAGSServerObject)pName.Open();
IMapServer pMapServer = (IMapServer)pServerObject;
ESRI.ArcGIS.Carto.IMapServerLayer pMapServerLayer = new ESRI.ArcGIS.Carto.MapServerLayerClass();
pMapServerLayer.ServerConnect(pServerObjectName, pMapServer.DefaultMapName);
axMapControl1.AddLayer(pMapServerLayer as ILayer);
axMapControl1.Refresh();
pActvie = axMapControl1.Map as IActiveView;
IDynamicMap pDynamicMap = pActvie.FocusMap as IDynamicMap;
pDynamicMap.DynamicMapEnabled = true;
pDynamicMap.DynamicDrawRate = 15;
}
1.7.2 定义自定义的动态图层并重写DrawDynamicLayer方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Timers;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.ADF.Connection.Local;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.SystemUI;
namespace QARFlight
{
public sealed class FlightDynamicLayer : BaseDynamicLayer
{
public bool m_bOnce = true;
private IDynamicGlyph m_myGlyph = null;
IDynamicGlyph m_lineGlyph = null;
private IDynamicSymbolProperties2 m_dynamicSymbolProps = null;
private IDynamicSymbolProperties2 m_ldynamicSymbolProps = null;
private IPoint m_point = null;
private Timer m_updateTimer = null;
public double Course;
public FlightDynamicLayer()
: base()
{
base.m_sName = "My Dynamic layer";
m_updateTimer = new Timer(10);
m_updateTimer.Enabled = false;
m_updateTimer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
}
public void Draw(IPoint pPoint)
{
this.m_point = pPoint;
}
IPointCollection pPointCollection;
///
///更新飞机的位置和姿态
///
///
///
///
public void Update(double x, double y, double Course)
{
if (this.m_point == null)
{
m_point = new PointClass();
pPointCollection = new PolylineClass() as IPointCollection;
}
this.m_point.X = x;
this.m_point.Y = y;
this.Course = Course;
pPointCollection.AddPoint(this.m_point);
}
///
///获取线的渲染符号
///
///
///
///
///
public ESRI.ArcGIS.Display.ISimpleLineSymbol GetSimpleLineSymbol(ESRI.ArcGIS.Display.IRgbColor rgbColor, System.Double inWidth, ESRI.ArcGIS.Display.esriSimpleLineStyle inStyle)
{
if (rgbColor == null)
{
return null;
}
ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
simpleLineSymbol.Style = inStyle;
simpleLineSymbol.Color = rgbColor;
simpleLineSymbol.Width = inWidth;
return simpleLineSymbol;
}
public override void DrawDynamicLayer(esriDynamicDrawPhase DynamicDrawPhase, IDisplay Display, IDynamicDisplay DynamicDisplay)
{
if (DynamicDrawPhase != esriDynamicDrawPhase.esriDDPImmediate)
return;
if (!m_bValid || !m_visible)
return;
IEnvelope visibleExtent = Display.DisplayTransformation.FittedBounds;
IDynamicGlyphFactory dynamicGlyphFactory = null;
if (m_bOnce)
{
dynamicGlyphFactory = DynamicDisplay.DynamicGlyphFactory;
m_dynamicSymbolProps = DynamicDisplay as IDynamicSymbolProperties2;
m_ldynamicSymbolProps = DynamicDisplay as IDynamicSymbolProperties2;
IDynamicGlyphFactory2 dynamicGlyphFactory2 = dynamicGlyphFactory as IDynamicGlyphFactory2;
IPictureMarkerSymbol pPicture = new PictureMarkerSymbolClass();
if (this.Course < 0)
{
this.Course = Math.Abs(this.Course);
}
pPicture.Angle = Course;
pPicture.Size = 46;
pPicture.BitmapTransparencyColor = ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.FromArgb(255, 255, 255)) as IColor;
pPicture.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, @".\plane.bmp");
m_myGlyph = dynamicGlyphFactory.CreateDynamicGlyph(pPicture as ISymbol);
m_updateTimer.Enabled = true;
m_bOnce = false;
ISimpleLineSymbol pSimpleLineSym = GetSimpleLineSymbol(ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.FromArgb(0, 255, 0)) as IRgbColor, 2, esriSimpleLineStyle.esriSLSSolid);
m_lineGlyph = dynamicGlyphFactory.CreateDynamicGlyph((ISymbol)pSimpleLineSym);
}
m_dynamicSymbolProps.set_DynamicGlyph(esriDynamicSymbolType.esriDSymbolMarker, m_myGlyph);
m_dynamicSymbolProps.SetScale(esriDynamicSymbolType.esriDSymbolMarker, 1.0f, 1.0f);
m_dynamicSymbolProps.set_RotationAlignment(esriDynamicSymbolType.esriDSymbolMarker, esriDynamicSymbolRotationAlignment.esriDSRAScreen);
DynamicDisplay.DrawMarker(m_point);
if (pPointCollection.PointCount > 1)
{
m_ldynamicSymbolProps.set_DynamicGlyph(esriDynamicSymbolType.esriDSymbolLine, m_lineGlyph);
m_ldynamicSymbolProps.SetScale(esriDynamicSymbolType.esriDSymbolLine, 1.0f, 1.0f);
DynamicDisplay.DrawPolyline(pPointCollection);
}
//绘制完毕
base.m_bIsImmediateDirty = false;
}
void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
//确保下一个绘图周期
base.m_bIsImmediateDirty = true;
}
}
}
1.7.3 加载动态图层
FlightDynamicLayer pFlightLayer = new FlightDynamicLayer();
Global.MainForm.pActvie.FocusMap.AddLayer(pFlightLayer as ILayer);
1.7.4 动态绘制数据
pFlightLayer.Update(point.X, point.Y, Course);
public IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
{
//设置连接属性
IPropertySet pPropertySet = new PropertySetClass();
if (pIsLAN)
pPropertySet.SetProperty("machine", pHostOrUrl);
else
pPropertySet.SetProperty("url", pHostOrUrl);
//打开连接
IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
pServerObjectNames.Reset();
IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
while (ServerObjectName != null)
{
if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
(ServerObjectName.Type == "MapServer") )
{
break;
}
ServerObjectName = pServerObjectNames.Next();
}
//返回对象
return ServerObjectName;
}