Identifying features on a map

An Identify operation is a user driven operation typically involving a single click on the map and the retrieval of information about any features hit by that clicked point from any layer in the map. This operation has the potential to retrieve a lot of information depending on the Identify parameters specified. In the ArcGIS Runtime SDK for Java, you can use the Identify task to search the layers in both local and online data for features that intersect an input geometry. Once the matching features are returned, you can display their geometries and attributes in your application.

Using the Identify task

Follow these 4 key steps to identify features on a map:

  1. Instantiate the IdentifyTask class and provide the URL of the online or local service to the task's constructor.
  2. Specify the input parameters to the task.
  3. Execute the task synchronously or asynchronously.
  4. Listen for the completion of the task and process the results.

Instantiate the IdentifyTask

Firstly, obtain the URL of the online or local map service containing the layers that will be used by the IdentifyTask.

Here is the code to obtain the URL to the local map service.

ArcGISLocalDynamicMapServiceLayer theLocalDynamicMapServiceLayer = 
                   new ArcGISLocalDynamicMapServiceLayer("Path to map package (.mpk)");
string theMapServerURL = theLocalDynamicMapServiceLayer.getURL();

Here is the URL to an online map service. In this case the ESRI_Census_USA map server.

string theMapServerURL = 
"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer";

Secondly, instantiate the IdentifyTask class passing the map server's URL to the task's constructor.

Instantiate the IdentifyTask passing in the local or online URL to the map server.

IdentifyTask identifyTask = new IdentifyTask(theMapServerURL);

Specify the inputs to the IdentifyParameters class

The IdentifyParameters class defines how you want the IdentifyTask to operate. Specify the parameters and then pass this class into the constructor of the IdentifyTask. Here are some basic guidelines:

IdentifyParameters identifyparam = new IdentifyParameters(); 
identifyparam.setGeometry(geometry); 
identifyparam.setMapExtent(map.getExtent()); 
identifyparam.setSpatialReference(map.getSpatialReference()); 
identifyparam.setMapHeight(map.getHeight()); 
identifyparam.setMapWidth(map.getWidth()); 
identifyparam.setLayerMode(IdentifyParameters.VISIBLE_LAYERS); 
identifyparam.setTolerance(10);

Execute the task asynchronously

Asynchronous execution of the IdentifyTask is performed using the executeAsync method using the previously defined IdentifyParameters class as input. You also need to define a CallbackListener into which the results will be passed to a set of IdentifyResults. If you execute the task asynchronously the application will remain responsive during the execution of the task. You will need to setup a call back listener to respond when the task completes or error. This is the recommended method of execution for most identiy operations.

identifyTask.executeAsync(identifyparam, new CallbackListener<IdentifyResult[]>(){ 
 
	@Override 
	public void onError(Throwable e){ 
	e.printStackTrace(); 
 } 
 
 @Override 
 public void onCallback(IdentifyResult[] identifyResults) { 
 	
        for (int i = 0; i < identifyResults.length; i++) { 
            IdentifyResult result = identifyResults[i]; 
 
            String resultString = result.getAttributes().get( 
                                  result.getDisplayFieldName()) 
                         + " (" + result.getLayerName() + ")"; 
 
        }
 } 
});

Execute the task synchronously

Synchronous execution of the IdentifyTask is performed using the execute method using the previously defined IdentifyParameters class as input. If you execute the task synchronously the application will be paused whilst the task is executing. The results of the identify will be returned to a set of IdentifyResults that can be examined by the application. This method of execution is only recommended if the identify is reliably rapid and the resultant IdentifyResults is small.

IdentifyResult[] results = task.execute(identifyparam); 

for (int i = 0; i < identifyResults.length; i++) {
 
	IdentifyResult result = identifyResults[i];  
 
 String resultString = result.getAttributes().get( 
                       result.getDisplayFieldName()) 
              + " (" + result.getLayerName() + ")";
});

Process the results

The results of the Identify task are returned in an array of IdentifyResults. Each item in the array represents a feature containing one geometry and one or more attributes with their values. Once you have acquired the IdentifyResult collection all that remains is for you to process the contents.

for (int i = 0; i < identifyResults.length; i++) {
 
	IdentifyResult result = identifyResults[i];  
 
   ... = result.getLayerName();
   ... = result.getDisplayFieldName();
   ... = result.getGeometry();
   ... = result.getAttributes();

});

Sample code

To view any samples related to searching data, please launch the ArcGIS Runtime for Java sample application and explore the interactive samples and their code in the Search section.

2/7/2013