Displaying task and query results

Complexity: Beginner Data Requirement: ArcGIS Tutorial Data for Desktop

This topic describes the recommended ways to display features returned from a task or query on the map by way of a graphics layer and feature layer. The workflows for achieving this are defined and explained, and code examples are provided.

Displaying results in the graphics layer

When task results return, it is possible to retrieve the features and add them into the graphics layer. Task results contain features, which consist of a geometry and attributes but no symbol; therefore, some symbology needs to be added so the feature can be displayed appropriately.

There are two different workflows for displaying a task result in a graphics layer because there are two ways to add symbology to a graphic. The first method is to set a renderer (which contains multiple symbols set for specific graphic attribute values) onto the graphics layer. The second method is to create a specific symbol and set this onto a graphic directly. If both approaches are implemented, the symbol set directly on the graphic takes precedence.

The following sections summarize and explain the two workflows in more detail.

Setting a renderer on the graphics layer

This is the recommended way to provide symbols for features, as it is only necessary to set the renderer on the graphics layer once. All subsequent graphics added to the layer will then get their appropriate symbols set automatically. However, if complex logic is required, which is not supported by the available renderers in the ArcGIS for Android API, or if multiple geometry types in a single graphics layer are required, then setting symbols directly on the graphics is recommended.

Steps:
  1. Initialize a GraphicsLayer object and set its renderer.
  2. Set up the tasks query parameters and execute the query task asynchronously.
  3. When the task result returns, place the graphics from the result into the graphics layer.
  4. Refresh the graphics layer. The graphics display with symbol defined by the renderer.

When a graphics layer's renderer is applied, it is assumed that its graphics have only one type of geometry and the renderer should define the symbology for graphics of that geometry type. Graphics of other geometry types are ignored and will fail to display.

You can choose from one of the available renderers based on your requirements. A SimpleRenderer uses one symbol for every graphic. The UniqueValueRenderer symbolizes graphics into groups with matching attribute values, while a default symbol is assigned to graphics without a matching attribute. A ClassBreaksRenderer symbolizes the graphics according to a numeric range of attribute values. See the following code example:

The following code snippet shows how to define a ClassBreaksRenderer.

ClassBreaksRenderer renderer = new ClassBreaksRenderer();
renderer.setMinValue(0.0);
renderer.setField("POP07_SQMI");
ClassBreak cb1 = new ClassBreak();
cb1.setClassMaxValue(25);
cb1.setSymbol(new SimpleFillSymbol(Color.argb(128, 56, 168, 0)));
cb1.setLabel("First class");
ClassBreak cb2 = new ClassBreak();
cb2.setClassMaxValue(75);
cb2.setSymbol(new SimpleFillSymbol(Color.argb(128, 139, 209, 0)));
cb2.setLabel("Second class");
renderer.addClassBreak(cb1);
renderer.addClassBreak(cb2);

Setting symbols directly on the graphics

This approach is recommended if you have complex logic to determine the symbology of a graphic that is not supported by the available renderers previously described, or if you want to add graphics of multiple geometry types to a single graphics layer on the map.

Steps:
  1. Initialize a GraphicsLayer object.
  2. Set up the tasks query parameters and execute the query task asynchronously.
  3. When the task result returns, inspect each feature in turn, then create and define a symbol for each.
  4. Add each graphic to the graphics layer.
  5. Refresh the graphics layer. The graphics display with the defined symbols.

A rich set of symbols are provided by the ArcGIS for Android API for the display of graphics. For points, you can select from SimpleMarkerSymbol (simple shapes such as circles, squares, and so on), PictureMarkerSymbol (an image you choose), and TextSymbol (to display text). For lines, you can use a SimpleLineSymbol to set the line thickness, color, style, and opacity. For polygons, you can use the SimpleFillSymbol to set the color, opacity, and outline (which is a SimpleFillSymbol). See the following code example:

The following code snippet demonstrates how to create a PictureMarkerSymbol for a point using a building image.

Graphic gr = new Graphic();
Bitmap libImage = BitmapFactory.decodeResource(getResources(), R.drawable.building);
BitmapDrawable libDrawable = new BitmapDrawable(libImage);
PictureMarkerSymbol libSymbol = new PictureMarkerSymbol(libDrawable);
gr.setSymbol(libSymbol)

Displaying selected features in a feature layer

The feature layer is a representation of a feature service or map service that contains multiple features in the form of graphic objects. As the ArcGISFeatureLayer class extends the GraphicsLayer class, these layers can also be used for showing results; however, it is not recommended to add graphics to a feature layer directly. Either use the feature layer connected to a service or create a feature layer from a feature collection. For more information on feature layer settings and selecting features, see Map layer types .

Steps:
  1. Initialize a ArcGISFeatureLayer object.
  2. Set the layers renderer or the layers selection symbol.
  3. Set up a query to select features from the feature layer or apply a layer definition expression. The features (either all or the selected features depending on the layer's settings) display with the appropriate symbol based on the defined renderer.

To symbolize features in a feature layer, it is only recommended to set the renderer at the layer level via the setRenderer() method (for all features) or the setSelectionSymbol() method (for selected features only). It is inappropriate to set the symbol directly on a graphic of a feature layer, since these graphics get updated from the server and their data might be temporal; therefore, the symbol might get overwritten.

Adding a callout

You can enhance your results display by showing the graphic's attributes in a callout. Consider defining an InfoTemplate for the graphic if the callout's default look and feel is acceptable for your application. For more information, see Customizing the callout.

1/24/2013