Geocoding

Geocoding is the process of transforming a description of a location—such as a pair of coordinates, an address, or a name of a place—to a location on the earth's surface. You can geocode by entering one location description at a time or by providing many of them at once in a table. The resulting locations are output as geographic features with attributes, which can be used for mapping or spatial analysis.

You can quickly find various kinds of locations through geocoding. The types of locations that you can search for include points of interest or names from a gazetteer, like mountains, bridges, and stores; coordinates based on latitude and longitude or other reference systems, such as the Military Grid Reference System (MGRS) or the U.S. National Grid system; and addresses, which can come in a variety of styles and formats, including street intersections, house numbers with street names, and postal codes.

More information about what geocoding can be used for can be found here

Add Geocoding to your map

The Android API provides a Locator class to geocode and reverse geocode locations using ArcGIS Server Geocoding services. Geocode services are based on address locators. Address locators may reside in an ArcGIS Geodatabase or as *.loc files on disk. They rely on GIS datasets and other reference data to find addresses and locations. Each address locator uses an address style for parsing addresses and standardizing them into well-structured address components. You use ArcMap or ArcCatalog to create new address locators. You can then publish them as Geocode services using ArcMap or ArcCatalog, or using ArcGIS Server’s Manager web application.

ArcGIS Online World Geocoding Service is accessible on the web as a REST web service. The REST web service supports multiple geocoding operations including Find, Find Address Candidates, and Reverse Geocoding. The locator in the ArcGIS API for Android uses these operations to achieve its functionality.

Creating a Locator

The Locator default constructor uses the ArcGIS Online World Geocoding service.

// Create the default Locator from 
// ArcGIS Online World Geocoding service
Locator locator = new Locator();

To instantiate a Locator with a URL from a Geocoding service, you need to provide a URL to a Geocode service REST endpoint. This URL is usually of the form

http://[server:port]/[instance]/services/[service]/GeocodeServer.

The REST endpoint for the World Geocoding service takes the following form:

Locator locator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

Geocoding an address

To find matching locations for an address, you must provide a LocatorFindParameters object containing the address in a format expected by the service. The service we used above allows for a search string to be in the form of a single input field. The Find method is used for single field geocoding and supports the following types of locations:

  • Street address: 380 New York St, Redlands, CA 92373
  • Administrative place names: Seattle, Washington
  • Postal codes: 92573 USA
  • Points of Interest (POI) and businesses: Disneyland

Setting up the Locator Parameters for single field geocoding

// create Locator parameters from single line address string
LocatorFindParameters findParams = new LocatorFindParameters("380 New York St. Redlands CA 92373");
// set the search country to USA
findParams.setSourceCountry("USA");
// limit the results to 2
findParams.setMaxLocations(2);
// set address spatial reference to match map
findParams.setOutSR(mMapView.getSpatialReference());

Run your task as an Android AsyncTask to perform a background operation.

// execute async task to geocode address
new Geocoder().execute(findParams);

Here is an example background Geocoder AsyncTask task overriding the doInBackground method

@Override
protected List<LocatorGeocodeResult> doInBackground(LocatorFindParameters... params) {
    // get spatial reference from map
    SpatialReference sr = mMapView.getSpatialReference();
    // create results object and set to null
    List<LocatorGeocodeResult> results = null;
    // set the geocode service
    locator = new Locator(getResources().getString(http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer));
    try {
        // pass address to find method to return point representing address
        results = locator.find(params[0]);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // return the resulting point(s)
    return results;
}

Geocoding results

Once you have successfully geocoded an address you will most likely want to view the results on a map. Overriding your AsyncTask.onPostExecute() method will allow you to invoke the UI thread after the background geocoding task has completed. The result of geocode task is passed to this step as a parameter.

protected void onPostExecute(List<LocatorGeocodeResult> result){
    if(result == null || result.size() == 0){
        // update UI with notice that no results were found
        Toast toast = Toast.makeText(GeocodeActivity.this, "No result found.", Toast.LENGTH_LONG);
        toast.show();
    } else {
        // get return geometry from geocode result
        Geometry resultLocGeom = result.get(0).getLocation();
        // create marker symbol to represent location
        SimpleMarkerSymbol resultSymbol = new SimpleMarkerSymbol(Color.BLUE, 20, SimpleMarkerSymbol.STYLE.CIRCLE);
        // create graphic object for resulting location
        Graphic resultLocation = new Graphic(resultLocGeom, resultSymbol);
        // add graphic to location layer
        locationLayer.addGraphic(resultLocation);
        // create text symbol for return address
        TextSymbol resultAddress = new TextSymbol(16, result.get(0).getAddress(), Color.BLACK);
        // create offset for text
        resultAddress.setOffsetX(-200);
        resultAddress.setOffsetY(100);
        // create a graphic object for address text
        Graphic resultText = new Graphic(resultLocGeom, resultAddress);
        // add address text graphic to location graphics layer
        locationLayer.addGraphic(resultText);
        // zoom to geocode result
        mMapView.zoomTo(result.get(0).getLocation(), 10);
    }
}
1/24/2013