Private Sub GeocodeAddress() locatorManager = TryCast(obj, ILocatorManager2) locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(gLocatorWorkspace) Dim locator As ILocator = locatorWorkspace.GetLocator("Street_Addresses_US") ' 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, txtAddress.Text) addressProperties.SetProperty(addressFields.Field(1).Name, txtCity.Text) addressProperties.SetProperty(addressFields.Field(2).Name, txtState.Text) addressProperties.SetProperty(addressFields.Field(3).Name, txtZIP.Text) ' Match the Address Dim addressGeocoding As IAddressGeocoding = TryCast(locator, IAddressGeocoding) Dim resultSet As IPropertySet = addressGeocoding.MatchAddress(addressProperties) 'Write results to DataGridView Dim names, values As Object ' Not sure how these ever really get populated. They produce a warning where a null reference exception could be produced resultSet.GetAllProperties(names, values) Dim namesArray() As String = TryCast(names, String()) Dim valuesArray() As Object = TryCast(values, Object()) Dim length As Integer = namesArray.Length ' Not terribly sure what this line does. Dim addressPoint As IPoint = Nothing dgvAddrResults.Rows.Add() ' Create DataGridRow to hold result For i As Integer = 0 To length - 1 Select Case namesArray(i) ' Case Statement to write proper fields to DataGrid Columns Case "Status" If valuesArray(i).ToString() = "U" Then lblAddressPrompt.Text = "***Address was not found." lblAddressPrompt.Visible = True Exit Sub End If Case "Shape" addressPoint = TryCast(valuesArray(i), IPoint) If addressPoint IsNot Nothing AndAlso (Not addressPoint.IsEmpty) Then dgvAddrResults.Item("Coordinates", 0).Value = addressPoint.X.ToString & ", " & addressPoint.Y.ToString Else dgvAddrResults.Item("Coordinates", 0).Value = "No geographic point available" End If Case "Score" dgvAddrResults.Item("Score", 0).Value = valuesArray(i).ToString() Case "Match_addr" dgvAddrResults.Item("Address", 0).Value = valuesArray(i).ToString() Case "Addr_type" dgvAddrResults.Item("Type", 0).Value = valuesArray(i).ToString() End Select Next i End Sub
Solved! Go to Solution.
Sub GeosearchAddress()
' Uses IAddressCandidates interface to geosearch locator for addresses. Returns multiple results
locatorManager = TryCast(obj, ILocatorManager2)
locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(gLocatorWorkspace)
Dim locator As ILocator = locatorWorkspace.GetLocator("Street_Addresses_US") ' Dimension the relative name of the locator
Dim addressCandidates As IAddressCandidates2 = locator ' Like an Array of Addresses
Dim addressPoint As IPoint = New Point
' Find the address candidates
Dim addressProperties As IPropertySet2 = New PropertySetClass()
With addressProperties ' Setting properties from textboxes
.SetProperty("Street", txtAddress.Text)
.SetProperty("City", txtCity.Text)
.SetProperty("State", txtState.Text)
.SetProperty("Zip", txtZIP.Text)
End With
' Use the FindAddressCandidates method find candidates for an address
Dim resultsArray As IArray = addressCandidates.FindAddressCandidates(addressProperties) ' Create array based on addressCandidates array filtered by addressProperties array
' Use the CandidateFields property to display the properties of each candidate
Dim candidateFields As IFields = addressCandidates.CandidateFields ' Like an Array of fields based on addessProperties and addressCandidates
Dim candidatePropertySet As IPropertySet2
Dim addressField As IField
Dim addressFieldVal As Object ' Object to hold field data. Created as object to be able to hold geographic field data. ToString method used to see text fields
For candidateIndex As Integer = 0 To 10 ' Loop through 10 geosearch results
candidatePropertySet = resultsArray.Element(candidateIndex)
dgvAddrResults.Rows.Add() ' Create DataGridRow to hold result
For fieldIndex As Integer = 0 To candidateFields.FieldCount - 1 ' Loop through all fields/properties of geosearch address candidates
addressField = candidateFields.Field(fieldIndex)
addressFieldVal = candidatePropertySet.GetProperty(addressField.Name) ' Variable set to hold field objects
Select Case addressField.Name ' Case Statement to write fields to correct data grid columns
Case "Shape"
Try
addressPoint = CType(addressFieldVal, IPoint)
Catch ex As Exception
Debug.Print("Failed to convert to ESRI Point object")
End Try
If addressPoint IsNot Nothing AndAlso (Not addressPoint.IsEmpty) Then
dgvAddrResults.Item("Coordinates", candidateIndex).Value = addressPoint.X.ToString & ", " & addressPoint.Y.ToString
Else
dgvAddrResults.Item("Coordinates", candidateIndex).Value = "No geographic point available"
End If
Case "Score"
dgvAddrResults.Item("Score", candidateIndex).Value = addressFieldVal.ToString()
Case "Match_addr"
dgvAddrResults.Item("Address", candidateIndex).Value = addressFieldVal.ToString()
Case "Addr_type"
dgvAddrResults.Item("Type", candidateIndex).Value = addressFieldVal.ToString()
End Select
Next
Next
End Sub