How to work with geocoding properties



Setting geocoding properties

To get and set geocoding properties, perform the following steps:
  1. Get the locator.
  2. Get and set any of the geocoding properties using the IGeocodingProperties interface with either of the following options:
    1. Set the geocoding properties temporarily for use in a simple program or method, but do not call UpdateLocator. These changes are not saved in the locator the next time the locator is loaded. See the following code example:
[C#]
public void GeocodingProperties()
{
    // 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.
    obj = Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriLocation.LocatorManager"));
    ILocatorManager2 locatorManager = obj as ILocatorManager2;
    ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspace
        (workspace);
    ILocator locator = locatorWorkspace.GetLocator("USLocator")as ILocator2;

    // Set the geocoding properties.
    IGeocodingProperties2 geocodingProperties = locator as IGeocodingProperties2;
    geocodingProperties.EndOffsetDistance = 10;
    geocodingProperties.EndOffsetDistanceUnits = esriUnits.esriFeet;
    geocodingProperties.AddReferenceIDToMatchFields = true;
    locatorWorkspace.UpdateLocator(locator);
}
[VB.NET]
Sub GeocodingProperties()
    ' Open a workspace from a file geodatabase.
    Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
    Dim workspaceFactory As IWorkspaceFactory2 = obj
    Dim workspace As IWorkspace = workspaceFactory.OpenFromFile("C:\UnitedStates.gdb", 0)
    
    ' Get a locator from the locator workspace.
    obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
    Dim locatorManager As ILocatorManager2 = obj
    Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspace(workspace)
    Dim locator As ILocator = locatorWorkspace.GetLocator("USLocator")
    
    ' Set the geocoding properties.
    Dim geocodingProperties As IGeocodingProperties = locator
    geocodingProperties.EndOffsetDistance = 10
    geocodingProperties.EndOffsetDistanceUnits = esriUnits.esriFeet
    geocodingProperties.AddReferenceIDToMatchFields = True
    locatorWorkspace.UpdateLocator(locator)
End Sub
    1. Call UpdateLocator after the geocoding properties have been set. In this case, the geocoding properties are updated the next time the locator is loaded.

Setting reverse geocoding properties

To get and set reverse geocoding properties, perform the following steps:
  1. Get the locator.
  2. Get and set any of the reverse geocoding properties using the IReverseGeocodingProperties interface with either of the following options:
    1. Set the reverse geocoding properties temporarily for use in a simple program or method, but do not call UpdateLocator. These changes are not saved in the locator the next time the locator is loaded. See the following code example:
    2. Call UpdateLocator after the reverse geocoding properties have been set. In this case, the reverse geocoding properties are updated the next time the locator is loaded.
[C#]
public void reverseGeocoding()
{
    // Open a workspace from a 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 a locator from the locator workspace.
    obj = Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriLocation.LocatorManager"));
    ILocatorManager2 locatorManager = obj as ILocatorManager2;
    ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspace
        (workspace);
    IReverseGeocoding reverseGeocoding = (IReverseGeocoding)
        locatorWorkspace.GetLocator("USLocator");

    // Create a point at which to find the address.
    IAddressGeocoding addressGeocoding = (IAddressGeocoding)reverseGeocoding;
    IFields matchFields = addressGeocoding.MatchFields;
    IField shapeField = matchFields.get_Field(matchFields.FindField("Shape"));
    IPoint point = new PointClass();
    point.SpatialReference = shapeField.GeometryDef.SpatialReference;
    point.X =  - 117.2;
    point.Y = 34.06;

    // Set the search tolerance for reverse geocoding.
    IReverseGeocodingProperties reverseGeocodingProperties = 
        (IReverseGeocodingProperties)reverseGeocoding;
    reverseGeocodingProperties.SearchDistance = 100;
    reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriFeet;

    // Find the address nearest the point.
    IPropertySet addressProperties = reverseGeocoding.ReverseGeocode(point, false);

    // Print the address properties.
    IAddressInputs addressInputs = (IAddressInputs)reverseGeocoding;
    IFields addressFields = addressInputs.AddressFields;
    for (int i = 0; i < addressFields.FieldCount; i++)
    {
        IField addressField = addressFields.get_Field(i);
        Console.WriteLine(addressField.AliasName + ": " +
            addressProperties.GetProperty(addressField.Name));
    }
}
[VB.NET]
Sub ReverseGeocoding()
    ' Open a workspace from a file geodatabase.
    Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
    Dim workspaceFactory As IWorkspaceFactory2 = obj
    Dim workspace As IWorkspace = workspaceFactory.OpenFromFile("C:\Redlands\Redlands93.gdb", 0)
    
    ' Get a locator from the locator workspace.
    obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
    Dim locatorManager As ILocatorManager2 = obj
    Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspace(workspace)
    Dim locator As ILocator = locatorWorkspace.GetLocator("Redlands Locator")
    Dim reverseGeocoding As IReverseGeocoding = locator
    
    ' Create a point at which to find the address.
    Dim addressGeocoding As IAddressGeocoding = reverseGeocoding
    Dim matchFields As IFields = addressGeocoding.MatchFields
    Dim shapeField As IField = matchFields.Field(matchFields.FindField("Shape"))
    Dim point As IPoint = New Point
    point.SpatialReference = shapeField.GeometryDef.SpatialReference
    point.X = -117.2
    point.Y = 34.06
    
    ' Set the search tolerance for reverse geocoding.
    Dim reverseGeocodingProperties As IReverseGeocodingProperties = reverseGeocoding
    reverseGeocodingProperties.SearchDistance = 100
    reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriMeters
    
    ' Find the address nearest the point.
    Dim addressProperties As IPropertySet = reverseGeocoding.ReverseGeocode(point, False)
    
    ' Print the address properties.
    Dim addressInputs As IAddressInputs = locator
    Dim addressFields As IFields = addressInputs.AddressFields()
    For i As Integer = 0 To addressFields.FieldCount - 1
        Dim addressField As IField = addressFields.Field(i)
        Console.WriteLine(addressField.AliasName + ": " + addressProperties.GetProperty(addressField.Name))
    Next
End Sub


See Also:

How to geocode a single address
How to geocode a table of addresses
How to find the address closest to a point using reverse geocoding




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 for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced