Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Move Points

Download Samples Repository

Description

Demonstrates one method of moving graphic points on the map.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.MovePoints"
             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">
        <Grid.Resources>
			<esri:SimpleMarkerSymbol x:Key="PointMarkerSymbol"
								Color="CornflowerBlue" Size="15"  Style="Circle" >
				<esri:SimpleMarkerSymbol.Outline>
					<esri:SimpleLineSymbol Color="White" Width="2" />
				</esri:SimpleMarkerSymbol.Outline>
			</esri:SimpleMarkerSymbol>
		</Grid.Resources>

		<esri:MapView x:Name="MyMapView"
					  MapViewTapped="MyMapView_MapViewTapped"
					  MouseMove="MyMapView_MouseMove">
			<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="MyGraphicsOverlay">
					<esri:GraphicsOverlay.Renderer>
						<esri:SimpleRenderer Symbol="{StaticResource PointMarkerSymbol}" />
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
				<esri:GraphicsOverlay ID="EditGraphicsOverlay" Opacity=".5"/>
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top" 
				Margin="30" Padding="20"
				MaxWidth="250">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>

			<TextBlock Text="Click a point to move it. Click again to commit the change." 
					   TextWrapping="Wrap" />
		</Border>
		
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates one method of moving graphic points on the map.
    /// </summary>
    /// <title>Move Points</title>
    /// <category>Editing</category>
    public partial class MovePoints : UserControl
    {
		private Graphic graphicBeingEdited;

        public MovePoints()
        {
			InitializeComponent();
			LoadData();
        }

		private void LoadData()
		{
			//Add some random points for editing
			Random r = new Random();
			var graphicsOverlay = MyMapView.GraphicsOverlays["MyGraphicsOverlay"];
			for (int i = 0; i < 20; i++)
			{
				graphicsOverlay.Graphics.Add(
					new Graphic(new MapPoint(r.NextDouble() * 360, r.NextDouble() * 180, SpatialReferences.Wgs84)));
			}
		}

		private async void MyMapView_MapViewTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
		{
			var graphicsOverlay = MyMapView.GraphicsOverlays["MyGraphicsOverlay"];
			var editGraphicsOverlay = MyMapView.GraphicsOverlays["EditGraphicsOverlay"];
			if (graphicBeingEdited == null)
			{
				var hit = await graphicsOverlay.HitTestAsync(sender as Esri.ArcGISRuntime.Controls.ViewBase, e.Position);
				if (hit != null)
				{
					graphicBeingEdited = hit;
					//highlight the active graphic
					graphicBeingEdited.IsSelected = true;
					//Create a temporary we can move around without 'disturbing' the original feature until commit
					Graphic g = new Graphic();
					g.Symbol = hit.Symbol ?? graphicsOverlay.Renderer.GetSymbol(hit);
					g.Geometry = hit.Geometry;
					editGraphicsOverlay.Graphics.Add(g);
				}
			}
			else //Commit and clean up
			{
				graphicBeingEdited.Geometry = e.Location;
				graphicBeingEdited.IsSelected = false;
				graphicBeingEdited = null;
				editGraphicsOverlay.Graphics.Clear();
			}
		}

		private void MyMapView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			if (graphicBeingEdited != null)
			{
				var editGraphicsOverlay = MyMapView.GraphicsOverlays["EditGraphicsOverlay"];
				MapView mapview = (MapView)sender;
				var location = mapview.ScreenToLocation(e.GetPosition(mapview));
				editGraphicsOverlay.Graphics[0].Geometry = location;
			}
		}
    }
}
Feedback on this topic?