This sample demonstrates reverse-geocoding points using a LocatorTask. To use the sample, simply click on or near a street in the map. When a result is found, it will be drawn on the map with the returned address in the graphic's MapTip.
In the code-behind, a LocatorTask is used to perform an address lookup operation for the clicked point. When a result is returned, a graphic is added to the map, and the address is added to the graphic's attributes such that it shows up in the graphic's MapTip.
<UserControlx:Class="ArcGISWPFSDK.LocationToAddress"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/client/2009"><Gridx:Name="LayoutRoot"Background="White"><Grid.Resources><esri:PictureMarkerSymbolx:Key="DefaultMarkerSymbol"OffsetX="12"OffsetY="12"Source="/Assets/Images/x-24x24.png"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"Background="#FFE3E3E3"MouseClick="MyMap_MouseClick"WrapAround="True"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:GraphicsLayerID="LocationGraphicsLayer"><esri:GraphicsLayer.MapTip><GridBackground="LightYellow"><StackPanelOrientation="Vertical"Margin="5"><TextBlockText="{Binding [Address1]}"HorizontalAlignment="Left"Foreground="Black"/><TextBlockText="{Binding [Address2]}"HorizontalAlignment="Left"Foreground="Black"/><TextBlockText="{Binding [LatLon]}"HorizontalAlignment="Left"Foreground="Black"/></StackPanel><BorderBorderBrush="Black"BorderThickness="1"/></Grid></esri:GraphicsLayer.MapTip></esri:GraphicsLayer></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,15,15,0"Width="350"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#FFFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="10,10,10,15"/><TextBlockx:Name="InformationText"Text="Click on or near a street in the map to define a location. The address of the location will be displayed in a MapTip when the cursor hovers over the marker."HorizontalAlignment="Center"VerticalAlignment="Top"TextAlignment="Left"Margin="30,20,20,30"TextWrapping="Wrap"Foreground="Black"/></Grid></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass LocationToAddress : UserControl
{
privatestatic ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
GraphicsLayer _locationGraphicsLayer;
public LocationToAddress()
{
InitializeComponent();
ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
new ESRI.ArcGIS.Client.Geometry.Envelope(
_mercator.FromGeographic(
new ESRI.ArcGIS.Client.Geometry.MapPoint(-117.387, 33.97)) as MapPoint,
_mercator.FromGeographic(
new ESRI.ArcGIS.Client.Geometry.MapPoint(-117.355, 33.988)) as MapPoint);
initialExtent.SpatialReference = new SpatialReference(102100);
MyMap.Extent = initialExtent;
_locationGraphicsLayer = MyMap.Layers["LocationGraphicsLayer"] as GraphicsLayer;
}
privatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
Locator locatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US/GeocodeServer");
locatorTask.LocationToAddressCompleted += LocatorTask_LocationToAddressCompleted;
locatorTask.Failed += LocatorTask_Failed;
// Tolerance (distance) specified in metersdouble tolerance = 30;
locatorTask.LocationToAddressAsync(e.MapPoint, tolerance, e.MapPoint);
}
privatevoid LocatorTask_LocationToAddressCompleted(object sender, AddressEventArgs args)
{
Address address = args.Address;
Dictionary<string, object> attributes = address.Attributes;
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = args.UserState as MapPoint
};
string latlon = String.Format("{0}, {1}", address.Location.X, address.Location.Y);
string address1 = attributes["Street"].ToString();
string address2 = String.Format("{0}, {1} {2}", attributes["City"], attributes["State"], attributes["ZIP"]);
graphic.Attributes.Add("LatLon", latlon);
graphic.Attributes.Add("Address1", address1);
graphic.Attributes.Add("Address2", address2);
_locationGraphicsLayer.Graphics.Add(graphic);
}
privatevoid LocatorTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Unable to determine an address. Try selecting a location closer to a street.");
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass LocationToAddress
Inherits UserControl
PrivateShared _mercator AsNew ESRI.ArcGIS.Client.Projection.WebMercator()
Private _locationGraphicsLayer As GraphicsLayer
PublicSubNew()
InitializeComponent()
Dim initialExtent AsNew ESRI.ArcGIS.Client.Geometry.Envelope(TryCast(_mercator.FromGeographic(New ESRI.ArcGIS.Client.Geometry.MapPoint(-117.387, 33.97)), MapPoint), TryCast(_mercator.FromGeographic(New ESRI.ArcGIS.Client.Geometry.MapPoint(-117.355, 33.988)), MapPoint))
initialExtent.SpatialReference = New SpatialReference(102100)
MyMap.Extent = initialExtent
_locationGraphicsLayer = TryCast(MyMap.Layers("LocationGraphicsLayer"), GraphicsLayer)
EndSubPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim locatorTask AsNew Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US/GeocodeServer")
AddHandler locatorTask.LocationToAddressCompleted, AddressOf LocatorTask_LocationToAddressCompleted
AddHandler locatorTask.Failed, AddressOf LocatorTask_Failed
' Tolerance (distance) specified in metersDim tolerance AsDouble = 30
locatorTask.LocationToAddressAsync(e.MapPoint, tolerance, e.MapPoint)
EndSubPrivateSub LocatorTask_LocationToAddressCompleted(sender AsObject, args As AddressEventArgs)
Dim address As Address = args.Address
Dim attributes As Dictionary(Of String, Object) = address.Attributes
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = TryCast(args.UserState, MapPoint) _
}
Dim latlon AsString = [String].Format("{0}, {1}", address.Location.X, address.Location.Y)
Dim address1 AsString = attributes("Street").ToString()
Dim address2 AsString = [String].Format("{0}, {1} {2}", attributes("City"), attributes("State"), attributes("ZIP"))
graphic.Attributes.Add("LatLon", latlon)
graphic.Attributes.Add("Address1", address1)
graphic.Attributes.Add("Address2", address2)
_locationGraphicsLayer.Graphics.Add(graphic)
EndSubPrivateSub LocatorTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Unable to determine an address. Try selecting a location closer to a street.")
EndSubEndClassEndNamespace