adi3ek

ArcGISLayerInfo.setVisibility not working as expected - possible bug?

Discussion created by adi3ek on Mar 27, 2013
Latest reply on Apr 12, 2013 by adi3ek
I'm having a hard time trying to get layers hiding/unhiding working properly. I just took "HelloWorld" sample code and tweaked it a little just to hide all layers before adding to MapView and... it desn't work! layers is always visible:

public class HelloWorld extends Activity {
 MapView mMapView = null;
 ArcGISTiledMapServiceLayer tileLayer;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // Retrieve the map and initial extent from XML layout
  mMapView = (MapView) findViewById(R.id.map);
  /* create a @ArcGISTiledMapServiceLayer */
  tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");

  tileLayer.setOnStatusChangedListener(new OnStatusChangedListener() {
   private static final long serialVersionUID = 1L;

   public void onStatusChanged(Object source, STATUS status) {
    debug("LAYER STATUS CHANGE: source=" + source.toString() + " status=" + status);
    if (status == STATUS.INITIALIZED) {
     for (ArcGISLayerInfo layer : ((ArcGISTiledMapServiceLayer) source).getLayers()) {
      layer.setVisible(false);
      debug(" Hiding Layer:" + layer);
     }

     // Add tiled layer to MapView
     mMapView.addLayer((ArcGISTiledMapServiceLayer) source);
    }
   }
  });

  mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
   private static final long serialVersionUID = 1L;

   public void onStatusChanged(Object source, STATUS status) {
    debug("MAP STATUS CHANGE: source=" + source.toString() + " status=" + status);
    if (status == STATUS.LAYER_LOADED && (source == tileLayer)) {
     for (ArcGISLayerInfo layer : ((ArcGISTiledMapServiceLayer) source).getLayers()) {
      debug(" Layer:" + layer);
     }
    }
   }
  });

 }

 @Override
 protected void onPause() {
  super.onPause();
  mMapView.pause();
 }

 @Override
 protected void onResume() {
  super.onResume();
  mMapView.unpause();
 }

 private void debug(String message) {
  if (BuildConfig.DEBUG) Log.d("MY_DEBUG", message);
 }

}


And here's the output:

MY_DEBUG  LAYER STATUS CHANGE: source=com.esri.android.map.ags.ArcGISTiledMapServiceLayer@4129ada0 status=INITIALIZED
MY_DEBUG   Hiding Layer:ArcGISLayerInfo [name=World Street Map, id=0, visible=false, minScale=0.0, maxScale=0.0,isShowLegend=true]
MY_DEBUG  MAP STATUS CHANGE: source=com.esri.android.map.MapView@4128c698 status=INITIALIZED
MY_DEBUG  MAP STATUS CHANGE: source=com.esri.android.map.ags.ArcGISTiledMapServiceLayer@4129ada0 status=LAYER_LOADED
MY_DEBUG   Layer:ArcGISLayerInfo [name=World Street Map, id=0, visible=false, minScale=0.0, maxScale=0.0,isShowLegend=true]


It says that layer has visibility set to false although I can still see it on the screen. I have already tried a much simpler code with only one listener attached to mMapView - same result.

Any help or thought would be much appreciated! :-)

Outcomes