Project

Download Samples Repository

Description

Sample shows how to project a coordinate from the current map projection (in this case Web Mercator) to a different projection.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ProjectCoordinate"
             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 x:Name="layoutGrid">
        <esri:MapView x:Name="MyMapView" WrapAround="True">
            <esri:Map>
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="graphicsOverlay">
					<esri:GraphicsOverlay.Renderer>
						<esri:SimpleRenderer>
							<esri:SimpleMarkerSymbol Color="Black" Style="Diamond" Size="12" />
						</esri:SimpleRenderer>
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				 HorizontalAlignment="Right" VerticalAlignment="Top"
				 Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Click the map to project a point."
                           FontSize="14" Width="200" TextWrapping="Wrap" />

                <Grid x:Name="gridXY" Margin="12" HorizontalAlignment="Center" Visibility="Collapsed">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding SpatialReference.Wkid, StringFormat='Source (WKID: {0})'}" 
                               Margin="4,8" HorizontalAlignment="Center" />
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="X:" Margin="6,2" HorizontalAlignment="Right" />
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding X, StringFormat={}{0:0.000}}" />
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Y:" Margin="6,2" HorizontalAlignment="Right" />
                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Y, StringFormat={}{0:0.000}}" />
                </Grid>

                <Grid x:Name="gridLatLon" HorizontalAlignment="Center" Visibility="Collapsed">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding SpatialReference.Wkid, StringFormat='Target (WKID: {0})'}" 
                               Margin="4,8" HorizontalAlignment="Center" />
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="X:" Margin="6,2" HorizontalAlignment="Right" />
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding X, StringFormat={}{0:0.000}}" />
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Y:" Margin="6,2" HorizontalAlignment="Right" />
                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Y, StringFormat={}{0:0.000}}" />
                </Grid>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Sample shows how to project a coordinate from the current map projection (in this case Web Mercator) to a different projection.
	/// </summary>
	/// <title>Project</title>
	/// <category>Geometry</category>
	public partial class ProjectCoordinate : UserControl
	{
		private GraphicsOverlay _graphicsOverlay;

		/// <summary>Construct Project sample control</summary>
		public ProjectCoordinate()
		{
			InitializeComponent();
		
			_graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"]; 
			MyMapView.ExtentChanged += MyMapView_ExtentChanged;
		}

		// Start map interaction
		private async void MyMapView_ExtentChanged(object sender, EventArgs e)
		{
			try
			{
				MyMapView.ExtentChanged -= MyMapView_ExtentChanged;
				await AcceptPointsAsync();
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error: " + ex.Message, "Project Sample");
			}
		}

		// Accept user map clicks and add points to the graphics overlay with the selected symbol
		// - collected point is in the coordinate system of the current map
		private async Task AcceptPointsAsync()
		{
			while (MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry.Extent != null)
			{
				var point = await MyMapView.Editor.RequestPointAsync();

				_graphicsOverlay.Graphics.Clear();
				_graphicsOverlay.Graphics.Add(new Graphic(point));

				// Take account of WrapAround
				var normalizedPt = GeometryEngine.NormalizeCentralMeridian(point) as MapPoint;

				// Convert from Web Mercator to WGS84
				var projectedPoint = GeometryEngine.Project(normalizedPt, SpatialReferences.Wgs84);

				gridXY.Visibility = gridLatLon.Visibility = Visibility.Visible;
				gridXY.DataContext = normalizedPt;
				gridLatLon.DataContext = projectedPoint;
			}
		}
	}
}
Feedback on this topic?