Rendering dynamic layers

When you create a dynamic layer and add it to your map, it will display with the default rendering options that have been set up for the online map service or in the map package for a local map service. Dynamic layers give you the power to change the rendering of the dynamic layers on-the-fly from the client application on a map service which has its dynamic layers capability enabled.

For an online map service, this is set at publishing time, please see the About dynamic layers topic in the ArcGIS Services documentation for information on enabling this capability. For a local map service, this capability can be set client-side in code prior to initializing the service. The code below shows how to set the 'Supports Dynamic Layers' property on the map service to 'true' in order to enable changing the rendering of the map service's layers on-the-fly:

// create local dynamic layer from a map package (.mpk)
ArcGISLocalDynamicMapServiceLayer dynamicLayer = new ArcGISLocalDynamicMapServiceLayer("path_to_mpk.mpk");
// enable dynamic layers before the layer/map service is initialized
// add layer to map which will initialize the layer and map service

The call to enable dynamic layers needs to be made before the map service is initialized, therefore before the layer is added to a map's layer list, as shown above.

You can view this property of the initialized map service by opening the service's REST URL in a browser:

Support Dynamic Layers: true

Each dynamic layer has a DynamicLayerInfoCollection associated with it which you can obtain from the layer. A DynamicLayerInfoCollection object is a collection of DynamicLayerInfo objects, one per sub-layer of the dynamic layer. You change the rendering of a sub-layer of your choice by modifying its DynamicLayerInfo object and applying these modifications to the dynamic layer.

The following code sample shows the workflow for changing the renderer on-the-fly for a dynamic layer:

// create a renderer, for example using GenerateRendererTask
Renderer<Graphic> renderer = null;
GenerateRendererTask rendererTask = new GenerateRendererTask(dynamicLayer.getUrl() + "/" + layerId);
//... etc.
// create new drawing info from our renderer
int transparency = 20;
DrawingInfo drawingInfo = new DrawingInfo(renderer, transparency); 
// get the layerInfo from the dynamic layer and set the drawing info 
DynamicLayerInfoCollection layerInfos = dynamicLayer.getDynamicLayerInfos(); 
DynamicLayerInfo layerInfo = layerInfos.get(layerId); 
// refresh the layer to show the new renderer

Further code samples

For full working samples showing how to change the rendering of a dynamic layer on-the-fly, have a look at the sample application installed with the ArcGIS Runtime SDK for Java. Relevant samples can be found under the table of contents heading 'Mapping > Dynamic layers'. They include code on how to create a SimpleRenderer, UniqueValueRenderer, and ClassBreaksRenderer using the GenerateRendererTask.