This document is archived and information here might be outdated.  Recommended version.


Toggle Between Surface and Global Navigation Mode Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Toggle Between Surface and Global Navigation Mode Snippet

Toggle between global navigation mode and surface navigation mode.

[C#]
///<summary>Toggle between global navigation mode and surface navigation mode.</summary>
/// 
///<param name="globe">An IGlobe interface</param>
///  
///<remarks></remarks>
public void ToggleBetweenSurfaceAndGlobalNavigationMode(ESRI.ArcGIS.GlobeCore.IGlobe globe)
{
  // get Globe Camera in the globe display
  ESRI.ArcGIS.GlobeCore.IGlobeDisplay globeDisplay=globe.GlobeDisplay;
  ESRI.ArcGIS.Analyst3D.ISceneViewer sceneViewer=globeDisplay.ActiveViewer;
  ESRI.ArcGIS.Analyst3D.ICamera camera=sceneViewer.Camera;
  ESRI.ArcGIS.GlobeCore.IGlobeCamera globeCamera=(ESRI.ArcGIS.GlobeCore.IGlobeCamera)camera; // Explicit Cast
  ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode globeCameraNavigateMode=globeCamera.OrientationMode;
  ESRI.ArcGIS.GlobeCore.esriGlobeNavigationType globeNavigationType=globeCamera.NavigationType;

  if (globeCameraNavigateMode == ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal)
  {
    globeCamera.OrientationMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal;

    // do not change position if flying
    if (globeNavigationType == ESRI.ArcGIS.GlobeCore.esriGlobeNavigationType.esriGlobeNavigationAttached)
    {
      System.Double targetLatitude;
      System.Double targetLongitude;
      System.Double targetAltitude;
      globeCamera.GetTargetLatLonAlt(out targetLatitude, out targetLongitude, out targetAltitude);

      System.Double observerLatitude;
      System.Double obsLongitude;
      System.Double obsAltitude;
      globeCamera.GetObserverLatLonAlt(out observerLatitude, out obsLongitude, out obsAltitude);

      // In order to change the orientation mode to Global, we have to set the target of the camera of active viewer at the center of the earth.
      ESRI.ArcGIS.Geometry.IPoint point=new ESRI.ArcGIS.Geometry.PointClass();
      point.PutCoords(0, 0);
      point.Z=0;
      camera.Target=point;
      camera.RollAngle=0;
      camera.RecalcUp();

       // Set the GlobeCamera to Global Navigation mode.
       globeCamera.OrientationMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal;
       globeCamera.SetObserverLatLonAlt(targetLatitude, targetLongitude, obsAltitude);

       globeDisplay.RefreshViewers();
    }
  }
  else
  {
    // do not change position if flying
    if (globeNavigationType == ESRI.ArcGIS.GlobeCore.esriGlobeNavigationType.esriGlobeNavigationAttached)
    {
      System.Double azimuth=camera.Azimuth;
      System.Double inclination=camera.Inclination;
      System.Double distance=camera.ViewingDistance;

       ESRI.ArcGIS.Geometry.IPoint point=new ESRI.ArcGIS.Geometry.PointClass();
       System.Int32 originX=0;
       System.Int32 originY=0;
       System.Int32 width=0;
       System.Int32 height=0;

        // Get the width and height of the main viewer via the viewport
        camera.GetViewport(ref originX, ref originY, ref width, ref height);

        System.Object owner;
        System.Object ipObject;

        // Get the location at the center point of the main viewer
        globeDisplay.Locate(sceneViewer, width / 2, height / 2, true, true, out point, out owner, out ipObject);

        ESRI.ArcGIS.GlobeCore.IGlobeViewUtil globeViewUtil=(ESRI.ArcGIS.GlobeCore.IGlobeViewUtil)globeCamera; // Explicit cast

        System.Double xTarget;
        System.Double yTarget;
        point.QueryCoords(out xTarget, out yTarget);

        System.Double zTarget=point.Z;

        // Calculate the current Azimuth and Inclination of the camera
        azimuth=System.Math.Atan2(xTarget, yTarget) * 180 / System.Math.PI;
        inclination=(180 / System.Math.PI) * (System.Math.Asin(zTarget / System.Math.Sqrt(xTarget * xTarget + yTarget * yTarget + zTarget * zTarget))) - 10.0;

        if (inclination > 88)
        {
          inclination=88;
        }
        else if (inclination < -88)
        {
          inclination=-88;
        }
        camera.Target=point;
        camera.Azimuth=azimuth;
        camera.Inclination=inclination;
        camera.ViewingDistance=distance - 1.0;
     }

     // Change to surface navigation mode
     globeCamera.OrientationMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal;
     globeDisplay.RefreshViewers();
  }
}
[Visual Basic .NET]
'''<summary>Toggle between global navigation mode and surface navigation mode.</summary>
''' 
'''<param name="globe">An IGlobe interface</param>
'''  
'''<remarks></remarks>
Public Sub ToggleBetweenSurfaceAndGlobalNavigationMode(ByVal globe As ESRI.ArcGIS.GlobeCore.IGlobe)

  ' get Globe Camera in the globe display
  Dim globeDisplay As ESRI.ArcGIS.GlobeCore.IGlobeDisplay=globe.GlobeDisplay
  Dim sceneViewer As ESRI.ArcGIS.Analyst3D.ISceneViewer=globeDisplay.ActiveViewer
  Dim camera As ESRI.ArcGIS.Analyst3D.ICamera=sceneViewer.Camera
  Dim globeCamera As ESRI.ArcGIS.GlobeCore.IGlobeCamera=CType(camera, ESRI.ArcGIS.GlobeCore.IGlobeCamera) ' Explicit Cast
  Dim globeCameraNavigateMode As ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode=globeCamera.OrientationMode
  Dim globeNavigationType As ESRI.ArcGIS.GlobeCore.esriGlobeNavigationType=globeCamera.NavigationType

  If globeCameraNavigateMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal Then
    globeCamera.OrientationMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal

    ' do not change position if flying
    If globeNavigationType=ESRI.ArcGIS.GlobeCore.esriGlobeNavigationType.esriGlobeNavigationAttached Then
      Dim targetLatitude As System.Double
      Dim targetLongitude As System.Double
      Dim targetAltitude As System.Double
      globeCamera.GetTargetLatLonAlt(targetLatitude, targetLongitude, targetAltitude)

      Dim observerLatitude As System.Double
      Dim obsLongitude As System.Double
      Dim obsAltitude As System.Double
      globeCamera.GetObserverLatLonAlt(observerLatitude, obsLongitude, obsAltitude)

      ' In order to change the orientation mode to Global, we have to set the target of the camera of active viewer at the center of the earth.
      Dim point As ESRI.ArcGIS.Geometry.IPoint=New ESRI.ArcGIS.Geometry.PointClass()
      point.PutCoords(0, 0)
      Point.Z=0
      camera.Target=Point
      camera.RollAngle=0
      camera.RecalcUp()

      ' Set the GlobeCamera to Global Navigation mode.
      globeCamera.OrientationMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal
      globeCamera.SetObserverLatLonAlt(targetLatitude, targetLongitude, obsAltitude)

      globeDisplay.RefreshViewers()
    End If
  Else
    ' do not change position if flying
    If globeNavigationType=ESRI.ArcGIS.GlobeCore.esriGlobeNavigationType.esriGlobeNavigationAttached Then
      Dim azimuth As System.Double=camera.Azimuth
      Dim inclination As System.Double=camera.Inclination
      Dim distance As System.Double=camera.ViewingDistance

      Dim point As ESRI.ArcGIS.Geometry.IPoint=New ESRI.ArcGIS.Geometry.PointClass()
      Dim originX As System.Int32=0
      Dim originY As System.Int32=0
      Dim width As System.Int32=0
      Dim height As System.Int32=0

      ' Get the width and height of the main viewer via the viewport
      camera.GetViewport(originX, originY, width, height)

      Dim owner As System.Object=Nothing
      Dim ipObject As System.Object=Nothing

      ' Get the location at the center point of the main viewer
      globeDisplay.Locate(sceneViewer, CInt(width / 2), CInt(height / 2), True, True, point, owner, ipObject)

      Dim globeViewUtil As ESRI.ArcGIS.GlobeCore.IGlobeViewUtil=CType(globeCamera, ESRI.ArcGIS.GlobeCore.IGlobeViewUtil) ' Explicit cast
      Dim xTarget As System.Double
      Dim yTarget As System.Double
      point.QueryCoords(xTarget, yTarget)

      Dim zTarget As System.Double=point.Z

      ' Calculate the current Azimuth and Inclination of the camera
      azimuth=System.Math.Atan2(xTarget, yTarget) * 180 / System.Math.PI
      inclination=180 / System.Math.PI * System.Math.Asin((zTarget / System.Math.Sqrt((xTarget * xTarget + yTarget * yTarget + zTarget * zTarget)))) - 10.0

      If inclination > 88 Then
        inclination=88
      Else
        If inclination < -88 Then
          inclination=-88
        End If
      End If
      camera.Target=point
      camera.Azimuth=azimuth
      camera.Inclination=inclination
      camera.ViewingDistance=distance - 1.0
    End If

    ' Change to surface navigation mode
    globeCamera.OrientationMode=ESRI.ArcGIS.GlobeCore.esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal
    globeDisplay.RefreshViewers()
  End If

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.3DAnalyst
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.GlobeCore
  • ESRI.ArcGIS.System
  • System