This sample demonstrates using the ArcGIS Runtime SDK for WPF to geocode addresses by using a LocalGeocodeService
and a Locator. To use the sample, specify an address in the dialog in the upper right of the application and click the Find button.
In the code-behind, a LocalGeocodeService is started and the Url is used to create a Locator to geocode the specified address. The results are then used to create Graphics and added to the map.
The
supported source for a LocalGeocodeService is an ArcGIS Locator Package (. gcpk
), which can be authored in ArcGIS for Desktop.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.LocalGeocode "
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 "
xmlns:d = " http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 " >
< Grid x:Name = " LayoutRoot " >
< Grid.Resources >
< BooleanToVisibilityConverter x:Key = " BooleanToVisibilityConverter " />
< LinearGradientBrush x:Key = " PanelGradient " EndPoint = " 0.5,1 " StartPoint = " 0.5,0 " >
< LinearGradientBrush.RelativeTransform >
< TransformGroup >
< ScaleTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< SkewTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< RotateTransform Angle = " 176 " CenterY = " 0.5 " CenterX = " 0.5 " />
< TranslateTransform />
</ TransformGroup >
</ LinearGradientBrush.RelativeTransform >
< GradientStop Color = " #FF145787 " Offset = " 0.16 " />
< GradientStop Color = " #FF3D7FAC " Offset = " 0.502 " />
< GradientStop Color = " #FF88C5EF " Offset = " 0.984 " />
</ LinearGradientBrush >
< esri : MarkerSymbol x:Key = " StrobeMarkerSymbol " >
< esri : MarkerSymbol.ControlTemplate >
< ControlTemplate >
< Canvas >
< VisualStateManager.VisualStateGroups >
< VisualStateGroup x:Name = " CommonStates " >
< VisualState x:Name = " Normal " >
< Storyboard RepeatBehavior = " Forever " >
< DoubleAnimation BeginTime = " 0 "
Storyboard.TargetName = " ellipse " Storyboard.TargetProperty = " (UIElement.RenderTransform).(ScaleTransform.ScaleX) "
From = " 1 " To = " 10 " Duration = " 00:00:02 " />
< DoubleAnimation BeginTime = " 0 "
Storyboard.TargetName = " ellipse " Storyboard.TargetProperty = " (UIElement.RenderTransform).(ScaleTransform.ScaleY) "
From = " 1 " To = " 10 " Duration = " 00:00:02 " />
< DoubleAnimation BeginTime = " 0 "
Storyboard.TargetName = " ellipse " Storyboard.TargetProperty = " (UIElement.Opacity) "
From = " 1 " To = " 0 " Duration = " 00:00:01 " />
</ Storyboard >
</ VisualState >
</ VisualStateGroup >
</ VisualStateManager.VisualStateGroups >
< Ellipse Height = " 10 " Width = " 10 " Canvas.Left = " -5 " Canvas.Top = " -5 "
RenderTransformOrigin = " 0.5,0.5 " x:Name = " ellipse "
IsHitTestVisible = " False " >
< Ellipse.RenderTransform >
< ScaleTransform />
</ Ellipse.RenderTransform >
< Ellipse.Fill >
< RadialGradientBrush >
< GradientStop Color = " #00FF0000 " />
< GradientStop Color = " #FFFF0000 " Offset = " 0.25 " />
< GradientStop Color = " #00FF0000 " Offset = " 0.5 " />
< GradientStop Color = " #FFFF0000 " Offset = " 0.75 " />
< GradientStop Color = " #00FF0000 " Offset = " 1 " />
</ RadialGradientBrush >
</ Ellipse.Fill >
</ Ellipse >
< Ellipse Height = " 10 " Width = " 10 " Canvas.Left = " -5 " Canvas.Top = " -5 "
Fill = " #FFFF0000 " x:Name = " ellipse1 " />
</ Canvas >
</ ControlTemplate >
</ esri : MarkerSymbol.ControlTemplate >
</ esri : MarkerSymbol >
</ Grid.Resources >
< esri : Map x:Name = " MyMap " Background = " #FFE3E3E3 " Extent = " -122.519985221, 37.836494682 ,-122.302264539 , 37.698485944 " >
< esri : ArcGISLocalTiledLayer Path = " ..\Data\TPKS\SanFrancisco.tpk " />
< esri : GraphicsLayer ID = " MyGraphicsLayer " />
</ esri : Map >
< StackPanel Orientation = " Vertical " Margin = " 10 " HorizontalAlignment = " Right " VerticalAlignment = " Top " >
< Grid x:Name = " GeocodeGrid " HorizontalAlignment = " Right " VerticalAlignment = " Top " >
< Rectangle Fill = " {StaticResource PanelGradient} " Stroke = " Gray " RadiusX = " 10 " RadiusY = " 10 " Margin = " -5,-5,-5,0 " >
< Rectangle.Effect >
< DropShadowEffect />
</ Rectangle.Effect >
</ Rectangle >
< Grid MinWidth = " 500 " >
< Grid.ColumnDefinitions >
< ColumnDefinition Width = " Auto " />
< ColumnDefinition Width = " Auto " />
< ColumnDefinition Width = " Auto " />
< ColumnDefinition Width = " 115* " />
</ Grid.ColumnDefinitions >
< TextBlock Text = " Search for Address: " Foreground = " White " Grid.Column = " 0 "
HorizontalAlignment = " Center " Height = " 24 " VerticalAlignment = " Center "
FontWeight = " Bold " FontSize = " 12 " Margin = " 10,10,5,0 " />
< ComboBox x:Name = " FindText " IsEditable = " True " Background = " White " Width = " 300 " HorizontalContentAlignment = " Left " Grid.Column = " 1 " Margin = " 0,4,0,8 " />
< Button x:Name = " ExecuteButton " Content = " Find " Width = " 75 " Click = " ExecuteButton_Click " IsEnabled = " False "
Margin = " 8,4,2,6.622 " Cursor = " Hand " Grid.Column = " 3 " />
</ Grid >
</ Grid >
</ StackPanel >
< ProgressBar x:Name = " MyProgressBar " IsIndeterminate = " True " VerticalAlignment = " Bottom " Width = " 200 " Height = " 20 " Margin = " 10 " Visibility = " {Binding Path=IsBusy, Converter={StaticResource BooleanToVisibilityConverter}} " > </ ProgressBar >
</ Grid >
</ UserControl >
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client.Tasks;
using System.ComponentModel;
namespace ArcGISWPFSDK
{
public partial class LocalGeocode : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isBusy = true ;
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
_isBusy = value;
if (PropertyChanged != null )
{
PropertyChanged(this , new PropertyChangedEventArgs("IsBusy" ));
}
}
}
LocalGeocodeService _localGeocodeService;
GraphicsLayer _locationGraphicsLayer;
public LocalGeocode()
{
InitializeComponent();
FindText.Text = "1455 MARKET ST, SAN FRANCISCO, CA 94103" ;
FindText.Items.Add("2011 MISSION ST, SAN FRANCISCO CA 94110" );
FindText.Items.Add("820 BRYANT ST, SAN FRANCISCO CA 94103" );
FindText.SelectedIndex = 0;
_locationGraphicsLayer = MyMap.Layers["MyGraphicsLayer" ] as GraphicsLayer;
LocalGeocodeService.GetServiceAsync("..\\Data\\Locators\\SanFrancisco\\SanFranciscoLocator.gcpk" , "SanFranciscoLocator" , (gs) =>
{
if (gs.Error != null )
{
MessageBox.Show(gs.Error.Message);
}
if (gs.Status == LocalServiceStatus.Running)
{
_localGeocodeService = gs;
ExecuteButton.IsEnabled = true ;
DataContext = this ;
IsBusy = false ;
}
});
}
private void ExecuteButton_Click(object sender, RoutedEventArgs e)
{
IsBusy = true ;
var searchText = FindText.Text;
Locator locatorTask = new Locator(_localGeocodeService.UrlGeocodeService);
locatorTask.AddressToLocationsCompleted += (object sender1, AddressToLocationsEventArgs args) =>
{
if (args.Results.Count > 0)
{
AddressCandidate bestCandidate = args.Results[0];
foreach (AddressCandidate candidate in args.Results)
bestCandidate = (candidate.Score > bestCandidate.Score) ? candidate : bestCandidate;
MapPoint geographicPoint = new MapPoint(bestCandidate.Location.X, bestCandidate.Location.Y, new SpatialReference(4326));
Graphic graphic = new Graphic()
{
Geometry = geographicPoint,
Symbol = LayoutRoot.Resources["StrobeMarkerSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol
};
_locationGraphicsLayer.Graphics.Clear();
_locationGraphicsLayer.Graphics.Add(graphic);
IsBusy = false ;
}
else
{
MessageBox.Show("No results found" );
IsBusy = false ;
}
};
locatorTask.Failed += (s2, e2) =>
{
MessageBox.Show("Geocode failed with error: " + e2.Error.Message);
};
AddressToLocationsParameters addressParameters = new AddressToLocationsParameters();
Dictionary<string , string > address = addressParameters.Address;
address.Add("Single Line Input" , searchText);
locatorTask.AddressToLocationsAsync(addressParameters);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Local
Imports ESRI.ArcGIS.Client.Tasks
Imports System.ComponentModel
Namespace ArcGISWPFSDK
Partial Public Class LocalGeocode
Inherits UserControl
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _isBusy As Boolean = True
Public Property IsBusy() As Boolean
Get
Return _isBusy
End Get
Set (value As Boolean )
_isBusy = value
RaiseEvent PropertyChanged(Me , New PropertyChangedEventArgs("IsBusy" ))
End Set
End Property
Private _localGeocodeService As LocalGeocodeService
Private _locationGraphicsLayer As GraphicsLayer
Public Sub New ()
InitializeComponent()
FindText.Text = "1455 MARKET ST, SAN FRANCISCO, CA 94103"
FindText.Items.Add("2011 MISSION ST, SAN FRANCISCO CA 94110" )
FindText.Items.Add("820 BRYANT ST, SAN FRANCISCO CA 94103" )
FindText.SelectedIndex = 0
_locationGraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer" ), GraphicsLayer)
LocalGeocodeService.GetServiceAsync("..\Data\Locators\SanFrancisco\SanFranciscoLocator.gcpk" , "SanFranciscoLocator" , Function (gs)
If gs.[Error ] IsNot Nothing Then
MessageBox.Show(gs.[Error ].Message)
End If
If gs.Status = LocalServiceStatus.Running Then
_localGeocodeService = gs
ExecuteButton.IsEnabled = True
DataContext = Me
IsBusy = False
End If
End Function )
End Sub
Private Sub ExecuteButton_Click(sender As Object , e As RoutedEventArgs)
IsBusy = True
Dim searchText = FindText.Text
Dim locatorTask As New Locator(_localGeocodeService.UrlGeocodeService)
AddHandler locatorTask.AddressToLocationsCompleted, AddressOf LocatorTask_AddressToLocationsCompleted
AddHandler locatorTask.Failed, AddressOf LocatorTask_Failed
Dim addressParameters As New AddressToLocationsParameters()
Dim address As Dictionary(Of String , String ) = addressParameters.Address
address.Add("Single Line Input" , searchText)
locatorTask.AddressToLocationsAsync(addressParameters)
End Sub
Private Sub LocatorTask_AddressToLocationsCompleted(sender As Object , args As AddressToLocationsEventArgs)
If args.Results.Count > 0 Then
Dim bestCandidate As AddressCandidate = args.Results(0)
For Each candidate As AddressCandidate In args.Results
bestCandidate = If ((candidate.Score > bestCandidate.Score), candidate, bestCandidate)
Next
Dim geographicPoint As New MapPoint(bestCandidate.Location.X, bestCandidate.Location.Y, New SpatialReference(4326))
Dim graphic As New Graphic() With { _
.Geometry = geographicPoint, _
.Symbol = TryCast(LayoutRoot.Resources("StrobeMarkerSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol) _
}
_locationGraphicsLayer.Graphics.Clear()
_locationGraphicsLayer.Graphics.Add(graphic)
IsBusy = False
Else
MessageBox.Show("No results found" )
End If
End Sub
Private Sub LocatorTask_Failed(sender As Object , e As TaskFailedEventArgs)
MessageBox.Show("Geocode failed with error: " & e.[Error ].Message)
End Sub
End Class
End Namespace
5/16/2014