Select to view content in your preferred language

dealing with layers of ArcGISDynamicMapServiceLayer

3144
9
08-13-2012 01:38 AM
lujun
by
Emerging Contributor
i deploy a mxd file on arcgis server which contains 4 layers ,such as l1,l2,l3,l4
code below could display the map correctly:
ArcGISDynamicMapServiceLayer ams = new ArcGISDynamicMapServiceLayer(SERVER_URL);
mMapView.addLayer(ams);

SERVER_URL is the mxd map service url

now i have new requirements ,in some conditions,it need display the map with l1,l2 and l4.
in other conditions, the map should only contains l1,l2 and l3
i don't know how to dealing with such requirement

i get all layers using the method ams.getLayers(), loop the layers  and setVisible(false),but it
does'nt work,i don't konw why
0 Kudos
9 Replies
SimonKlein
Regular Contributor
Do you check in the onstatuschangedlistener or in your method with mapview.isloaded() if if the map is loaded?
0 Kudos
lujun
by
Emerging Contributor
Do you check in the onstatuschangedlistener or in your method with mapview.isloaded() if if the map is loaded?


I have no idea of onstatuschangedlistener ,and what u said is related to my question ? I am new ,so  i dont know the details...
0 Kudos
SimonKlein
Regular Contributor
http://resources.arcgis.com/en/help/android-sdk/concepts/index.html#/Add_layer_to_map/01190000000n00...
You have to check if the map is actually loaded, before you can do anything with its layers etc..
0 Kudos
lujun
by
Emerging Contributor
http://resources.arcgis.com/en/help/android-sdk/concepts/index.html#/Add_layer_to_map/01190000000n00...
You have to check if the map is actually loaded, before you can do anything with its layers etc..


I've kown about  this sample,but my question differents with this.My code below

MapView mMapView;
static final String SERVER_URL = "http://localhost:8399/arcgis/rest/services/room/MapServer";
ArcGISDynamicMapServiceLayer ams;

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mMapView = (MapView) findViewById(R.id.map);
  status = (TextView) findViewById(R.id.status);
  mMapView.setMapBackground(0xffffff, 0xffffff, 10, 10);
  ams = new ArcGISDynamicMapServiceLayer(SERVER_URL);
  
  mMapView.addLayer(ams);

  mMapView.setOnLongPressListener(new OnLongPressListener() {
   private static final long serialVersionUID = 1L;

   public void onLongPress(float x, float y) {
    if(mMapView.isLoaded()){
     ArcGISLayerInfo[] layers = ams.getLayers();
     //this layer is what I want
                                                                    if(layers[1].isVisible())
      layers[1].setVisible(false);    
    }
   }
  });

 }


when I press on the screen for long time,the layer is still on the map,the code does not work.
0 Kudos
SimonKlein
Regular Contributor


...
    if(mMapView.isLoaded()){
     ArcGISLayerInfo[] layers = ams.getLayers();
     //this layer is what I want
                                                                    if(layers[1].isVisible())
      layers[1].setVisible(false);    
    }
   }
  });

 }


...

You need to get the layers from the mapview not the ams.
If a layer (ArcGISDynamic/ArcGISTiled,...) is not directly in a mapview it will not be loaded and you can't work with it.
Your code would have to look something like this:

    if(mMapView.isLoaded()){
     Layers[] layers = mMapView.getLayers();
     //this layer is what I want
                                                                    if(layers[1].isVisible())
      layers[1].setVisible(false);    
    }
0 Kudos
lujun
by
Emerging Contributor
You need to get the layers from the mapview not the ams. 
If a layer (ArcGISDynamic/ArcGISTiled,...) is not directly in a mapview it will not be loaded and you can't work with it. 
Your code would have to look something like this: 

    if(mMapView.isLoaded()){
     Layers[] layers = mMapView.getLayers();
     //this layer is what I want
                                                                    if(layers[1].isVisible())
      layers[1].setVisible(false);    
    }


In onCreate method,I add a dynamic map service layer onto the map.
Layers[] layers = mMapView.getLayers();
And now the mMapView just has only one layer ,that's the whole ArcGISDynamicMapServiceLayer.It's not what I want.
I want to access one of the layers ,such as l1,l2 ,l3 or l4 which i talk about above.
Is there any method which help me to get all 4 layers and then access one of the layers ?
0 Kudos
SimonKlein
Regular Contributor
Ah ok. Could be tricky.
You could try to cast the desired Layer element back to a ArcGISDynamicmapservicelayer. Remove the "old" one from the mapview. Change your visibilitys of the the layer and re-add it to the mapview.

I don't know if this works.
Maybe also debugging the longpress method. MapView.isLoaded() could be a little funny on what it reports in earlier versions of the SDK.

I guess andygup could help you better with this...
0 Kudos
lujun
by
Emerging Contributor
Ah ok. Could be tricky.
You could try to cast the desired Layer element back to a ArcGISDynamicmapservicelayer. Remove the "old" one from the mapview. Change your visibilitys of the the layer and re-add it to the mapview.

I don't know if this works.
Maybe also debugging the longpress method. MapView.isLoaded() could be a little funny on what it reports in earlier versions of the SDK.

I guess andygup could help you better with this...


Anyway,thanks a lot.Maybe I should seperate the layer from the others
0 Kudos
RobertHauck
Deactivated User
Here's how I make individual layers in a ArcGISDynamicMapServiceLayer visible or invisible.  This uses an ArrayList of TOC index numbers passed from another activity.

Bundle extras = intent.getExtras();
     ArrayList<Integer> checkedlyrs =extras.getIntegerArrayList("checkedlayers"); //list of layers to show from layers_activity by TOC index number
     
     for (int i = 0; i < dynamicMapServiceLayer.getAllLayers().length; i++) {
      dynamicMapServiceLayer.getAllLayers().setVisible(false); //set all layers off           
     }
     
     for (int i = 0; i < checkedlyrs.size(); i++) {
      dynamicMapServiceLayer.getAllLayers()[checkedlyrs.get(i)].setVisible(true); //set checked layers visible
     } 
     
     dynamicMapServiceLayer.refresh();
     return;
0 Kudos