Finding the address closest to a point using reverse geocoding
When given a location on a map, if you want to find the address closest to that point, use reverse geocoding. The IReverseGeocoding and IReverseGeocodingProperties interfaces provide access to members for finding the address closest to a point.
- To define the maximum search tolerance in the units specified by the SearchDistanceUnits property, use the SearchDistance property when looking for an address represented by a point.
The ReverseGeocode method returns a PropertySet that represents the address closest to the point passed as the location parameter.
The point object passed to the location parameter must be projected into the spatial reference used by the locator before it is passed to the ReverseGeocode parameter.
- Use the bReturnIntersection parameter to indicate whether the ReverseGeocode method should return an intersection address. The property names contained in the PropertySet correspond to the Field object names in the Fields collection returned by the AddressFields property on IAddressInputs on the locator. See the following code example:
public void ReverseGeocode()
{
// 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]
Public Sub ReverseGeocode()
' 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")
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:
Location library overviewSample: Find the closest intersection from a point
How to reverse geocode point features in a feature class
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):
ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.Geometry ESRI.ArcGIS.Location
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 |