Select to view content in your preferred language

Dynamically Remove Layer

4986
9
05-14-2010 12:10 PM
MikeTischler
Emerging Contributor
Hi,
I've followed the API Example for adding a layer dynamically....but how do I remove one dynamically?  I see plenty of examples for toggling visibility, but how do I actually remove it from the map?  I'm buidling a table of contents that dynamically updates when a new layer is added.  However, I can't figure out how to remove a layer if it's no longer necessary. 

thanks,
mike
0 Kudos
9 Replies
DominiqueBroux
Esri Frequent Contributor
Remove the layer from the layer collection associated to the map.
Something like:
myMap.Layers.Remove(myLayer);
0 Kudos
MikeTischler
Emerging Contributor
which seems easy enough...

but, if you add in 3 layers dynamically (following the example), how do you link them back to a MyLayer object in XAML?  How can you get a layer index?  How can you query a layer name? 

Is there a way to do the layer removal through XAML, or must it be a function in C#/VB?

To be a bit clearer (my original post was pretty vague, I'll admit).  I'm attempting to add a list of objects to a silverlight front end much like the example at http://resources.esri.com/help/9.3/arcgisserver/apis/silverlight/samples/start.htm#LayerList.  This would be done dynamically...you can put in a URL for a service and a new checkbox/slider would appear for that layer underneath the already existing ones.  BUT, I would also like to add a "close" button to the individual stack panels that would 1) Remove the layer from the map entirely and 2) remove the slider/checkbox/button .   I'm having a hard time figuring out how to reference a layer to remove it from the map, if it's added dynamically, and through a control that uses binding such as in the example.

thanks very much for the help!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
In the example, the datacontext of each item is the layer itself (it's why we can display the name, change the opacity....).
So if you add a 'Delete' button, you have to write a code like:
private void ButtonDelete_Click(object sender, System.Windows.RoutedEventArgs e)
{
 FrameworkElement element = sender as FrameworkElement;
 this.MyMap.Layers.Remove(element.DataContext as Layer);
}

The layer will be removed from the map and the button/checkbox/slider will be removed automatically as well (thanks to the SL/WPF binding).

Concerning your question about the layer removal through XAML, I don't think you can do that out of the box.
0 Kudos
MikeTischler
Emerging Contributor
Perfect - Exactly what I was looking for.  Thanks so much!
0 Kudos
dotMorten_esri
Esri Notable Contributor
or simply: myMap.Layers.RemoveAt(index)
0 Kudos
EzequiasRodrigues_da_Rocha
Regular Contributor
But how to get a Layer object if it is in a grouplayer?

Thanks in advance.
0 Kudos
JenniferNery
Esri Regular Contributor
New to v2.3 and v3.0 is GroupLayer. I'm not sure if we are talking about the same layer type here. If yes, to remove a child layer of GroupLayer, you would do:
var groupLayer = MyMap.Layers["MyGroupLayer"] as GroupLayer;
groupLayer.ChildLayers.RemoveAt(index);
0 Kudos
EzequiasRodrigues_da_Rocha
Regular Contributor
I am using agslib2.3 and I didn't find out this class GroupLayer. Could you paste the namespace for me please Jennifer?
0 Kudos
JenniferNery
Esri Regular Contributor
Are you using ArcGIS API for Silverlight? The current version is 2.2.

We are currently working on 2.3. But a 3.0 Beta 1 can be downloaded through ESRI Beta Community with ArcGIS Server 10.1 Beta 1. Both v2.3 and 3.0 versions include GroupLayer. beta.esri.com/community/
0 Kudos