Visible Layers in a DynamicMapServiceLayer

3754
9
Jump to solution
07-14-2012 10:50 PM
NicoTek
New Contributor III
Hello Everyone,

I have the following layer,

ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer(my_map_server_url);

And i know i can add a int array to the constructor to decide which layers are visible in the following way:

ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer(my_map_server_url,new int[] {0,1,2});

but can I change the visible layers programmatically?
For example I would like to have a button where the user can select which layer to see.

How can I achieve this?

Thanks!
0 Kudos
1 Solution

Accepted Solutions
ViridianaGO
New Contributor III
Hi, what I do is something like this, depending what you want:

ArcGISDynamicMapServiceLayer agdms = new ArcGISDynamicMapServiceLayer(my_map_server_url); ArcGISLayerInfo[] arcgisLayerInfo = agdms.getAllLayers();          for(int j=0; j<arcgisLayerInfo.length; j++) {    arcgisLayerInfo.setVisible(my_boolean_value); }


Let me know if it helps you.

View solution in original post

0 Kudos
9 Replies
Town_ofSnowflake
Occasional Contributor
As a very general response, you should be able to:

XML:
1) Create a button in your layout
2) Specify a method that is called when the button is clicked

Java:
3) Define a method for your activity that redefines which layers are visible using cases

Let me know if you want more clarification.
0 Kudos
NicoTek
New Contributor III
@meloking
I understand how to create buttons and specify methods.

My question is specifically about how to change the visible layers in a DynamicMapServiceLayer.
So in your post it would be what is the Java code that allows me to redefine which layers are visible.

Thanks again.
0 Kudos
ViridianaGO
New Contributor III
Hi, what I do is something like this, depending what you want:

ArcGISDynamicMapServiceLayer agdms = new ArcGISDynamicMapServiceLayer(my_map_server_url); ArcGISLayerInfo[] arcgisLayerInfo = agdms.getAllLayers();          for(int j=0; j<arcgisLayerInfo.length; j++) {    arcgisLayerInfo.setVisible(my_boolean_value); }


Let me know if it helps you.
0 Kudos
NicoTek
New Contributor III
Thank you Diana, that seems to be the right way.

What I did was to create a switch statement that removes the layer and calls it again with the layers I want to see : S

Basically this:
     map.removeLayer(1);
     layer = new ArcGISDynamicMapServiceLayer(my_map_server_url,Array_with_layers_I_Want);
     map.addLayer(layer);


Hi, what I do is something like this, depending what you want:

ArcGISDynamicMapServiceLayer agdms = new ArcGISDynamicMapServiceLayer(my_map_server_url);
ArcGISLayerInfo[] arcgisLayerInfo = agdms.getAllLayers();         
for(int j=0; j<arcgisLayerInfo.length; j++)
{
   arcgisLayerInfo.setVisible(my_boolean_value);
}


Let me know if it helps you.
0 Kudos
ViridianaGO
New Contributor III
Yes, that could be another way to solve that, as I told you, depending your needs, I have a ExpandableListView and I use setVisible method from ArcGISLayerInfo.
Recive greetings from México 😉
0 Kudos
MiriEshel
New Contributor III
Hi,

I have a dynamic layer. I use Lavascript API 3.2.
I want to toggle visibility of DynamicLayer on each click on the button.

I tried to remove it in many ways but it is still show on the map.
The latest try is attached:

 
      elecUrl = "http://gis2012.emap.co.il/arcgis/rest/services/CompareElections/MapServer";
         elecLayer = new esri.layers.ArcGISDynamicMapServiceLayer(elecUrl, {
             "id": "elec",
             "opacity": 0.7
         });
         if (elecLayer != null) {          
             if (map.layerIds.length > 1) {
                //var visible = [];
                //visible.push(-1);
                //elecLayer.setVisibleLayers(visible);

                map.removeLayer(elecLayer);
              }
            else {
                 elecLayer.setVisibleLayers([0]);

                 var symbol = new esri.symbol.SimpleFillSymbol();
                 symbol.setColor(new dojo.Color([150, 150, 150, 0.5]));

                   var renderer = new esri.renderer.UniqueValueRenderer(symbol, "WinningParty");
                 renderer.addValue("א�?ת", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([238, 31, 58, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([132, 164, 203, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([121, 71, 0, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([126, 149, 102, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([39, 166, 117, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([71, 102, 36, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([160, 160, 225, 1])));
                 renderer.addValue("�?�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255, 140, 0, 1])));
                 renderer.addValue("�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([91, 91, 169, 1])));
                 renderer.addValue("�?�?�?", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([25, 106, 255, 1])));
                 renderer.addValue("�?רץ", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([0, 206, 0, 1])));
                 renderer.addValue("עם", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([182, 212, 168, 1])));
                 renderer.addValue("שס", new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([136, 111, 69, 1])));



                 // set up the parameters for the dynamic layer
                 var optionsArray = [];
                 var drawingOptions = new esri.layers.LayerDrawingOptions();
                 drawingOptions.renderer = renderer;
                 optionsArray[0] = drawingOptions;
                 elecLayer.setLayerDrawingOptions(optionsArray);
                 // add the layer to the map
                 try {
                     map.addLayer(elecLayer);
                 }
                 catch (err) {
                     console.log(err.message);
                 }
             }


What do I do wrong?
As you can see in comment, I tried setVisibleLayers but no impact on the map - it still shows the layer.

Please try to help ASAP.

Thanks a lot,
Miri

Thanks a lot,
Miri
0 Kudos
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
Might want to post this over in the Javascript forum, not the Android API Forum...
http://forums.arcgis.com/forums/15-ArcGIS-API-for-JavaScript
0 Kudos
MiriEshel
New Contributor III
Oops. I'm sorry....

Thanks.
Miri
0 Kudos
weiyunli
New Contributor
Hi, what I do is something like this, depending what you want:

ArcGISDynamicMapServiceLayer agdms = new ArcGISDynamicMapServiceLayer(my_map_server_url);
ArcGISLayerInfo[] arcgisLayerInfo = agdms.getAllLayers();         
for(int j=0; j<arcgisLayerInfo.length; j++)
{
   arcgisLayerInfo.setVisible(my_boolean_value);
}


Let me know if it helps you.


I did as you said before, but the program still had problems.Can you help me?
[ATTACH=CONFIG]29924[/ATTACH]
my code
dlayer = new ArcGISDynamicMapServiceLayer(url);
  mMapView.addLayer(dlayer);
  ArcGISLayerInfo[] layinfo = dlayer.getAllLayers();
  layinfo[0].setVisible(true);
Thank you!
0 Kudos