Select to view content in your preferred language

Bind multiple maps to one legend (using 2.1 RC)

2365
7
11-17-2010 05:44 PM
RyanCoodey
Frequent Contributor
The new legend/TOC control is great... but is there any way to bind multiple maps, that contain the exact same services and layers, to the same Legend/TOC?  So the same layers are visible in the different maps...

I didn't see any useful events that I could use to set one map to the other either, maybe I missed something.

Thanks a lot!
0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
A Legend control can bind only one map.
If you have multiple maps even with the exact same service, the layers are different (one layer can't be included in 2 maps). This means that the visibility, the scale, ... are (or can be) different by map. So the legend control can't  display 2 layers with only one legend item.

That being said, if you sure that your 2 maps are exactly the same, you can bind the legend control to any of these maps the result will be the same.
I am guessing that what you want is to use the legend control to keep in sync your 2 maps.
This might probably be done by customizing the legend control (when a layer is checked on or off --> check on or off the same layer in the other map) but I think it would be better to create a separate component to manage that. This component would take care of any layer change, not only those done by the legend control.

Not sure I answered to your question, did I?
0 Kudos
RyanCoodey
Frequent Contributor
Thanks Dominique!  That helps!

Exactly, I have another map that is a clone essentially of the first and just want to keep them in sync.  I already have their extents in sync with the ExtentChanged event.  I'll see what I can do with the CheckBox Changed events and keeping them in sync with that.

Thanks!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Another option (independent of the legend control), is you to hook up the 'LayerChanged' event of the layers, and if the 'VisibleLayers' or 'Visible' property changes, you apply the same change to your cloned layer.
0 Kudos
RyanCoodey
Frequent Contributor
Good idea, that was easier... here is what I did then:

Setup in controls loaded event:
            //Set table of contents to show all services
            List<String> layerIDs = new List<String>();
            foreach (Layer layer in Maps[0].Layers)
            {
                layerIDs.Add(layer.ID);
                //Set layer changed event to keep multiple maps in sync
                if (layer is ArcGISDynamicMapServiceLayer)
                {
                    layer.PropertyChanged += Layer_PropertyChanged;
                    ((ArcGISDynamicMapServiceLayer)layer).VisibilityChanged += Layer_VisibilityChanged;
                }
            }
            layerIDs.Reverse();
            Legend.LayerIDs = layerIDs.ToArray();


To support service on/off
        protected void Layer_PropertyChanged(Object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Visible" && MapSplitter != null && MapSplitter.LockMaps)
            {
                Layer layer = (Layer)sender;
                for (Int32 i = 1; i < Maps.Count; i++)
                    Maps.Layers[layer.ID].Visible = layer.Visible;
            }
        }


To support sub-layer on/off
        protected void Layer_VisibilityChanged(Object sender, EventArgs e)
        {
            if(MapSplitter != null && MapSplitter.LockMaps)
            {
                Layer layer = (Layer)sender;
                for (Int32 i = 1; i < Maps.Count; i++)
                {
                    if (layer is ArcGISDynamicMapServiceLayer)
                        ((ArcGISDynamicMapServiceLayer)Maps.Layers[layer.ID]).VisibleLayers = ((ArcGISDynamicMapServiceLayer)layer).VisibleLayers;
                }
            }
        }


FYI, MapSplitter is a control I made for hidding and showing more than one map. 

Hope this maybe helps someone else in the future.

Thanks a lot for your help Dominique!
0 Kudos
AtleyDavidson
New Contributor
Ryan,

Any chance you could post the MapSplitter control?

Thanks,

Atley Davidson
Hamilton County GIS
0 Kudos
dotMorten_esri
Esri Notable Contributor
Why not just put two legend controls on top of each other in a StackPanel?
0 Kudos
RyanCoodey
Frequent Contributor

Any chance you could post the MapSplitter control?


Hi Yeltad,

I can't share too much code since it technically belongs to the company I work for... but I have attached this control with a few little things stripped out.  I wrote this code back when I was learning WPF, so it may not be the best either :P.  Hope it helps some though.

Sorry for the late reply, I was out on vacation...
0 Kudos