Graphics Source

Download Samples Repository

Description

This sample demonstrates the use of the GraphicsLayer.GraphicsSouce property. Here, three separate graphics source lists are initially created with random graphics. A button is used to switch the GraphicsSource property of the GraphicsLayer between the sources.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.GraphicsSourceSample"
             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">
			<esri:Map>
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
				<esri:GraphicsLayer ID="graphicsLayer" />
			</esri:Map>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <Button Content="Switch Graphics Source" Click="SwitchGraphicSourceButton_Click" />
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample demonstrates the use of the GraphicsLayer.GraphicsSouce property. Here, three separate graphics source lists are initially created with random graphics. A button is used to switch the GraphicsSource property of the GraphicsLayer between the sources.
	/// </summary>
	/// <title>Graphics Source</title>
	/// <category>Layers</category>
	/// <subcategory>Graphics Layers</subcategory>
	public partial class GraphicsSourceSample : UserControl
	{
		private Random _random = new Random();
		private GraphicsLayer _graphicsLayer;
		private List<List<Graphic>> _grahicsSources;
		private int _graphicSourceIndex;

		/// <summary>Construct Graphics Source sample control</summary>
		public GraphicsSourceSample()
		{
			InitializeComponent();

			_graphicsLayer = MyMapView.Map.Layers["graphicsLayer"] as GraphicsLayer;
			MyMapView.NavigationCompleted += MyMapView_NavigationCompleted;
		}

		private void MyMapView_NavigationCompleted(object sender, EventArgs e)
		{
			MyMapView.NavigationCompleted -= MyMapView_NavigationCompleted;
			CreateGraphics();
		}

		// Switch between pre-created graphics lists
		private void SwitchGraphicSourceButton_Click(object sender, RoutedEventArgs e)
		{
			++_graphicSourceIndex;
			if (_graphicSourceIndex == _grahicsSources.Count)
				_graphicSourceIndex = 0;

			_graphicsLayer.GraphicsSource = _grahicsSources[_graphicSourceIndex];
		}

		// Create three List<Graphic> objects with random graphics to serve as overlay GraphicsSources
		private void CreateGraphics()
		{
			_grahicsSources = new List<List<Graphic>>()
			{
				new List<Graphic>(),
				new List<Graphic>(),
				new List<Graphic>()
			};

			foreach (var graphicList in _grahicsSources)
			{
				for (int n = 0; n < 10; ++n)
				{
					graphicList.Add(CreateRandomGraphic());
				}
			}

			_graphicSourceIndex = 0;
			_graphicsLayer.GraphicsSource = _grahicsSources[_graphicSourceIndex];
		}

		// Create a random graphic
		private Graphic CreateRandomGraphic()
		{
			return new Graphic()
			{
				Geometry = GetRandomMapPoint(),
				Symbol = new SimpleMarkerSymbol() { Color = GetRandomColor(), Size = 15, Style = GetRandomMarkerStyle() }
			};
		}

		// Utility: Generate a random MapPoint within the current extent
		private MapPoint GetRandomMapPoint()
		{
			// Get current viewpoints extent from the MapView
            var currentViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
            var viewpointExtent = currentViewpoint.TargetGeometry.Extent;

			double x = viewpointExtent.XMin + (_random.NextDouble() * viewpointExtent.Width);
			double y = viewpointExtent.YMin + (_random.NextDouble() * viewpointExtent.Height);
			return new MapPoint(x, y, MyMapView.SpatialReference);
		}

		// Utility: Generate a random System.Windows.Media.Color
		private Color GetRandomColor()
		{
			var colorBytes = new byte[3];
			_random.NextBytes(colorBytes);
			return Color.FromRgb(colorBytes[0], colorBytes[1], colorBytes[2]);
		}

		// Utility: Generate a random marker style
		private SimpleMarkerStyle GetRandomMarkerStyle()
		{
			return (SimpleMarkerStyle)_random.Next(0, 6);
		}
	}
}
Feedback on this topic?