Click or drag to resize
Code Example: Camera_SetMethods

Demonstrates navigating/animating between MapPoint Graphics with a Camera. Simultaneous changes of X, Y, Z, Pitch, and Heading during the animation is shown.

Code Example

SPECIAL INSTRUCTIONS: This example code reads information from a text file to display Graphics with Attributes in the SceneView. Use a text editor program (like Notepad) to add the MapPoint positional and attribute information found in the top of the MainWindows.xaml.vb/MainWindow.xaml.cs file into the TestData.txt file and save the file on disk. The elements of the text files are comma separated ASCII text that have the form: X,Y,Z,Pitch,Heading.

The following screen shots correspond of the code example in this page:

Camera Set Methods
Document Folder
Test Data txt

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="Camera_SetMethods.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    Title="MainWindow" Height="600" Width="800">
    <Grid x:Name="LayoutRoot">

        <!-- Define a Marker symbol that will be used in the code-behind to draw graphics on the screen. -->
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="MyMarkerSymbol" Color="Red" Style="Circle" Size="5" />
        </Grid.Resources>

        <StackPanel Orientation="Vertical" Margin="10">

            <!-- TextBlock to hold the instructions on how to use the sample code. -->
            <StackPanel Orientation="Horizontal" >
                <TextBlock Height="70"  Width="775" HorizontalAlignment="Left" Name="TextBlock1" TextWrapping="Wrap" />
            </StackPanel>

            <!-- Buttons to run the code example. -->
            <StackPanel Orientation="Horizontal">
                <Button x:Name="Button1" Content="Load Graphics Data from TextFile" Click="Button1_Click" Margin="0,0,5,0"/>
                <Button x:Name="Button2" Content="Navigate via Camera Methods" Click="Button2_Click" IsEnabled="False"/>
            </StackPanel>

            <!-- Add a SceneView Control to display 3D data.-->
            <esri:SceneView x:Name="SceneView1" Width="775" Height="458" Margin="0,0,-3,0" >
                <esri:Scene>

                    <!-- Add a backdrop ArcGISDynamicMapServiceLayer. -->
                    <esri:ArcGISDynamicMapServiceLayer ID="World_Light_Gray_Base" 
                          ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"/>

                    <!-- Add a GraphicsLayer that will hold a MapPoints to animate along. -->
                    <esri:GraphicsLayer ID="MyGraphicsLayer" RenderingMode="Dynamic"/>
                </esri:Scene>
            </esri:SceneView>

        </StackPanel>
    </Grid>
</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "Camera_SetMethods.MainWindow" to be just "MainWindow".

// SPECIAL INSTRUCTIONS:
// This example code reads information from a text file to display Graphics with Attributes on the Scene. Use a text 
// editor program (like Notepad) to add the following lines into the TestData.txt file and save the file on disk. 
// The elements of the text files are: X,Y,Z,Pitch,Heading.
// 
// NOTE: REMOVE THE STARTING COMMENT (' or //) BEFORE EACH LINE!
// 
// 0,0,2000000,10,0
// 5,0,2000000,15,0
// 10,0,2000000,20,0
// 15,0,2000000,25,0
// 20,0,2000000,30,0
// 25,0,2000000,35,0
// 30,0,2000000,45,0
// 35,0,2000000,40,0
// 40,0,2000000,30,0
// 45,0,2000000,10,0
// 45,5,2000000,0,0
// 40,5,2000000,0,15
// 35,5,2000000,0,30
// 30,5,2000000,0,45
// 25,5,2000000,0,60
// 20,5,2000000,0,75
// 15,5,2000000,0,90
// 10,5,2000000,0,105
// 5,5,2000000,0,120
// 0,5,2000000,0,135
// 0,10,2000000,10,0
// 5,10,500000,15,15
// 10,10,1000000,20,30
// 15,10,1500000,25,45
// 20,10,2000000,30,60
// 25,10,2500000,35,75
// 30,10,3000000,45,90
// 35,10,3500000,40,105
// 40,10,4000000,30,120
// 45,10,4500000,10,135

namespace Camera_SetMethods
{
    public partial class MainWindow : System.Windows.Window
    {
        // Member (aka. Global) variable for the Camera.
        public Esri.ArcGISRuntime.Controls.Camera _MyCamera;

        public MainWindow()
        {
            InitializeComponent();

            // Add instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads, click the 'Load Graphics Data from TextFile' button and navigate on the hard drive to the TestData.txt file." + 
                "This will contain sample data to load a series of MapPoint based Graphics into a GraphicsLayer. Then click the 'Navigate via Camera Methods' button to " + 
                "animate along the sequence of Graphics. The values for the MapPoint's X (Longitude), Y (Latitude), Z (Elevation), as well as the Pitch, and Heading will" +
                "continuously adjust as the SceneView.SetViewAsync Method is executed.";

            // Center the SceneView on the continent of Africa
            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = new Esri.ArcGISRuntime.Geometry.MapPoint(22.532, 4.726, 11500000, Esri.ArcGISRuntime.Geometry.SpatialReferences.Wgs84);
            _MyCamera = new Esri.ArcGISRuntime.Controls.Camera(myMapPoint, 2.373, 0);
            SceneView1.SetView(_MyCamera);
        }

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // This function will load data that is in a text file (.txt) on the local hard drive as display that information 
            // as a GraphicsLayer (based upon MapPoints with Attributes) in the SceneView.

