How to geocode a single address
|
| Development licensing |
Deployment licensing |
| Engine Developer Kit |
Engine Runtime |
| ArcView |
ArcView |
| ArcEditor |
ArcEditor |
| ArcInfo |
ArcInfo |
Geocoding a single address
There are a two ways to geocode a single address:
Using IAddressGeocoding and MatchAddress
The following is the primary way to geocode a single address:
- 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.
- 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:
[Java]
public void GeocodeSingleAddress()
{
// Open an AGSServerConnection.
IPropertySet propertySet_Connection = new PropertySetClass();
propertySet_Connection.SetProperty("machine", "mendota");
IAGSServerConnectionFactory agsServerConnectionFactory = new
AGSServerConnectionFactoryClass();
IAGSServerConnection agsServerConnection = agsServerConnectionFactory.Open
(propertySet_Connection, 0);
// Open the AGSLocatorWorkspace.
ILocatorManager2 locatorManager2 = new LocatorManagerClass();
IName name = agsServerConnection.FullName;
IAGSServerConnectionName agsServerConnectionName =
(IAGSServerConnectionName)name;
ILocatorWorkspace locatorWorkspace = locatorManager2.GetAGSLocatorWorkspace
(agsServerConnectionName);
// Get a locator from the AGSLocatorWorkspace.
ILocator locator = locatorWorkspace.GetLocator("USA Streets");
IAddressGeocoding addressGeocoding = new IAddressGeocodingProxy(locator);
// Geocode an address using the locator.
IPropertySet propertySet_Address = new PropertySetClass();
propertySet_Address.SetProperty("Street", "380 New York St.");
propertySet_Address.SetProperty("City", "Redlands");
propertySet_Address.SetProperty("State", "CA");
propertySet_Address.SetProperty("ZIP", "92373");
IPropertySet propertySet_Match = addressGeocoding.MatchAddress
(propertySet_Address);
// Print the match properties.
IFields fields = addressGeocoding.MatchFields;
Int32 int32_MatchFieldIndex = 0;
IField field = null;
IPoint point = null;
for (int32_MatchFieldIndex = 0; int32_MatchFieldIndex < fields.FieldCount;
int32_MatchFieldIndex++)
{
field = fields.get_Field(int32_MatchFieldIndex);
string string_Name = field.Name;
object object_Match = propertySet_Match.GetProperty(string_Name);
if (field.Type == esriFieldType.esriFieldTypeGeometry)
{
point = (IPoint)object_Match;
if (!point.IsEmpty)
{
Debug.Print("X: " + point.X);
Debug.Print("Y: " + point.Y);
}
}
else
{
Debug.Print(field.AliasName + ": " + System.Convert.ToString
(object_Match));
}
}
}
A GeocodeServer is a ServerObject delivered by an ArcGIS Server that can be used to geocode addresses. Internally, a GeocodeServer uses an address locator to do the geocoding and exposes the high-level functionality of the address locator using the IGeocodeServer interface.
Generally, GeocodeServer objects will be used by ArcGIS Server developers to create server applications that include geocoding functionality. However, ArcGIS Desktop and ArcGIS Engine developers can use GeocodeServer objects to include geocoding functionality in ArcGIS Desktop customizations and custom applications using only the high-level geocoding functionality exposed by a GeocodeServer.
See the following code example:
[Java]
// Open a GISServerConnection.
IGISServerConnection serverConnection = new GISServerConnection();
serverConnection.connect("mendota");
// Get a GeocodeServer from the GISServerConnection.
IServerObjectManager som = serverConnection.getServerObjectManager();
IServerContext soc = som.createServerContext("USA Streets", "GeocodeServer");
IGeocodeServer geocodeServer = (IGeocodeServer)soc.getServerObject();
// Construct an address PropertySet to geocode.
IPropertySet addressProperties = (PropertySet)soc.createObject
(PropertySet.getClsid());
addressProperties.setProperty("Street", "380 New York St.");
addressProperties.setProperty("City", "Redlands");
addressProperties.setProperty("State", "CA");
addressProperties.setProperty("ZIP", "92373");
// Set the geocoding properties to use to geocode the address.
IPropertySet locatorProperties = geocodeServer.getLocatorProperties();
locatorProperties.setProperty("MinimumMatchScore", "100");
locatorProperties.setProperty("SpellingSensitivity", "100");
// Geocode the address.
IPropertySet matchProperties = geocodeServer.geocodeAddress(addressProperties,
locatorProperties);
// Print the match properties.
IFields matchFields = geocodeServer.getResultFields(null);
for (int i = 0; i < matchFields.getFieldCount(); i++)
{
IField field = matchFields.getField(i);
if (field.getType() == esriFieldType.esriFieldTypeGeometry)
{
IPoint point = (IPoint)matchProperties.getProperty(field.getName());
if (!point.isEmpty())
{
System.out.println("X: " + point.getX());
System.out.println("Y: " + point.getY());
}
else
{
System.out.println(field.getAliasName() + ": " +
matchProperties.getProperty(field.getName()));
}
}
See Also:
How to geocode a table of addressesLocation library overview