Remove ArcGISDynamicServiceLayer

630
4
10-25-2012 08:51 AM
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
0 Kudos
4 Replies
by Anonymous User
Not applicable
If you want to toggle the layer, why not just show/hide it?  Removing and adding the layer is way more expensive. Try calling something like this on the button's onClick:

function toggleLayer(theLayerID){
    
    var theLayer = map.getLayer("theLayerID");

    if (theLayer.visible){
        theLayer.hide();
    }
    else {
        theLayer.show();
    }
}


This wasn't pulled from a running sample or anything, but you get the idea.  Retrieve the layer from the map by ID and then show/hide it.

You will probably also want to change your button style to one that indicates on (visible) or off (hidden) at the same time you toggle the visibility of the layer.

Hope this helps.

Jeff
0 Kudos
MiriEshel
New Contributor III
Hi Jeff,

Thank you for your reply. I think I've tried it but I'll try it again on Sunday.

Can you tell me if the generateRendererTask works on a dynamicLayer or featureLayer? It is a little bit confusing because it has its own url (very similar to featureLayer's url) and I can't know, if it's a dynamic layer or feature layer. I guess it's a dynamic layer because when I uncheck the "allow change symbology and reorder" under mapping tab in Service properties, it does not work, but I can I tell it from the code?

Thanks a lot and have a nice weekend,
Miri
0 Kudos
StephenLead
Regular Contributor III
Can you tell me if the generateRendererTask works on a dynamicLayer or featureLayer?


Hi Miri,

Please mark this thread as answered by clicking on the tick, and post this as a new question.

This helps other people to use these forum posts to find answers more easily.

Cheers,
Steve
0 Kudos
MiriEshel
New Contributor III
Hi Jeff,

Thanks. It works OK.

Miri
0 Kudos