private void toggleSubLayerOpacity() { LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions(); layerDrawOptions.LayerID = 0; (MyMap.Layers["MyLayer"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions = new LayerDrawingOptionsCollection() { layerDrawOptions }; (MyMap.Layers["MyLayer"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 0 }; if (layerDrawOptions.Opacity == 0) { layerDrawOptions.Opacity = 1; } else { layerDrawOptions.Opacity = 0; } }
IEnumerable<int> myInts = westTexas.VisibleLayers.Except(new[] {0});
private void ToggleVisibility(int layerId) { var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if ( westTexas == null ) return; //this will return True if the VsibleLayers includes layerId (is Visible), or False if it is not included (not visible) bool isVisible = westTexas.VisibleLayers.Contains(layerId); //This will flip the visbility, if layerId was vsible it rurns it off, if it is not visible it turns it on. westTexas.SetLayerVisibility(layerId, !isVisible); }
I was playing around with this and I notice that after initialization the VisibleLayers collection is still null (which would explain the error). It is not until a method call has been made to SeLayerVisisibility has been made that this collection is actually available. This explains the behavior you see because clicking a layer on the Legend will make a call internally to SetLayerVisibility.I checked this on both 3.0 and 2.4 and see the same behavior.Personally, I would consider this a bug.
bool isVisible = westTexas.GetLayerVisibility(layerIds);
bool isVisible = westTexas.VisibleLayers.Contains(layerIds);
private void Button_Click_1(object sender, RoutedEventArgs e) { int[] layerIDs = new[] { 0, 1, 2, 3, 4, 8 }; foreach ( var layerId in layerIDs ) { ToggleVisibility(layerId); } }
// class to hold extension method public static class ExtensionMethods { public static void ToggleVisibility(this ArcGISDynamicMapServiceLayer layer, IEnumerable<int> layerIds) { foreach (var layerId in layerIds) { layer.SetLayerVisibility(layerId, !layer.VisibleLayers.Contains(layerId)); } } }
private void Button_Click_1(object sender, RoutedEventArgs e) { var westTexas = Map.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if ( westTexas == null ) return; var layerIDs = new[] { 0, 1, 2, 3, 4, 8 }; westTexas.ToggleVisibility(layerIDs); }
private void ToggleVisibility(int[] layerIds) { var westTexas = MyMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if (westTexas == null) return; bool isVisible = westTexas.VisibleLayers.Contains(layerIds); westTexas.SetLayerVisibility(layerId, !isVisible); } private void Button_Click_1(object sender, RoutedEventArgs e) { ToggleVisibility(0, 1 ,2, 3); }
private void ToggleVisibility(int layerId) { var westTexas = MyMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if (westTexas == null) return; bool isVisible = westTexas.VisibleLayers.Contains(layerId); westTexas.SetLayerVisibility(layerId, !isVisible); } private void Button_Click_1(object sender, RoutedEventArgs e) { ToggleVisibility(0); ToggleVisibility(1); ToggleVisibility(2); ToggleVisibility(3); ToggleVisibility(4); ToggleVisibility(8); }
private void toggleSubLayerON(int sublayerId) { ArcGISDynamicMapServiceLayer layer= TabletMap.Layers["Layer"] as ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = layer.VisibleLayers != null ? layer.VisibleLayers.ToList() : new List<int>(); visibleLayerList.Add(sublayerId); layer.VisibleLayers = visibleLayerList.ToArray(); } private void toggleSubLayerOFF(int sublayerID) { ArcGISDynamicMapServiceLayer layer= TabletMap.Layers["Layer"] as ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = layer.VisibleLayers != null ? layer.VisibleLayers.ToList() : new List<int>(); visibleLayerList.Remove(sublayerID); layer.VisibleLayers = visibleLayerList.ToArray(); }
Thanks Joe,I didn't realize this was working because of a strange problem with the my app. The sublayerToggle you provided is being used as an action strung into a button click , but the app also has a Legend with checkboxes controlling the sublayer visibility in my app, when I toggle the layer on or off on the legend first, then the sublayerToggle also works with the button click, but if it hasn't been toggled with the legend, the toggle strung to the button click doesn't do anything. I'm very confused as to why it's doing this, but I'm also pretty sure it has nothing to do with the code you provided me at this point. Thank you very much for the help.
private void ToggleVisibility(int layerId) { var westTexas = MyMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if (westTexas == null) return; bool isVisible = westTexas.VisibleLayers.Contains(layerId); westTexas.SetLayerVisibility(layerId, !isVisible); } private void Button_Click_1(object sender, RoutedEventArgs e) { ToggleVisibility(0); }
Jared,The Except() method is meant to return an IEnumerable<T> of all the elements in an IEnumerable<T> that are not a provided IEnumerable<T> (huh ). So the usage is like this IEnumerable<int> myInts = westTexas.VisibleLayers.Except(new[] {0}); Which returns an IEnumerable<int> of all the values in VisibleLayers Except for 0, which is not really what you want. The code sample I provided should work to Toggle visibility of a given layer. You just need to use contains backwards, if .Contains returns false, then pass SetLayerVisibility a value of true private void ToggleVisibility(int layerId) { var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if ( westTexas == null ) return; //this will return True if the VsibleLayers includes layerId (is Visible), or False if it is not included (not visible) bool isVisible = westTexas.VisibleLayers.Contains(layerId); //This will flip the visbility, if layerId was vsible it rurns it off, if it is not visible it turns it on. westTexas.SetLayerVisibility(layerId, !isVisible); } Hope that helps-joe
Hi Joe,Thanks for the response. It seems like this will work, but I'm getting an error saying System.Array does not contain a definition for 'Contains'. How would i fix this?
bool isNotVisible = westTexas.VisibleLayers.Except(0);
The way to change visibility is to use the SetLayerVisibility method on the ArcGISDynamicMapServiceLayer instead of removing/adding from the VisibleLayers collections, something like. var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; bool isVisible = westTexas.VisibleLayers.Contains(0); westTexas.SetLayerVisibility(0, !isVisible); Hope that helps-Joe
var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; bool isVisible = westTexas.VisibleLayers.Contains(0); westTexas.SetLayerVisibility(0, !isVisible);
private void togglecweiSublayer() { ArcGISDynamicMapServiceLayer westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = westTexas.VisibleLayers != null ? westTexas.VisibleLayers.ToList() : new List<int>(); if (!visibleLayerList.Contains(0)) { visibleLayerList.Remove(0); } else { visibleLayerList.Add(0); } westTexas.VisibleLayers = visibleLayerList.ToArray(); }
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.