Buffer

Download Samples Repository

Description

This sample demonstrates use of the GeometryEngine to calculate a buffer. To use the sample, click a point on the map. The click point and a buffer of 5 miles around the point will be shown.

"Desktop" "Store" "Phone"

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.BufferSample"
             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:SimpleFillSymbol x:Key="BufferSymbol" Color="#660000FF" Style="Solid">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Blue" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True" MapViewTapped="MyMapView_MapViewTapped">
			<esri:Map InitialViewpoint="-10863036,3838021,-10744801,3887145,3857">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="graphicOverlay" />
			</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>
            <TextBlock x:Name="InstructionsTextBlock" Text="Click on the map to generate a 5-mile buffer." 
                       Width="250" TextWrapping="Wrap" FontSize="14" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample demonstrates use of the GeometryEngine to calculate a buffer. To use the sample, click a point on the map. The click point and a buffer of 5 miles around the point will be shown.
	/// </summary>
	/// <title>Buffer</title>
	/// <category>Geometry</category>
	public partial class BufferSample : UserControl
	{
		private const double MILES_TO_METERS = 1609.34;

		private PictureMarkerSymbol _pinSymbol;
		private SimpleFillSymbol _bufferSymbol;

		private GraphicsOverlay _graphicOverlay; 

		/// <summary>Construct Buffer sample control</summary>
		public BufferSample()
		{
			InitializeComponent();

			_graphicOverlay = MyMapView.GraphicsOverlays["graphicOverlay"];

			SetupSymbols();
		}

		private void MyMapView_MapViewTapped(object sender, MapViewInputEventArgs e)
		{
			try
			{
				_graphicOverlay.Graphics.Clear();

				// Convert screen point to map point
				var point = e.Location;
				var buffer = GeometryEngine.Buffer(point, 5 * MILES_TO_METERS);

				var bufferGraphic = new Graphic { Geometry = buffer, Symbol = _bufferSymbol };
				_graphicOverlay.Graphics.Add(bufferGraphic);

				var pointGraphic = new Graphic { Geometry = point, Symbol = _pinSymbol };
				_graphicOverlay.Graphics.Add(pointGraphic);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Geometry Engine Failed!");
			}
		}

		private async void SetupSymbols()
		{
			try
			{
				_pinSymbol = new PictureMarkerSymbol() { Width = 48, Height = 48, YOffset = 24 };
				await _pinSymbol.SetSourceAsync(
					new Uri("pack://application:,,,/ArcGISRuntimeSamplesDesktop;component/Assets/RedStickpin.png"));

				_bufferSymbol = layoutGrid.Resources["BufferSymbol"] as SimpleFillSymbol;
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error occured : " + ex.Message, "Buffer Sample");
			}
		}
	}
}
Feedback on this topic?