ArcGIS Dynamic Map Service Layer

An ArcGIS Dynamic Map Service layer displays map content from an ArcGIS Server Map service. The Map service must not have been cached (tiled). The service generates map images when requested by the layer. As the user navigates the map, the layer requests new map images to be displayed. Because the map images are created dynamically, the contents of the map images can be customized by modifying some layer properties. If a dynamic layer's spatial reference does not match the map's spatial reference, the dyanmic layer will automatically re-project its map contents to match the map's spatial reference.

ArcGIS Server Map services are based on map documents (*.mxd or *.msd files). A map document contains the features that need to displayed in a map. Features are real world entities such as buildings, pipes, and parcels. Features are organized into layers. For example, the map document of a national park may contain separate layers for hiking trails, picnic areas, campgrounds, and major roads. The map document also contains information that controls the appearance of the map, such as the symbology, visibility, and scale ranges of layers, etc. Map documents can be published to an ArcGIS Server to create Map services.

ArcGIS Server Map services are accessible on the web as SOAP and REST web services. You can find the URL of these web service endpoints using the ArcGIS Server Service Directory.

To instantiate an AGSDynamicMapServiceLayer, you need to provide a URL to the map service's REST web service endpoint. This URL is usually of the form http://<server:port>/<instance>/rest/services/<service>/MapServer

NSURL* url = [NSURL URLWithString: @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"];

AGSDynamicMapServiceLayer* layer = [AGSDynamicMapServiceLayer dynamicMapServiceLayerWithURL: url];

Another way to create the layer is by using an AGSMapServiceInfo object. You first create an AGSMapServerInfo and then in turn use it to create a layer. This is useful if you want to inspect the properties of the Map service before creating the layer, or if you want the layer to use different default settings, such as layer definitions or layer visibility, right from the start.

NSURL* url = [NSURL URLWithString: @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/ Specialty/ESRI_StateCityHighway_USA/MapServer"];
NSError *error = nil; 
AGSMapServiceInfo *info = [AGSMapServiceInfo mapServiceInfoWithURL:url error:&error];

//inspect or modify the info object if you want
...;

AGSDynamicMapServiceLayer* layer = [AGSDynamicMapServiceLayer dynamicMapServiceLayerWithMapServiceInfo: info];

You should note that creating an AGSMapServiceInfo is synchronous, which means that it will block the calling thread. You should not call this method on the main application thread as it will prevent your application from responding to user input and thus make it appear sluggish. Instead, you should call this method on a background thread.

After creating the layer, you can add it to the map to display its contents. When the layer is ready, its loaded property will be enabled. You can then safely access its properties.

if(layer.loaded){ 
 NSLog(@"Initial Envelope : %@", layer.initialEnvelope); 
}

You can alter the map contents of the layer by modifying some properties. For example, you can specify which layers in the service should be visible by modifying the visibleLayer property.

//only display ‘ushigh’ (id=0) and ‘states’ (id=1) layer 
layer.visibleLayers = [NSArray arrayWithObjects:[NSNumber numberWithInt:0],[NSNumber numberWithInt:1], nil];

You can also choose to display only a subset of features in your dataset by specifying a layer definition.

//only display states beginning with 'C' 
int layerID = 1 ; 
NSString* defString = @"STATE_NAME like ‘C%’";
AGSLayerDefinition* layerDef = [AGSLayerDefinition  layerDefinitionWithLayerId:layerID definition:defString];

layer.layerDefinitions = [NSArray arrayWithObject: layerDef];

5/9/2012