About the Find an address Sample
[C#]
AddressForm.cs
using System; using System.Windows.Forms; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Location; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.Geometry; namespace FindAddress { public partial class AddressForm : Form { private AoInitialize m_license = null; public AddressForm() { GetLicense(); InitializeComponent(); ReturnLicence(); } void StateTextBox_KeyDown(object sender, KeyEventArgs e) { // pressing "enter" should do the same as clicking the button for locating if (e.KeyValue == 13) FindButton_Click(this, new System.EventArgs()); } void ZipTextBox_KeyDown(object sender, KeyEventArgs e) { // pressing "enter" should do the same as clicking the button for locating if (e.KeyValue == 13) FindButton_Click(this, new System.EventArgs()); } private void FindButton_Click(object sender, EventArgs e) { GeocodeAddress(); } private void GeocodeAddress() { // Get the locator System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager")); ILocatorManager2 locatorManager = obj as ILocatorManager2; ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(@"C:\California_fgdb.gdb"); ILocator locator = locatorWorkspace.GetLocator("California_city_state_zip_94_new"); // Set up the address properties IAddressInputs addressInputs = locator as IAddressInputs; IFields addressFields = addressInputs.AddressFields; IPropertySet addressProperties = new PropertySetClass(); addressProperties.SetProperty(addressFields.get_Field(0).Name, this.AddressTextBox.Text); addressProperties.SetProperty(addressFields.get_Field(1).Name, this.CityTextBox.Text); addressProperties.SetProperty(addressFields.get_Field(2).Name, this.StateTextBox.Text); addressProperties.SetProperty(addressFields.get_Field(3).Name, this.ZipTextBox.Text); // Match the Address IAddressGeocoding addressGeocoding = 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 GetLicense() { if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) throw new Exception("Could not set version. "); m_license = new AoInitializeClass(); m_license.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); } private void ReturnLicence() { m_license.Shutdown(); } } }
[Visual Basic .NET]
AddressForm.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 FindAddress Partial Public Class AddressForm Inherits Form Private m_license As AoInitialize = Nothing Public Sub New() GetLicense() InitializeComponent() ReturnLicence() End Sub Private Sub StateTextBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles StateTextBox.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 Private Sub ZipTextBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles ZipTextBox.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 Private Sub FindButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles FindButton.Click GeocodeAddress() End Sub Private Sub GeocodeAddress() ' 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("C:\locators") Dim locator As ILocator = locatorWorkspace.GetLocator("California_city_state_zip") ' Set up the address properties Dim addressInputs As IAddressInputs = TryCast(locator, IAddressInputs) Dim addressFields As IFields = addressInputs.AddressFields Dim addressProperties As IPropertySet = New PropertySetClass() addressProperties.SetProperty(addressFields.Field(0).Name, Me.AddressTextBox.Text) addressProperties.SetProperty(addressFields.Field(1).Name, Me.CityTextBox.Text) addressProperties.SetProperty(addressFields.Field(2).Name, Me.StateTextBox.Text) addressProperties.SetProperty(addressFields.Field(3).Name, Me.ZipTextBox.Text) ' Match the Address Dim addressGeocoding As IAddressGeocoding = TryCast(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 For i As Integer = 0 To length - 1 If namesArray(i) <> "Shape" Then Me.ResultsTextBox.Text += namesArray(i) & ": " & valuesArray(i).ToString() & Constants.vbLf Else If point IsNot 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 Next i Me.ResultsTextBox.Text += Constants.vbLf End Sub 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.esriLicenseProductCodeArcEditor) End Sub Private Sub ReturnLicence() m_license.Shutdown() End Sub End Class End Namespace