Select to view content in your preferred language

VB.NET Display multiple Geosearch results

2954
12
Jump to solution
01-08-2013 06:46 AM
Corbinde_Bruin
Frequent Contributor
I've written a GeocodeAddress method which is a modified version of the SDK vb.net FindAddress sample. I'm using a local address locator and I can't understand how to make it return multiple results. It is only returning the first result.

If I use the same locator in ArcMap's native Find tool, it pulls up a bunch of records.

All help is appreciated,
Corbin de Bruin

 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
0 Kudos
12 Replies
MichaelVolz
Esteemed Contributor
Try going to this URL for more information.

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/00010000038r000000.ht...

I believe this is the method that Neil might be referring to.
0 Kudos
Corbinde_Bruin
Frequent Contributor
Neil,

You may be right. Following that thought track through the documentation I was led to this page:

http://127.0.0.1:47873/help/3-6196/?method=page&id=b57a4e69-207d-4aa2-bf93-4d5f834a44aa&product=vs&p...

I'll post again if I'm able to fully work this out.

Thank you for you help,
Corbin de Bruin

Edit:
Thank you Mike, we were both led to the same link. Mine is just through the 10.1 help.
0 Kudos
Corbinde_Bruin
Frequent Contributor
I have it working to suit my needs.
Here's my success with it so far and comments indicating my best guess at what's going on:

    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


Thank you everyone for your help,
Corbin de Bruin
0 Kudos