(ArcObjects) Code sample to Geocode against a AddressLocator

Here is an Arcobjects code sample to test the Geocoding functionality of the ArcGIS Server against a published Geocode Service.

To run the sample as it is shown – Publish the SanFrancisco Address locator provided with the samples data (location: C:\program files\arcgis\java\samples\data\networkanalyst\SanFran_street_zip.loc) using ArcGIS Server as a Geocode service. In the sample I have called the service “SanFranciscoGeocode”.

If you are planning to use a different locator or name then modify the sample accordingly.

import java.io.IOException;

import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPointProxy;
import com.esri.arcgis.location.IGeocodeServer;
import com.esri.arcgis.location.IGeocodeServerProxy;
import com.esri.arcgis.server.IServerConnection;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObject;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgis.server.IServerObjectProxy;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.system.IPropertySet;
import com.esri.arcgis.system.IPropertySetProxy;
import com.esri.arcgis.system.PropertySet;
import com.esri.arcgis.system.ServerInitializer;

public class GeocodeTest {

//Modify these values as needed
String username = "student";
String password = "student";
String domain = "localhost";
String host = "localhost";
String service = "SanFranciscoGeocode";

IServerContext serverContext = null;

public GeocodeTest() {
ServerInitializer initializer = new ServerInitializer();
initializer.initializeServer(domain, username, password);
System.out.println("Initializing the server");

public void geocode(String street, String zipcode) throws IOException{
try {
IServerConnection serverConnection = new ServerConnection();
IServerObjectManager serverObjectManager = serverConnection.getServerObjectManager();
serverContext = serverObjectManager.createServerContext(service, "GeocodeServer");

IPropertySet address = new IPropertySetProxy(serverContext.createObject(PropertySet.getClsid()));
address.setProperty("Street", street);
address.setProperty("Zone", zipcode);

IServerObject serverObject = new IServerObjectProxy(serverContext.getServerObject());
IGeocodeServer geocodeServer = new IGeocodeServerProxy(serverObject);

IPropertySet result = geocodeServer.geocodeAddress(address, null);
String score = result.getProperty("Score").toString();
String status = result.getProperty("Status").toString();

System.out.println("Geocoding result for: " + street + ", " + zipcode);
System.out.println("Score: " + score);
System.out.println("Status: " + status);

if (status.equals("M")) {
IPoint point = new IPointProxy(result.getProperty("Shape"));
System.out.println("X: " + point.getX() + " || Y: " + point.getY());
} catch(Exception ex) {
} finally {
if (serverContext != null)

public static void main(String args[]) throws Exception {
GeocodeTest gctest = new GeocodeTest();
//good addresses to test
gctest.geocode("550 Arch St", "94132");
gctest.geocode("500 The Embarcadero", "94105");
gctest.geocode("200 Arguello Blvd", "94118");
gctest.geocode("55 Masonic Ave", "94118");
gctest.geocode("1123 Taraval St", "94116");
gctest.geocode("122 Franconia St", "94110");
gctest.geocode("312 Garces Dr", "94132");
gctest.geocode("200 Ashbury St", "94117");
gctest.geocode("500 Mission St", "94105");

//bad address to test
gctest.geocode("1234 abcd def", "53452");
gctest.geocode("2 Miadad St", "94105");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: