This document is archived and information here might be outdated.  Recommended version.


How to geocode a single address (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Spatial query and analysis > Geocoding > Finding locations > How to geocode a single address (ArcObjects .NET 10.6 SDK)

How to geocode a single address


In this topic


Geocoding a single address

This topic discusses the following two ways to geocode a single address:
  • Using IAddressGeocoding and MatchAddress
  • Using a GeocodeServer

Using IAddressGeocoding and MatchAddress

The following is the primary way to geocode a single address:
  1. Once you have the locator, set the address parameters, then call MatchAddress. The address parameter is a PropertySet containing properties that represent the input address components used by the locator. Use the AddressFields property on the IAddressInputs interface to determine the input address components used by the locator.
  2. The MatchFields property of the address locator contains the coordinates of the points that match the address that was passed in. The MatchFields property returns a Field collections where the names of the Field objects correspond to the names of the properties in the PropertySet returned by the MatchAddress method.

    See the following code example:
[C#]
public void geocodeSingleAddress()
{
    // Open the file geodatabase.
    System.Object obj=Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriDataSourcesGDB.FileGDBWorkspaceFactory"));
    IWorkspaceFactory2 workspaceFactory=obj as IWorkspaceFactory2;
    IWorkspace workspace=workspaceFactory.OpenFromFile(@"C:\UnitedStates.gdb", 0);

    // Get the locator workspace.
    obj=Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriLocation.LocatorManager"));
    ILocatorManager2 locatorManager=obj as ILocatorManager2;
    ILocatorWorkspace locatorWorkspace=locatorManager.GetLocatorWorkspace
        (workspace);

    // Get the locator for the locator workspace.
    IAddressGeocoding addressGeocoding=(IAddressGeocoding)
        locatorWorkspace.GetLocator("USLocator");

    // Geocode the address using the locator
    IPropertySet addressProperties=new PropertySetClass();
    addressProperties.SetProperty("Street", "380 New York St");
    addressProperties.SetProperty("City", "Redland");
    addressProperties.SetProperty("State", "CA");
    addressProperties.SetProperty("ZIP", "92377");
    IPropertySet matchProperties=addressGeocoding.MatchAddress(addressProperties);

    // Print the match properties.
    IFields matchFields=addressGeocoding.MatchFields;
    IField matchField=new FieldClass();
    for (int i=0; i < matchFields.FieldCount; i++)
    {
        matchField=matchFields.get_Field(i);
        if (matchField.Type == esriFieldType.esriFieldTypeGeometry)
        {
            IPoint point=(IPoint)matchProperties.GetProperty(matchField.Name);
            if (!point.IsEmpty)
            {
                Console.WriteLine("X: " + point.X);
                Console.WriteLine("Y: " + point.Y);
            }
        }
        else
        {
            Console.WriteLine(matchField.AliasName + ": " +
                matchProperties.GetProperty(matchField.Name));
        }
    }
}
[VB.NET]
Sub AddressGeocoding()
    ' Open a file geodatabase.
    Dim obj As System.Object=Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
    Dim workspaceFactory As IWorkspaceFactory=obj
    Dim workspace As IWorkspace=workspaceFactory.OpenFromFile("C:\UnitedStates.gdb", 0)
    
    ' Open the locator workspace.
    obj=Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
    Dim locatorManager As ILocatorManager=obj
    Dim locatorWorkspace As ILocatorWorkspace=locatorManager.GetLocatorWorkspace(workspace)
    
    ' Get a locator.
    Dim addressGeocoding As IAddressGeocoding=locatorWorkspace.GetLocator("USLocator")
    
    ' Set up the address properties.
    Dim addressProperties As IPropertySet=New PropertySet
    With addressProperties
        .SetProperty("Street", "380 New York St.")
        .SetProperty("City", "Redlands")
        .SetProperty("State", "CA")
        .SetProperty("ZIP", "92373")
    End With
    
    ' Geocode an address using the locator.
    Dim matchProperties As IPropertySet=addressGeocoding.MatchAddress(addressProperties)
    
    ' Print the match properties.
    Dim point As IPoint
    Dim matchField As IField
    Dim matchFields As IFields=addressGeocoding.MatchFields
    For matchFieldIndex As Long=0 To matchFields.FieldCount - 1
        matchField=matchFields.Field(matchFieldIndex)
        If matchField.Type=ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry Then
            point=matchProperties.GetProperty(matchField.Name)
            If Not point.IsEmpty Then
                Console.WriteLine("X: " & point.X)
                Console.WriteLine("Y: " & point.Y)
            End If
        Else
            Console.WriteLine(matchField.AliasName & ": " & matchProperties.GetProperty(matchField.Name))
        End If
    Next
End Sub

Using IAddressCandidates and FindAddressCandidates

The following is the way to return all of the address candidates for a single address:
  1. Once you have the locator, set the address parameters, then call FindAddressCandidates. The address parameter is a PropertySet containing properties that represent the input address components used by the locator. Use the AddressFields property on the IAddressInputs interface to determine the input address components used by the locator.
  2. The CandidateFields property of the address locator contains the fields that are returned for each candidate. You can use the fields returned by this property to inspect candidates found using the FindAddressCandidates method.

    See the following code example:
[C#]
public void AddressCandidates()
{
    // Get the Workspace
    System.Object obj=Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriDataSourcesGDB.FileGDBWorkspaceFactory"));
    IWorkspaceFactory2 workspaceFactory=obj as IWorkspaceFactory2;
    IWorkspace workspace=workspaceFactory.OpenFromFile(@"C:\UnitedStates.gdb", 0);

    // Get the Locator and QI to IAddressCandidates2
    obj=Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriLocation.LocatorManager"));
    ILocatorManager locatorManager=obj as ILocatorManager2;
    ILocatorWorkspace locatorWorkspace=locatorManager.GetLocatorWorkspace
        (workspace);
    ILocator locator=locatorWorkspace.GetLocator("USLocator");
    IAddressCandidates2 addressCandidates=locator as IAddressCandidates2;

    // Find the address candidates
    IPropertySet2 addressProperties=new PropertySetClass();
    addressProperties.SetProperty("Street", "380 New York St.");
    addressProperties.SetProperty("City", "Redlands");
    addressProperties.SetProperty("State", "CA");
    addressProperties.SetProperty("Zip", "92373");

    // Use the FindAddressCandidates method find candidates for an address
    IArray resultsArray=addressCandidates.FindAddressCandidates(addressProperties);

    // Use the CandidateFields property to display the properties of each candidate
    IFields candidateFields=addressCandidates.CandidateFields;
    IPropertySet2 candidatePropertySet=null;
    IField addressField=null;
    for (int candidateIndex=0; candidateIndex < resultsArray.Count; candidateIndex
        ++)
    {
        candidatePropertySet=resultsArray.get_Element(candidateIndex)as
            IPropertySet2;
        for (int fieldIndex=0; fieldIndex < candidateFields.FieldCount; fieldIndex
            ++)
        {
            addressField=candidateFields.get_Field(fieldIndex);
            if (addressField.Type != esriFieldType.esriFieldTypeGeometry)
            {
                Console.WriteLine(addressField.AliasName + ": " +
                    candidatePropertySet.GetProperty(addressField.Name));
            }
        }
        Console.WriteLine();
    }
}
[VB.NET]
Sub AddressCandidates()
    ' Get the Workspace
    Dim obj As System.Object=Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
    Dim workspaceFactory As IWorkspaceFactory=obj
    Dim workspace As IWorkspace=workspaceFactory.OpenFromFile("C:\UnitedStates.gdb", 0)
    
    ' Get the Locator and QI to IAddressCandidates2
    obj=Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
    Dim locatorManager As ILocatorManager=obj
    Dim locatorWorkspace As ILocatorWorkspace=locatorManager.GetLocatorWorkspace(workspace)
    Dim locator As ILocator=locatorWorkspace.GetLocator("USLocator")
    Dim addressCandidates As IAddressCandidates2=locator
    
    ' Find the address candidates
    Dim addressProperties As IPropertySet2=New PropertySetClass()
    addressProperties.SetProperty("Street", "380 New York St.")
    addressProperties.SetProperty("City", "Redlands")
    addressProperties.SetProperty("State", "CA")
    addressProperties.SetProperty("Zip", "92373")
    
    ' Use the FindAddressCandidates method find candidates for an address
    Dim resultsArray As IArray=addressCandidates.FindAddressCandidates(addressProperties)
    
    ' Use the CandidateFields property to display the properties of each candidate
    Dim candidateFields As IFields=addressCandidates.CandidateFields
    Dim candidatePropertySet As IPropertySet2
    Dim addressField As IField
    For candidateIndex As Integer=0 To resultsArray.Count - 1
        candidatePropertySet=resultsArray.Element(candidateIndex)
        For fieldIndex As Integer=0 To candidateFields.FieldCount - 1
            addressField=candidateFields.Field(fieldIndex)
            If (addressField.Type <> esriFieldType.esriFieldTypeGeometry) Then
                Console.WriteLine(addressField.AliasName + ": " + candidatePropertySet.GetProperty(addressField.Name).ToString())
            End If
        Next
        Console.WriteLine()
    Next
End Sub


See Also:

How to geocode a table of addresses
Location library overview
Sample: Find an address




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):

Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced