Turn on/off layers in mapView programmatically

3371
3
11-29-2011 09:59 AM
DarrenMackiewicz
Occasional Contributor
What I'm trying to do:

Turn layers on and off in a mapView programmatically.

Currently, I do the following

Upon application start, I add a couple of layers to the mapView - An AGSDynamicMapServiceLayer Layer and an AGSFeatureLayer

    [self.mapView addMapLayer:baseLayer withName:@"baseLayer"];
   
    [self.mapView addMapLayer:self.activeFeatureLayer withName:@"featureLayer"];


I then want to programmatically change the layers that are turned on in my mapView. So, I prepare an array of Layer Names that I want turned on.

I then create a new reference to a new AGSDynamicMapServiceLayer. 


    AGSDynamicMapServiceLayer *baseLayer = [[[AGSDynamicMapServiceLayer alloc] initWithMapServiceInfo:bms]autorelease];

I set my baselayer visible layers to be the layers in the array

baseLayer.visibleLayers = layerIDsForMap;

I then reset the mapview (to empty the existing layers)

    [self.mapView reset];

and then reload them in

    [self.mapView addMapLayer:baseLayer withName:@"baseLayer"];
   
    [self.mapView addMapLayer:self.activeFeatureLayer withName:@"featureLayer"];



This, however, is causing some odd behavior elsewhere in the app, for example, when I then go to use the add feature functionality.

So, I'm wondering if there's a less obtrusive way to programmatically set visible layers rather than removing the map service layers from the mapView and adding them back in.
0 Kudos
3 Replies
TOSPOLTOOMMALA
New Contributor
I modify TOC from legend sample.
I keep ref of agslayerview for manage visible while agsmapview:didloadlayerforlayerview (AGSMapViewLayerDelegate). The addTOCForLayerView is just add layerView to NSMutableArray.
- (void) mapView:(AGSMapView*) mapView didLoadLayerForLayerView:(UIView<AGSLayerView>*) layerView {
    [self.legendDataSource addTOCForLayerView:layerView];
}
I set visible of layer like this. Or just set layer.hidden = YES or NO.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UIView<AGSLayerView>* layer = [self.legendDataSource.tocView objectAtIndex:indexPath.row];
        layer.hidden = !layer.hidden;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (layer.hidden) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        } else {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}
Hope this help.
0 Kudos
DarrenMackiewicz
Occasional Contributor
Thanks for the reply.  I don't think that I explained well enough.

I have a layer that i add to the mapView that is the basemap

In that basemap, there are layers (like the layers in a Map Document)

So the "BaseMap" AGSDynamicMapServiceLayer has a bunch of layers in it.
Let's just say
Pavement
Buildings
Sidewalks
Parcels

Then I add an AGSFeatureLayer that has an editable layer.
There is one layer in the AGSFeatureLayer
Let's say

Collection Points

Here's what I'm trying to do:  If pavement and buildings are turned on by default in the map service, and i want to update my map to turn on the parcels and sidewalks, and turn off the pavement and building....that's what I'm trying to do.

I'm not trying to hide the AGSDynamicMapServiceLayer, but rather the individual Layers within that map service.

I hope that helps explain better.
0 Kudos
DarrenMackiewicz
Occasional Contributor
Tospol,
I think I'm following you.
Can you show me how you add the layerView to NSMutableArray?
0 Kudos