Show Map Extent

Download Samples Repository

Description

This sample includes a Map and a ArcGISTiledMapServiceLayer and demonstrates how to handle the ExtentChanged event on the MapView.The extent parameters of the map are displayed in a textblock as they change at runtime.Since the Map is in wrap-around mode, absolute values represent the map extent on an infinite continuous grid where values on the X axis increase or decrease as you pan.Normalized values represent the map extent in the real-world and take the dateline (or current central meridian) into account.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ShowMapExtent"
             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">
    <Grid>
        <esri:MapView x:Name="MyMapView" WrapAround="True" ExtentChanged="MapView_ExtentChanged">
            <esri:Map>
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
            </esri:Map>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20" IsHitTestVisible="False">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <StackPanel.Resources>
                    <Style TargetType="TextBlock">
                        <Setter Property="FontWeight" Value="Bold"/>
                        <Setter Property="FontSize" Value="11" />
                    </Style>
                </StackPanel.Resources>
                
                <TextBlock Foreground="Black">
                    <Run FontStyle="Italic">Absolute</Run>
                    <LineBreak />
                    <Run>MinX: </Run>
                    <Run Text="{Binding ElementName=MyMapView, Path=Extent.XMin, StringFormat=\{0:F3\}, Mode=OneWay}" />
                    <LineBreak />
                    <Run>MinY: </Run>
                    <Run Text="{Binding ElementName=MyMapView, Path=Extent.YMin, StringFormat=\{0:F3\}, Mode=OneWay}" />
                    <LineBreak />
                    <Run>MaxX: </Run>
                    <Run Text="{Binding ElementName=MyMapView, Path=Extent.XMax, StringFormat=\{0:F3\}, Mode=OneWay}" />
                    <LineBreak />
                    <Run>MaxY: </Run>
                    <Run Text="{Binding ElementName=MyMapView, Path=Extent.YMax, StringFormat=\{0:F3\}, Mode=OneWay}" />
               
                    <LineBreak />
                    <LineBreak />
                    <Run FontStyle="Italic">Normalized</Run>
                    <LineBreak />
                    <Run>Left X: </Run>
                    <Run x:Name="MinXNormalized" />
                    <LineBreak />
                    <Run>Bottom Y: </Run>
                    <Run x:Name="MinYNormalized"/>
                    <LineBreak />
                    <Run>Right X: </Run>
                    <Run x:Name="MaxXNormalized"/>
                    <LineBreak />
                    <Run>Top Y: </Run>
                    <Run x:Name="MaxYNormalized"/>
                </TextBlock>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Controls;
using System;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample includes a Map and a ArcGISTiledMapServiceLayer and demonstrates how to handle the ExtentChanged event on the MapView.  The extent parameters of the map are displayed in a textblock as they change at runtime.  Since the Map is in wrap-around mode, absolute values represent the map extent on an infinite continuous grid where values on the X axis increase or decrease as you pan.  Normalized values represent the map extent in the real-world and take the dateline (or current central meridian) into account.
	/// </summary>
	/// <title>Show Map Extent</title>
	/// <category>Mapping</category>
	public partial class ShowMapExtent : UserControl
	{
		public ShowMapExtent()
		{
			InitializeComponent();
		}

		private void MapView_ExtentChanged(object sender, EventArgs e)
		{
			Envelope newExtent = null;

			// Get current viewpoints extent from the MapView
			var currentViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
			var viewpointExtent = currentViewpoint.TargetGeometry.Extent;

			if (MyMapView.WrapAround)
			{
				Geometry normalizedExtent = GeometryEngine.NormalizeCentralMeridian(viewpointExtent);
				if (normalizedExtent is Polygon)
				{
					var normalizedPolygon = (Polygon)normalizedExtent;

					if (normalizedPolygon.Parts.Count == 1)
						newExtent = normalizedPolygon.Extent;
					else
					{
						var newExtentBuilder = new EnvelopeBuilder(MyMapView.SpatialReference);

						foreach (var p in normalizedPolygon.Parts[0].GetPoints())
						{
							if (p.X < newExtentBuilder.XMin || double.IsNaN(newExtentBuilder.XMin))
								newExtentBuilder.XMin = p.X;
							if (p.Y < newExtentBuilder.YMin || double.IsNaN(newExtentBuilder.YMin))
								newExtentBuilder.YMin = p.Y;
						}

						foreach (var p in normalizedPolygon.Parts[1].GetPoints())
						{
							if (p.X > newExtentBuilder.XMax || double.IsNaN(newExtentBuilder.XMax))
								newExtentBuilder.XMax = p.X;
							if (p.Y > newExtentBuilder.YMax || double.IsNaN(newExtentBuilder.YMax))
								newExtentBuilder.YMax = p.Y;
						}
						newExtent = newExtentBuilder.ToGeometry();
					}
				}
				else if (normalizedExtent is Envelope)
					newExtent = normalizedExtent as Envelope;
			}
			else
				newExtent = viewpointExtent;

			MinXNormalized.Text = newExtent.XMin.ToString("0.000");
			MinYNormalized.Text = newExtent.YMin.ToString("0.000");
			MaxXNormalized.Text = newExtent.XMax.ToString("0.000");
			MaxYNormalized.Text = newExtent.YMax.ToString("0.000");
		}
	}
}
Feedback on this topic?