Click or drag to resize
Code Example - MapView_SetView

Demonstrates using the various MapView.SetView Methods to adjust the extent of the map.

Code Example
Map View Set View

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_SetView.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 Orientation="Vertical">
            <StackPanel Orientation="Vertical">

                <!-- TextBlock to provide the instructions on how to use the sample code. It will be 
                populated with instructions in the code-behind when the application loads. -->
                <TextBlock Height="58" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <!-- 
                A MapView Control to display various GIS layers. Set the MapView.NavigationCompleted Event to notify when the 
                map has completed the changed extent; this will be used to display extent information back to the user. -->
                <esri:MapView x:Name="MapView1" Width="564" Height="480" VerticalAlignment="Top" Margin="2,2,2,2" 
                              NavigationCompleted="MapView1_NavigationCompleted" >

                    <!-- A Map. -->
                    <esri:Map  x:Name="Map1">

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

                    </esri:Map>
                </esri:MapView>

                <StackPanel Orientation="Vertical">

                    <!-- Add a series of buttons to change the map extent various ways. -->
                    <Button Content="SetView(Geometry)" 
                        x:Name="Button1" Click="Button1_Click" Width="225" HorizontalAlignment="Left"/>
                    <Button Content="SetView(MapPoint)" 
                        x:Name="Button2" Click="Button2_Click" Width="225" HorizontalAlignment="Left"/>
                    <Button Content="SetView(MapPoint, Double)" 
                        x:Name="Button3" Click="Button3_Click" Width="225" HorizontalAlignment="Left"/>
                    <Button Content="SetView(Viewpoint) v1" 
                        x:Name="Button4" Click="Button4_Click" Width="225" HorizontalAlignment="Left"/>
                    <Button Content="SetView(Viewpoint) v2" 
                        x:Name="Button5" Click="Button5_Click" Width="225" HorizontalAlignment="Left"/>

                    <!-- Display information about the current extent to the user. -->
                    <TextBlock Text="Scale: " Margin="5,20,0,0"/>
                    <TextBox x:Name="TextBox_Scale"/>
                    <TextBlock Text="Map Center: " Margin="5,20,0,0"/>
                    <TextBox x:Name="TextBox_MapCenter"/>
                    <TextBlock Text="Map Extent: " Margin="5,20,0,0"/>
                    <TextBox x:Name="TextBox_Extent1"/>
                    <TextBox x:Name="TextBox_Extent2"/>

                </StackPanel>

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

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

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application loads, click the various SetView buttons to see how the map extent/scale is changed for " +
                "various geographies. The current scale, map center and extent for each SetView Method will be displayed.";
        }

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // MapView.SetView(Geometry) version: Set the extent to Florida. 
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-9885355, 2832674, -8737373, 3689643);
            MapView1.SetView(myEnvelope);
        }

        private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // MapView.SetView(MapPoint) version: Center the extent to that of Arizona (given the current scale). 
            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = new Esri.ArcGISRuntime.Geometry.MapPoint(-12371459, 4075279);
            MapView1.SetView(myMapPoint);
        }

        private void Button3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // MapView.SetView(MapPoint, Double) version: Center the extent to that of France (given the scale of 1:1,3500,000). 
            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = new Esri.ArcGISRuntime.Geometry.MapPoint(341147, 5980251);
            double myScale = 13500000;
            MapView1.SetView(myMapPoint, myScale);
        }

        private void Button4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // MapView.SetView(Viewpoint) v1: Set the extent to Madagascar.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(4090634, -2994719, 6386598, -1280780);
            Esri.ArcGISRuntime.Controls.Viewpoint myViewPoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);
            MapView1.SetView(myViewPoint);
        }

        private void Button5_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // MapView.SetView(Viewpoint) v2: Center the extent to that of Tasmania (given the scale of 1:4,000,000).
            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = new Esri.ArcGISRuntime.Geometry.MapPoint(16319979, -5188315);
            double myScale = 4000000;
            Esri.ArcGISRuntime.Controls.Viewpoint myViewPoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myMapPoint, myScale);
            MapView1.SetView(myViewPoint);
        }

        private void MapView1_NavigationCompleted(object sender, System.EventArgs e)
        {
            // This sub-routine execute when the extent of the map has changed.

            // Get the various extent information.
            System.Int32 myCurrentScale = System.Convert.ToInt32(MapView1.Scale);
            Esri.ArcGISRuntime.Geometry.MapPoint myCurrentMapPoint = MapView1.Extent.GetCenter();
            System.Int32 myX = System.Convert.ToInt32(myCurrentMapPoint.X);
            System.Int32 myY = System.Convert.ToInt32(myCurrentMapPoint.Y);
            Esri.ArcGISRuntime.Geometry.Envelope myExtent = MapView1.Extent;
            System.Int32 myXMax = System.Convert.ToInt32(myExtent.XMax);
            System.Int32 myXMin = System.Convert.ToInt32(myExtent.XMin);
            System.Int32 myYMax = System.Convert.ToInt32(myExtent.YMax);
            System.Int32 myYMin = System.Convert.ToInt32(myExtent.YMin);

            // Display the results to the user.
            TextBox_Scale.Text = myCurrentScale.ToString();
            TextBox_MapCenter.Text = "X: " + myX.ToString() + " Y: " + myY.ToString();
            TextBox_Extent1.Text = "XMax: " + myXMax.ToString() + " XMin: " + myXMin.ToString();
            TextBox_Extent2.Text = "YMax: " + myYMax.ToString() + " YMin: " + myYMin.ToString();
        }

    }
}