            // Get the empty GraphicsLayer that was created in XAML.
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)SceneView1.Scene.Layers["MyGraphicsLayer"];

            // Create an instance of the open file dialog box.
            Microsoft.Win32.OpenFileDialog theOpenFileDialog = new Microsoft.Win32.OpenFileDialog();

            // Set filter options and filter index.
            theOpenFileDialog.Filter = "Text Files (*.txt)|*.txt";
            theOpenFileDialog.FilterIndex = 1;

            // Only let the user select one text file at a time.
            theOpenFileDialog.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            bool UserClickedOK = (bool)theOpenFileDialog.ShowDialog();

            // Process input if the user clicked OK.
            if (UserClickedOK == true)
            {
                //Open the selected file to read.
                System.IO.Stream fileStream = theOpenFileDialog.OpenFile();

                // Use a StreamReader to read the contents of the text file.
                using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                {
                    // Hold the contents of reading one line from the text file.
                    string line = null;

                    // Loop through the text file.
                    do
                    {
                        // Read one line from the StreamReader.
                        line = reader.ReadLine();

                        // Only process valid content
                        if (line != null)
                        {
                            // Break (tokenize) the contents of a line of information into a String Array. 
                            string[] parts = line.Split(',');

                            // Create simple local variables to define the properties of the Graphics that get created.
                            double myX = double.Parse(parts[0]);
                            double myY = double.Parse(parts[1]);
                            double myZ = double.Parse(parts[2]);
                            double myPitch = double.Parse(parts[3]);
                            double myHeading = double.Parse(parts[4]);

                            // Create a new Graphic.
                            Esri.ArcGISRuntime.Layers.Graphic myGraphic = new Esri.ArcGISRuntime.Layers.Graphic();

                            // Create a new MapPoint using the data from the text file and set it to the Graphics geometry.
                            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = new Esri.ArcGISRuntime.Geometry.MapPoint(myX, myY, myZ, Esri.ArcGISRuntime.Geometry.SpatialReferences.Wgs84);
                            myGraphic.Geometry = myMapPoint;

                            // Get the symbology defined in XAML and apply it to the Graphic.
                            Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol myMarkerSymbol = (Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol)LayoutRoot.Resources["MyMarkerSymbol"];
                            myGraphic.Symbol = myMarkerSymbol;

                            // Add a couple of Attributes to the Graphic (from the text file) that contain information for Pitch and Heading. 
                            myGraphic.Attributes.Add("Pitch", myPitch);
                            myGraphic.Attributes.Add("Heading", myHeading);

                            // Add the Graphic to the GraphicLayer.
                            myGraphicsLayer.Graphics.Add(myGraphic);
                        }

                        // Break out of the StreamReader if we don't have valid data.
                    } while ( line != null);

                } // Close the Using logic flow.

                // Close the StreamReader.
                fileStream.Close();
            }

            // Enable the button so the user can now perform some animation/navigation.
            Button2.IsEnabled = true;
        }

        private async void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            // This function will perform the animation/navigation between the MapPoints in the GraphicsLayer. 

            // Obtain the GraphicsLayer by its ID from the LayerCollection of the Scene.
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphcicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)SceneView1.Scene.Layers["MyGraphicsLayer"];

            // Get the GraphicCollection from the GraphicsLayer.
            Esri.ArcGISRuntime.Layers.GraphicCollection myGraphicCollection = myGraphcicsLayer.Graphics;

            // Loop through each Graphic in the GraphicCollection.
            foreach (Esri.ArcGISRuntime.Layers.Graphic oneGraphic in myGraphicCollection)
            {
                // Get the MapPoint from the Graphic's Geometry.
                Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = (Esri.ArcGISRuntime.Geometry.MapPoint)oneGraphic.Geometry;

                // Create simple local variables that were properties of the Graphics in the GraphicLayer.
                double myX = myMapPoint.X;
                double myY = myMapPoint.Y;
                double myZ = myMapPoint.Z;
                double myPitch = (double)oneGraphic.Attributes["Pitch"];
                double myHeading = (double)oneGraphic.Attributes["Heading"];

                // Update the Camera's location.
                _MyCamera = _MyCamera.SetLocation(myMapPoint);

                // Update the Camera's pitch.
                _MyCamera = _MyCamera.SetPitch(myPitch);

                // Update the Camera's heading.
                _MyCamera = _MyCamera.SetHeading(myHeading);

                // Perform the navigation/animation for the new Camera settings. It takes one second to navigate between each Graphic.
                await SceneView1.SetViewAsync(_MyCamera, new System.TimeSpan(0, 0, 0, 1));
            }
        }
    }
}