Display task and query results

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 a feature layer. The workflows for achieving this are defined and explained, and code examples are provided.

Display results in the graphics layer

When task results are returned, you can retrieve the features and add them to 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 workflows for displaying a task result in a graphics layer because there are two ways to add symbology to a graphic. The first workflow is to set a renderer (which contains multiple symbols set for specific graphic attribute values) on the graphics layer. The second workflow is to create a specific symbol and set it directly on a graphic. If both workflows are implemented, the symbol set directly on the graphic takes precedence.

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

Set a renderer on the graphics layer

This is the recommended workflow to provide symbols for features, since you only need to set the renderer on the graphics layer once. All subsequent graphics added to the layer have their appropriate symbols set automatically. However, if complex logic is required, which is not supported by the available renderers in ArcGIS Runtime SDK for Android, or if multiple geometry types in a single graphics layer are required, then setting symbols directly on the graphics is recommended.

  1. Initialize a GraphicsLayer object and set its renderer.
  2. Set up the task's query parameters and execute the query task asynchronously.
  3. When the task result is returned, place the graphics from the result into the graphics layer.
  4. Refresh the graphics layer.

    The graphics display with the symbol defined by the renderer.

When a graphics layer's renderer is applied, it's 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();
ClassBreak cb1 = new ClassBreak();
cb1.setSymbol(new SimpleFillSymbol(Color.argb(128, 56, 168, 0)));
cb1.setLabel("First class");
ClassBreak cb2 = new ClassBreak();
cb2.setSymbol(new SimpleFillSymbol(Color.argb(128, 139, 209, 0)));
cb2.setLabel("Second class");

Set 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.

  1. Initialize a GraphicsLayer object.
  2. Set up the task's query parameters and execute the query task asynchronously.
  3. When the task result is returned, inspect each feature in turn, and 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 ArcGIS Runtime SDK for Android 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);

Display 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. Since the ArcGISFeatureLayer class extends the GraphicsLayer class, these layers can also be used to show results; however, it's 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 Layer types.

  1. Initialize an ArcGISFeatureLayer object.
  2. Set the layer's renderer or the layer's 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's recommended to set the renderer at the layer level via the setRenderer() method (for all features) or the setSelectionSymbol() method (for selected features only). You should not set the symbol directly on a graphic of a feature layer, since these graphics are updated from the server and their data can be temporal; consequently, the symbol could be overwritten.

Add 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 Customize the callout.