Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Reverse Geocode

Download Samples Repository

Description

Demonstrates using the OnlineLocatorTask.ReverseGeocodeAsync method to get address information from a location on the map.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ReverseGeocode"
             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:SimpleRenderer x:Key="MarkerRenderer">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleMarkerSymbol Color="Red" Size="8" Style="Diamond"/>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>

            <ControlTemplate x:Key="MapTipTemplate">
                <ControlTemplate.Resources>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Left" />
                        <Setter Property="FontSize" Value="10" />
                        <Setter Property="FontWeight" Value="Normal" />
                        <Setter Property="Foreground" Value="White" />
                    </Style>
                </ControlTemplate.Resources>
                
                <Border Background="Black" BorderBrush="White" BorderThickness="2" Margin="4,0,0,4">
                    <StackPanel Margin="4">
                        <TextBlock Text="{Binding AddressFields[Address]}"/>

                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0}, {1} {2}">
                                    <Binding Path="AddressFields[City]" />
                                    <Binding Path="AddressFields[Region]" />
                                    <Binding Path="AddressFields[Postal]" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>

                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0:0.000}, {1:0.000}">
                                    <Binding Path="Location.X" />
                                    <Binding Path="Location.Y" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True" 
					  MapViewTapped="MyMapView_MapViewTapped">
			<esri:Map InitialViewpoint="-117.387,33.97,-117.355,33.988,4326">
				<esri:ArcGISTiledMapServiceLayer 
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay x:Name="graphicsOverlay" Renderer="{StaticResource MarkerRenderer}" />
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20"
				Width="350">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
            <StackPanel>
                <TextBlock Text="Click on or near a street in the map to define a location. The address of the location will be displayed in a map tip next to the graphic."
                           TextWrapping="Wrap" />
                <Button Content="Clear" Margin="48,24,48,0" Click="ClearButton_Click"/>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Tasks.Geocoding;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates using the OnlineLocatorTask.ReverseGeocodeAsync method to get address information from a location on the map.
    /// </summary>
    /// <title>Reverse Geocode</title>
	/// <category>Tasks</category>
	/// <subcategory>Geocoding</subcategory>
	public partial class ReverseGeocode : UserControl
    {
        private LocatorTask _locator;

        /// <summary>Construct reverse geocode sample control</summary>
        public ReverseGeocode()
        {
            InitializeComponent();
            _locator = new OnlineLocatorTask(new Uri("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
        }

        // Reverse geocode the clicked point and add a graphic and map tip to the map
        private async void MyMapView_MapViewTapped(object sender, MapViewInputEventArgs e)
        {
            try
            {
                graphicsOverlay.Graphics.Add(new Graphic(e.Location));

                var result = await _locator.ReverseGeocodeAsync(e.Location, 50, SpatialReferences.Wgs84, CancellationToken.None);

                var overlay = new ContentControl() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top };
                overlay.Template = layoutGrid.Resources["MapTipTemplate"] as ControlTemplate;
                overlay.DataContext = result;
                MapView.SetViewOverlayAnchor(overlay, e.Location);
                MyMapView.Overlays.Items.Add(overlay);
            }
            catch (AggregateException aex)
            {
                MessageBox.Show(aex.InnerExceptions[0].Message, "Reverse Geocode");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Reverse Geocode");
            }
        }

        // Clear current graphcis and overlay map tips
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            MyMapView.Overlays.Items.Clear();
			graphicsOverlay.Graphics.Clear();
        }
    }
}
Feedback on this topic?