How to geocode a single address


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 IAdressGeoCoding:matchAddress(). The address parameter is a PropertySet containing variables that represent the input address components used by the locator. Use the address fields property on the IAddressInputs interface to determine the input address components used by the locator.
  • The getMatchFields() method of the address locator contains the coordinates of the points that match the address that was passed in. The getMatchFields() method 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()throws IOException{
    // Open an AGSServerConnection.
    IPropertySet propertySetConnection = new PropertySet();
    propertySetConnection.setProperty("machine", "mendota");
    IAGSServerConnectionFactory agsServerConnectionFactory = new
        AGSServerConnectionFactory();
    IAGSServerConnection agsServerConnection = agsServerConnectionFactory.open
        (propertySetConnection, 0);

    // Open the AGSLocatorWorkspace.
    ILocatorManager2 locatorManager2 = new LocatorManager();
    IAGSServerConnectionName agsServerConnName = (IAGSServerConnectionName)
        agsServerConnection.getFullName();
    ILocatorWorkspace locatorWorkspace = locatorManager2.getAGSLocatorWorkspace
        (agsServerConnName);

    // Get a locator from the AGSLocatorWorkspace.
    ILocator locator = locatorWorkspace.getLocator("USA Streets");
    IAddressGeocoding addressGeocoding = (IAddressGeocoding)locator;

    // Geocode an address using the locator.
    IPropertySet propertySetAddress = new PropertySet();
    propertySetAddress.setProperty("Street", "380 New York St.");
    propertySetAddress.setProperty("City", "Redlands");
    propertySetAddress.setProperty("State", "CA");
    propertySetAddress.setProperty("ZIP", "92373");
    IPropertySet propertySetMatch = addressGeocoding.matchAddress(propertySetAddress)
        ;

    // Print the match properties.
    IFields fields = addressGeocoding.getMatchFields();
    IField field = null;
    IPoint point = null;
    for (int matchFieldIndex = 0; matchFieldIndex < fields.getFieldCount();
        matchFieldIndex++){
        field = fields.getField(matchFieldIndex);
        String field_name = field.getName();
        Object objectMatch = propertySetMatch.getProperty(field_name);
        if (field.getType() == esriFieldType.esriFieldTypeGeometry){
            point = (IPoint)objectMatch;
            if (!point.isEmpty()){
                System.out.println("X: " + point.getX());
                System.out.println("Y: " + point.getY());
            }
        }
        else{
            System.out.println(field.getAliasName() + ": " + objectMatch.toString());
        }
    }
}
 
Using a GeocodeServer
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 addresses
Location library overview




Development licensing Deployment licensing
Engine Developer Kit Engine Runtime
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo