Show Mouse Coordinates

Download Samples Repository

Description

This sample includes a Map and a single ArcGIS Server layer. MouseMove events on the Map are handled to return the mouse cursor location over the map. The location is displayed in pixels and map units.

"Desktop" "Store" Available for Desktop, Store

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ShowMouseCoordinates"
             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" MouseMove="MyMapView_MouseMove">
            <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" Width="355">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock x:Name="ScreenCoordsTextBlock" Foreground="Black"
                    HorizontalAlignment="Left" VerticalAlignment="Center" 
					Text="Screen Coords: " TextWrapping="Wrap" FontWeight="Bold" />
                <TextBlock x:Name="MapCoordsTextBlock" Foreground="Black"
                    HorizontalAlignment="Left" VerticalAlignment="Center" 
					Text="Map Coords: " TextWrapping="Wrap" FontWeight="Bold" />
            </StackPanel>
		</Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Controls;
using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample includes a Map and a single ArcGIS Server layer. MouseMove events on the Map are handled to return the mouse cursor location over the map. The location is displayed in pixels and map units.
	/// </summary>
	/// <title>Show Mouse Coordinates</title>
	/// <category>Mapping</category>
	public partial class ShowMouseCoordinates : UserControl
	{
		public ShowMouseCoordinates()
		{
			InitializeComponent();
		}

		private void MyMapView_MouseMove(object sender, MouseEventArgs e)
		{
			if (MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry) == null)
				return;

			System.Windows.Point screenPoint = e.GetPosition(MyMapView);
			ScreenCoordsTextBlock.Text = string.Format("Screen Coords: X = {0}, Y = {1}",
				screenPoint.X, screenPoint.Y);

			MapPoint mapPoint = MyMapView.ScreenToLocation(screenPoint);
			if (MyMapView.WrapAround)
				mapPoint = GeometryEngine.NormalizeCentralMeridian(mapPoint) as MapPoint;
			MapCoordsTextBlock.Text = string.Format("Map Coords: X = {0}, Y = {1}",
					Math.Round(mapPoint.X, 4), Math.Round(mapPoint.Y, 4));
		}
	}
}
Feedback on this topic?