Click or drag to resize
Code Example - MapView_SpatialReference

Demonstrates reseting the MapView.SpatialReference by setting it to a new Map.

Code Example
Map View Spatial Reference

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="MapView_SpatialReference.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="800"
        xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">

    <Grid>

        <StackPanel>

            <!-- Add a ListBox that will hold Uri strings to various ArcGISDynamicMapServiceLayers.-->
            <ListBox x:Name="ListBox1" Height="75"/>


            <StackPanel Orientation="Horizontal">

                <!-- Add a button to add the ArcGISDynamicMapServiceLayer to the Map. -->
                <Button x:Name="Button1" Content="Add the Layer to the Map" Click="Button1_Click" Margin="0,0,3,3" Width="390"/>

                <!-- Add a button to obtain the SpatialReference and Extent of the MapView. -->
                <Button x:Name="Button2" Content="What's the Spatial Reference/Extent?" Click="Button2_Click"  Margin="0,0,3,3" Width="390"/>

            </StackPanel>

            <!-- Add a default MapView and Map to the UI. -->
            <esri:MapView x:Name="MapView1" Height="474">
                <esri:Map x:Name="Map1"/>
            </esri:MapView>

        </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 "MapView_SpatialReference.MainWindow" to be just "MainWindow".

// USAGE INSTRUCTIONS:
// Typically the MapView.SpatialReference is set based upon the layers that are added to the Map.
// This example code shows how to reset the MapView.SpatialReference. 
// 
// STEP1:
// When the application loads, select the 1st ServiceUri in the Listbox and click the button 'Add Layer to the Map'.
// Then click the button 'What's the Spatial Reference/Extent?' 
// The SpatialReference WKID = 4326.
// 
// STEP2:
// Then repeat selecting the 2nd ServiceUri in the Listbox and click the button 'Add Layer to the Map'.
// You should get an MessageBox that says "This is a bad layer.".
// Then click the button 'What's the Spatial Reference/Extent?'
// The SpatialReference is Nothing/null. The MessageBox should say: "No SpatialReference Set"
// 
// STEP3:
// Then repeat selecting the 3nd ServiceUri in the Listbox and click the button 'Add Layer to the Map'.
// Then click the button 'What's the Spatial Reference/Extent?'
// The SpatialReference WKID = 102100.

namespace MapView_SpatialReference
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add various ServiceUri strings into the ListBox. Some are good and others do not exist.
            // ---------------------------------------------------------------------------------------

            // This is a good web service. WKID=4326
            ListBox1.Items.Add("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer");

            // This web service does not exist
            ListBox1.Items.Add("http://sampleserver1.arcgisonline.com/arcgis/rest/services/Specialty/BOGUS_SERVICE/MapServer");

            // This is a good web service. WKID=102100
            ListBox1.Items.Add("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
        }

           private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Create a new Map and set it to the MapView.Map Property. This will reset the MapView.SpatialReference to Nothing/null.
            Esri.ArcGISRuntime.Controls.Map myMap = new Esri.ArcGISRuntime.Controls.Map();
            MapView1.Map = myMap;

            // Create a string to hold the Uri a an ArcGISDynamicMapServiceLayer.
            string theArcGISDynamicMapServiceLayerName = null;

            try
            {
                // Get the Uri string from the users ListBox choice.
                theArcGISDynamicMapServiceLayerName = ListBox1.SelectedItem.ToString();
            }
            catch (System.Exception)
            {
                // The user did not select a Uri string before clicking the button. Let them know what they need to do.
                System.Windows.MessageBox.Show("You must select a ServiceUri string in the listbox before clicking the button.");
                return;
            }

            // Create a new ArcGISDynamicMapServiceLayer based upon the user selected Uri string.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();
            myArcGISDynamicMapServiceLayer.ServiceUri = theArcGISDynamicMapServiceLayerName;

            try
            {
                // Initialize the ArcGISDynamicMapServiceLayer.
                await myArcGISDynamicMapServiceLayer.InitializeAsync();

                // Add the ArcGISDynamicMapServiceLayer to the Map's LayerCollection.
                // This will cause the MapView.SpatialReference to take on the SpatialReference of the layer just added.
                MapView1.Map.Layers.Add(myArcGISDynamicMapServiceLayer);
            }
            catch (System.Exception)
            {
                // We had an error for the 2nd ServiceUri in the ListBox. This is expected.
                System.Windows.MessageBox.Show("This is a bad layer.");
            }

        }

        private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the MapView's SpatialReference.
            Esri.ArcGISRuntime.Geometry.SpatialReference theSpatialReference = MapView1.SpatialReference;

            if (theSpatialReference != null)
            {
                // Get the Extent Envelope of the MapView.
                Esri.ArcGISRuntime.Geometry.Envelope theEnvelopeExtent = MapView1.Extent;

                // Display to the user the MapView.SpatialReference Wkid and the Extent coordinates.
                System.Windows.MessageBox.Show(theEnvelopeExtent.ToString(), theSpatialReference.Wkid.ToString());
            }
            else
            {
                // There was not MapView.SpatialReference. This is because the user chose the bogus (2nd service).
                System.Windows.MessageBox.Show("No SpatialReference Set", "No SpatialReference Set");
            }
        }


    }
}