wherewhy

Turn on/off layers in mapView programmatically

Discussion created by wherewhy on Nov 29, 2011
Latest reply on Dec 1, 2011 by wherewhy
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.

Outcomes