http://blog.csdn.net/lysc_forever/article/details/7790211
在实际的Globe操作过程中,都希望能够控制当前场景的范围,即使是误操作超出了范围也能够快速的自动的回到初始的范围。以下代码根据图层的范围控制显示操作的范围,一旦超出图层的范围,根据重绘事件监听,将当前地图范围重置到初始化范围。
系统环境:Win7 64位旗舰版
GIS环境:ArcEngine 10 sp4
开发环境:VS 2012 SP1 C#
1、在frmMain主窗体中声明IGlobeDisplayEvents_Event事件
private IGlobeDisplayEvents_Event m_pGlobeDisplayEvents = null;
2、在窗体初始化的时候,对m_pGlobeDisplayEvents也进行初始化
public frmMain()
{
InitializeComponent();
IGlobe pGlobe = globeCtrl3D.Globe;// globeCtrl3D为Globe控件
m_pGlobeDisplayEvents = pGlobe.GlobeDisplay as IGlobeDisplayEvents_Event;
m_pGlobeDisplayEvents.AfterDraw += new IGlobeDisplayEvents_AfterDrawEventHandler(m_pGlobeDisplayEvents_AfterDraw);
}
3、重写m_pGlobeDisplayEvents_AfterDraw事件
public void m_pGlobeDisplayEvents_AfterDraw(ISceneViewer pViewer)
{
try
{
IGlobeCamera pGlobeCamera = globeCtrl3D.GlobeCamera;
double dObserverLatDD = 0.0;
double dObserverLonDD = 0.0;
double dObserverAltKMs = 0.0;
pGlobeCamera.GetObserverLatLonAlt(out dObserverLatDD,out dObserverLonDD, out dObserverAltKMs);
ILayer layer = Common.GlobeUtil.getLayerFromScene(globeCtrl3D.GlobeDisplay.Scene,"bj");//bj为要控制范围的图层,getLayerFromScene为在GlobeControl中获取相应图层的函数,这里不再赘述
IEnvelope pInitEnvelope = layer.AreaOfInterest;
double dXMin = pInitEnvelope.XMin;
double dXMax = pInitEnvelope.XMax;
double dYMin = pInitEnvelope.YMin;
double dYMax = pInitEnvelope.YMax;
// 以下判断当前操作是否在图层范围内,高度控制在0至20公里(可以根据实际情况设置,此时为地上模式;如果想要地下模式可以将其改为负数)
if ((dObserverLatDD < dYMin) || (dObserverLatDD > dYMax) || (dObserverLonDD < dXMin) || (dObserverLonDD > dXMax)
|| (dObserverAltKMs > 20) || (dObserverAltKMs < 0))
{// 以下为获取初始化的操作范围,该范围设置在App.Config中
double dInitObserverLatDD =Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["ObserverLatDD"]);
double dInitObserverLonDD =Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["ObserverLonDD"]);
double dInitObserverAltKMs =Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["ObserverAltKMs"]);
double dInitTargetLatDD =Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["TargetLatDD"]);
double dInitTargetLonDD =Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["TargetLonDD"]);
double dInitTargetAltKMs =Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["TargetAltKMs"]);
pGlobeCamera.SetObserverLatLonAlt(dInitObserverLatDD, dInitObserverLonDD, dInitObserverAltKMs);
pGlobeCamera.SetTargetLatLonAlt(dInitTargetLatDD, dInitTargetLonDD, dInitTargetAltKMs);
globeCtrl3D.GlobeDisplay.RefreshViewers();
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
4、App.Config中的初始化参数为
<appSettings>
<!--初始化的位置-->
<addkey="ObserverLatDD"value="-0.056946961416494392" />
<addkey="ObserverLonDD"value="121.49905414870099" />
<addkey="ObserverAltKMs"value="3.9120639485526882" />
<addkey="TargetLatDD"value="-0.028060602957429747" />
<addkey="TargetLonDD"value="121.49892786453003" />
<addkey="TargetAltKMs"value="0.5625919130259821" />
……
5、以上完成在GlobeControl中控制可操作的范围,如果超出了自动恢复到初始位置。