Reodering of Layers

Topics: WinForms Controls
Nov 10, 2014 at 3:33 AM
Hi,
I am using a Sharp Map having so many Vector layers.
Can anyone tell me is this possible to reorder the position of Layers on map.
or any option for layer to (Send to Front or Send to Back).


Regards,
Sgoyal
Coordinator
Nov 10, 2014 at 9:34 AM
You are looking for a user interface. There is no public legend control that allows user interaction that I know?
Nov 10, 2014 at 11:14 AM
Hi ,
I think you got me wrong.
I am just asking related to the Mapbox control.
Example:- I have a Mapbox Control mapbox1 and having three layers 0---2.
If i need to reorder the layers means i want to move first layer on third position and Third position layer to first which best method i have to use :-
1. Remove all layers and then added accordingly.
2. Or Any other option directly move last layer to the First Position.
Coordinator
Nov 11, 2014 at 7:50 AM
I'd say you should remove the layer from the list and insert it at the position you like.
public static void MoveLayerToFront(Map map, string layerName)
{
    if (map == null)
        throw new ArgumentNullException("map");

    if (string.IsNullOrEmpty(layerName))
        throw new ArgumentNullException("layerName");

    var layer = map.GetLayerByName(layerName);
    if (layer == null) 
        throw new ArgumentException("No layer with this name in map", "layerName");

    var layerIndex = map.Layers.IndexOf(layer);
    if (layerIndex == map.Layers.Count - 1)
        // layer is already in front
        return;

    // remove the layer
    map.Layers.RemoveAt(layerIndex);
    // re-add the layer at the updated position
    map.Layers.Insert(layerIndex + 1, layer);
}

public static void BringLayerToFront(Map map, string layerName)
{
    if (map == null)
        throw new ArgumentNullException("map");

    if (string.IsNullOrEmpty(layerName))
        throw new ArgumentNullException("layerName");

    var layer = map.GetLayerByName(layerName);
    if (layer == null)
        throw new ArgumentException("No layer with this name in map", "layerName");

    var layerIndex = map.Layers.IndexOf(layer);
    if (layerIndex == map.Layers.Count - 1)
        // layer is already in front
        return;

    // remove the layer
    map.Layers.RemoveAt(layerIndex);
    // re-add the layer at the last position.
    map.Layers.Add(layer);
}
Note:
  • this does not work with LayerGroup layers. You will have to extend the example code.
  • If you see too much flickering, use Map.Clone(), modify order in clone and return cloned map (instead of void).