About the Single line address geocoding Sample
[C#]
SingleLineGeocodingForm.cs
using System;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Location;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
namespace SingleLineGeocoding
{
public partial class SingleLineGeocodingForm : Form
{
private AoInitialize m_license = null;
private ILocator m_locator = null;
private String[] m_addressFields;
private String m_orgAddrLabel = "Address";
public SingleLineGeocodingForm()
{
GetLicense();
InitializeComponent();
ReturnLicence();
}
private void GeocodeAddress(IPropertySet addressProperties)
{
// Match the Address
IAddressGeocoding addressGeocoding = m_locator as IAddressGeocoding;
IPropertySet resultSet = addressGeocoding.MatchAddress(addressProperties);
// Print out the results
object names, values;
resultSet.GetAllProperties(out names, out values);
string[] namesArray = names as string[];
object[] valuesArray = values as object[];
int length = namesArray.Length;
IPoint point = null;
for (int i = 0; i < length; i++)
{
if (namesArray[i] != "Shape")
this.ResultsTextBox.Text += namesArray[i] + ": " + valuesArray[i].ToString() + "\n";
else
{
if (point != null && !point.IsEmpty)
{
point = valuesArray[i] as IPoint;
this.ResultsTextBox.Text += "X: " + point.X + "\n";
this.ResultsTextBox.Text += "Y: " + point.Y + "\n";
}
}
}
this.ResultsTextBox.Text += "\n";
}
private void locatorButton_Click(object sender, EventArgs e)
{
addressLabel.Text = m_orgAddrLabel;
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
String locatorPath = openFileDialog.FileName;
locatorPath = locatorPath.Substring(0, locatorPath.LastIndexOf('.'));
if (locatorPath != null && locatorPath != "")
{
locatorTextBox.Text = locatorPath;
addressTextBox.Enabled = true;
// Open the workspace
String workspaceName = locatorPath.Substring(0, locatorPath.LastIndexOf("\\"));
String locatorName = locatorPath.Substring(locatorPath.LastIndexOf("\\") + 1);
// Get the locator
System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
ILocatorManager2 locatorManager = obj as ILocatorManager2;
ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(workspaceName);
m_locator = locatorWorkspace.GetLocator(locatorName);
m_addressFields = get_AddressFields();
addressLabel.Text += " (" + String.Join(",", m_addressFields) + ")";
}
}
}
private void findButton_Click(object sender, EventArgs e)
{
String[] addressValues = addressTextBox.Text.Split(',');
if (addressValues.Length == m_addressFields.Length)
{
IPropertySet addressProperties = createAddressProperties(m_addressFields, addressValues);
GeocodeAddress(addressProperties);
}
else if(m_addressFields.Length == 1)
{
IPropertySet addressProperties = createAddressProperties(m_addressFields, addressValues);
GeocodeAddress(addressProperties);
}
else
{
MessageBox.Show("Your address needs a comma between each expected address field or just commas to delimit those fields ",
"Address Input Error");
}
}
void addressTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// pressing "enter" should do the same as clicking the button for locating
if (e.KeyValue == 13)
findButton_Click(this, new System.EventArgs());
}
/// <summary>
/// Get the address fields for the locator
/// </summary>
/// <param name="locator"></param>
/// <returns>A String array of address fields</returns>
private String[] get_AddressFields()
{
ISingleLineAddressInput singleLineInput = m_locator as ISingleLineAddressInput;
IAddressInputs addressInputs = null;
String[] fields;
if (singleLineInput != null)
{
IField singleField = singleLineInput.SingleLineAddressField;
fields = new String[] { singleField.Name };
}
else
{
addressInputs = m_locator as IAddressInputs;
IFields multiFields = addressInputs.AddressFields;
int fieldCount = multiFields.FieldCount;
fields = new String[fieldCount];
for (int i = 0; i < fieldCount; i++)
{
fields[i] = multiFields.get_Field(i).Name;
}
}
return fields;
}
/// <summary>
/// Create a propertySet of address fields and values
/// </summary>
/// <param name="addressFields"></param>
/// <param name="addressValues"></param>
/// <returns>A propertySet that contains address fields and address values that correspond to the fields.</returns>
private IPropertySet createAddressProperties(String[] addressFields, String[] addressValues)
{
int fieldCount = addressFields.Length;
if (fieldCount > 1 && fieldCount != addressValues.Length)
throw new Exception("There must be the same amount of address fields as address values. ");
IPropertySet propertySet = new PropertySetClass();
for (int i = 0; i < fieldCount; i++)
{
propertySet.SetProperty(addressFields[i], addressValues[i]);
}
return propertySet;
}
private void GetLicense()
{
if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
throw new Exception("Could not bind to license manager. ");
m_license = new AoInitializeClass();
m_license.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
}
private void ReturnLicence()
{
m_license.Shutdown();
}
}
}
[Visual Basic .NET]
SingleLineGeocodingForm.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Location
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Namespace SingleLineGeocoding
Public Partial Class SingleLineGeocodingForm : Inherits Form
Private m_license As AoInitialize = Nothing
Private m_locator As ILocator = Nothing
Private m_addressFields As String()
Private m_orgAddrLabel As String = "Address"
Public Sub New()
GetLicense()
InitializeComponent()
ReturnLicence()
End Sub
Private Sub GeocodeAddress(ByVal addressProperties As IPropertySet)
' Match the Address
Dim addressGeocoding As IAddressGeocoding = TryCast(m_locator, IAddressGeocoding)
Dim resultSet As IPropertySet = addressGeocoding.MatchAddress(addressProperties)
' Print out the results
Dim names, values As Object
resultSet.GetAllProperties(names, values)
Dim namesArray As String() = TryCast(names, String())
Dim valuesArray As Object() = TryCast(values, Object())
Dim length As Integer = namesArray.Length
Dim point As IPoint = Nothing
Dim i As Integer = 0
Do While i < length
If namesArray(i) <> "Shape" Then
Me.ResultsTextBox.Text += namesArray(i) & ": " & valuesArray(i).ToString() & Constants.vbLf
Else
If Not point Is Nothing AndAlso (Not point.IsEmpty) Then
point = TryCast(valuesArray(i), IPoint)
Me.ResultsTextBox.Text &= "X: " & point.X + Constants.vbLf
Me.ResultsTextBox.Text &= "Y: " & point.Y + Constants.vbLf
End If
End If
i += 1
Loop
Me.ResultsTextBox.Text += Constants.vbLf
End Sub
Private Sub locatorButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles locatorButton.Click
addressLabel.Text = m_orgAddrLabel
Dim result As DialogResult = openFileDialog.ShowDialog()
If result = System.Windows.Forms.DialogResult.OK Then
Dim locatorPath As String = openFileDialog.FileName
locatorPath = locatorPath.Substring(0, locatorPath.LastIndexOf("."c))
If Not locatorPath Is Nothing AndAlso locatorPath <> "" Then
locatorTextBox.Text = locatorPath
addressTextBox.Enabled = True
' Open the workspace
Dim workspaceName As String = locatorPath.Substring(0, locatorPath.LastIndexOf("\"))
Dim locatorName As String = locatorPath.Substring(locatorPath.LastIndexOf("\") + 1)
' Get the locator
Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
Dim locatorManager As ILocatorManager2 = TryCast(obj, ILocatorManager2)
Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(workspaceName)
m_locator = locatorWorkspace.GetLocator(locatorName)
m_addressFields = get_AddressFields()
addressLabel.Text &= " (" & String.Join(",", m_addressFields) & ")"
End If
End If
End Sub
Private Sub findButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles findButton.Click
Dim addressValues As String() = addressTextBox.Text.Split(","c)
If addressValues.Length = m_addressFields.Length Then
Dim addressProperties As IPropertySet = createAddressProperties(m_addressFields, addressValues)
GeocodeAddress(addressProperties)
ElseIf m_addressFields.Length = 1 Then
Dim addressProperties As IPropertySet = createAddressProperties(m_addressFields, addressValues)
GeocodeAddress(addressProperties)
Else
MessageBox.Show("Your address needs a comma between each expected address field or just commas to delimit those fields ", "Address Input Error")
End If
End Sub
Private Sub addressTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles addressTextBox.KeyDown
' pressing "enter" should do the same as clicking the button for locating
If e.KeyValue = 13 Then
findButton_Click(Me, New System.EventArgs())
End If
End Sub
''' <summary>
''' Get the address fields for the locator
''' </summary>
''' <param name="locator"></param>
''' <returns>A String array of address fields</returns>
Private Function get_AddressFields() As String()
Dim singleLineInput As ISingleLineAddressInput = TryCast(m_locator, ISingleLineAddressInput)
Dim addressInputs As IAddressInputs = Nothing
Dim fields As String()
If Not singleLineInput Is Nothing Then
Dim singleField As IField = singleLineInput.SingleLineAddressField
fields = New String() {singleField.Name}
Else
addressInputs = TryCast(m_locator, IAddressInputs)
Dim multiFields As IFields = addressInputs.AddressFields
Dim fieldCount As Integer = multiFields.FieldCount
fields = New String(fieldCount - 1) {}
Dim i As Integer = 0
Do While i < fieldCount
fields(i) = multiFields.Field(i).Name()
i += 1
Loop
End If
Return fields
End Function
''' <summary>
''' Create a propertySet of address fields and values
''' </summary>
''' <param name="addressFields"></param>
''' <param name="addressValues"></param>
''' <returns>A propertySet that contains address fields and address values that correspond to the fields.</returns>
Private Function createAddressProperties(ByVal addressFields As String(), ByVal addressValues As String()) As IPropertySet
Dim fieldCount As Integer = addressFields.Length
If fieldCount > 1 AndAlso fieldCount <> addressValues.Length Then
Throw New Exception("There must be the same amount of address fields as address values. ")
End If
Dim propertySet As IPropertySet = New PropertySetClass()
Dim i As Integer = 0
Do While i < fieldCount
propertySet.SetProperty(addressFields(i), addressValues(i))
i += 1
Loop
Return propertySet
End Function
Private Sub GetLicense()
If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) Then
Throw New Exception("Could not set version. ")
End If
m_license = New AoInitializeClass()
m_license.Initialize(esriLicenseProductCode.esriLicenseProductCodeStandard)
End Sub
Private Sub ReturnLicence()
m_license.Shutdown()
End Sub
End Class
End Namespace