Legend question

2459
5
10-21-2011 05:48 AM
RickJones
Occasional Contributor II
I have a question about getting legend info for a layer which is part of a frame with other layers.
The Legend info returned is all layers in the frame, not the one I passed.

So, if I ask for legend info on each of 5 layers, I get 5 lines returned each time (25 in the legend window).
Is there a way to get just the layer I sent back?

The sample legend code has been slightly modified to use the layer's View, so I can toggle it off/on, but that's irrelevant to the retrieveLegendInfo.

- (void) addLegendForLayer:(AGSLayer *)layer withView:(UIView*)view {
//Check type of layer
if ([layer isKindOfClass: [AGSDynamicMapServiceLayer class]]) {
 
  //Get the service info
  AGSMapServiceInfo* msi =  ((AGSDynamicMapServiceLayer*)layer).mapServiceInfo;
 
  //Legend only supported for services from  10 Service Pack 1 or above
  if(msi.version>=10.01){
   msi.delegate = self;
   [msi retrieveLegendInfo];
  }else {
   NSLog(@"Skipping layer [%@]. ArcGIS Service must be version 10 SP1 or above",msi.URL );
  }
<..snip..> 
 

- (void)mapServiceInfo:(AGSMapServiceInfo *)mapServiceInfo operationDidRetrieveLegendInfo:(NSOperation*)op {
NSArray* layerInfos = mapServiceInfo.layerInfos;
//Loop through all sub-layers
for (int i=0; i<[layerInfos count]; i++) {
 
  AGSMapServiceLayerInfo* layerInfo = (AGSMapServiceLayerInfo*)[layerInfos objectAtIndex:i];
  NSArray* legendLabels = layerInfo.legendLabels;
  NSArray* legendImages = layerInfo.legendImages;
 
  for(int j=0;j<[legendImages count];j++){
   //Store info for each legend item
   LegendInfo* legendInfo = [[[LegendInfo alloc] init] autorelease];
   legendInfo.name = layerInfo.name;
   legendInfo.detail = [legendLabels objectAtIndex:j];
   legendInfo.image = [legendImages objectAtIndex:j];
   legendInfo.view = nil;
   [self.legendInfos addObject:legendInfo];
  }
 
}
//Reload the table to display newly added legend items
[self reload];
}

PS. By frame, I mean in an ArcMap project.
0 Kudos
5 Replies
NimeshJarecha
Esri Regular Contributor
Rick,

I'm sorry but not clear about your question. Please elaborate your question so can help you.

Whatever, I understood from you question based on that.....You always request retrieveLegendInfo for each layer and it's returns legend for that layer only. It's up to you how you add that information to the table (legend view).

Regards,
Nimesh
0 Kudos
RickJones
Occasional Contributor II
If layer1 is alone in an ArcMap frame, the LegendInfo returns just that layer's info.

If layer1, layer2, layer3 are in the same frame together:
   I ask for LegendInfo on layer1, I get info for layer1, layer2, layer3.
   I ask for info on layer 2, and get info for layer1, layer2, layer3.
   I ask for info on layer 3, and get info for layer1, layer2, layer3.

Because the "mapServiceInfo operationDidRetrieveLegendInfo" method has no idea which of the 3 layers I asked for, and they can be returned in any order, I don't know which to display.
0 Kudos
RickJones
Occasional Contributor II
I think I've figured out my problem.

As mentioned, there are multiple layers sharing a frame in ArcMap.
The service points to the frame.

Loading the layer:

    dLayer = [[AGSDynamicMapServiceLayer alloc] initWithURL:[NSURL URLWithString:WaterNetworkURL]];  // points to the service with 5 layers
dLayer.visibleLayers = [NSArray arrayWithObjects:[NSNumber numberWithInt:0], nil];
[self.mapView addMapLayer:dLayer withName:@"Hydrants"];
[dLayer release];
  
I assumed the visibleLayers, since it only contained 1 row, would ignore the rest.
If they are all still there, just invisible, the legend probably sees them.

Am I on the right track?

If I include all layers in visibleLayers:

dLayer.visibleLayers = [NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:5], nil];

LegendInfo acts the same, but I also see them on the map.
0 Kudos
RickJones
Occasional Contributor II
So, I guess now my question is, how do I match up the layer names from the legendInfo with the layer names from self.mapView.mapLayerViews?

The legend is returning a list Address, Parcel, Valve, Hydrant, Water Meter, Water Main, etc.
The LayerViews has Address, Parcel, Water Network.

Is there a method to get the sub-layers (sub-views?), the ones I refer to in visibleLayers array?
0 Kudos
NimeshJarecha
Esri Regular Contributor
Okie..here is how you should handle this...

Consider, if there are 5 layers a published map service but only 2 are visible. So you want to show legend for only 2 layer in the legend view. When you ask for legend with retrieveLegendInfo, then it returns legend for all layers. At this time you should add only layers which are visible in the self.legendInfos.

Hope this helps!

Regards,
Nimesh
0 Kudos