Dynamic map service layer
An ArcGIS dynamic map service layer displays map content from an ArcGIS Server map service that has not 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 dynamic 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). Each map document contains the features that need to displayed in a map, for example, real world entities such as buildings, pipes, and parcels. Features are organized into layers, for example, a 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, scale ranges of layers, and so on. A map document can be published to an ArcGIS Server to create a map service.
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 AGSMapServerInfo, then 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];
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, making it appear sluggish. Instead, 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.
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];