Select to view content in your preferred language

Bind two maps together

667
3
10-18-2011 04:43 PM
Hwee_KiangSim
Emerging Contributor
Hi,
anyone knows if it's possible to bind two maps together? i.e. I have an application that have two map instances in two panels side by side. When I zoom / pan a map at the left hand side, I need the map at right hand side to pan / zoom the same amount and vice verse.
I tried using zoomtoresolution and panto on the right hand map whenever the left side updates but the transition does not seem smooth. Is there a better way to do it?

Thanks
0 Kudos
3 Replies
wangzhifang
Frequent Contributor
What you need is to listen to the each Map's ExtentChanging event, and whenever one map zooms in/out, all others responding the same way.
The key point is to avoiding endless loop by deciding the whether the new extent is the original one.
Here is the sample and key code.
http://newnaw.com/pub/sl/googlemapvsbingmap.html

public MainPage()
        {
            InitializeComponent();

            map1.ExtentChanging += new EventHandler<ESRI.ArcGIS.Client.ExtentEventArgs>(map_ExtentChanged);
            map2.ExtentChanging += new EventHandler<ESRI.ArcGIS.Client.ExtentEventArgs>(map_ExtentChanged);
            map3.ExtentChanging += new EventHandler<ESRI.ArcGIS.Client.ExtentEventArgs>(map_ExtentChanged);
            map4.ExtentChanging += new EventHandler<ESRI.ArcGIS.Client.ExtentEventArgs>(map_ExtentChanged);
            //map3.ViewChangeEnd+=new EventHandler<Microsoft.Maps.MapControl.MapEventArgs>(map_TargetViewChanged);

            this.Loaded+=new RoutedEventHandler(MainPage_Loaded);
        }
...

private void map_ExtentChanged(object sender, ESRI.ArcGIS.Client.ExtentEventArgs e)
        {
            
            if (e.OldExtent != null)
            {
                ESRI.ArcGIS.Client.Map map = sender as ESRI.ArcGIS.Client.Map;
                switch (int.Parse(map.Name.Substring(3, 1)))
                {
                    case 1:
                        if (!AreEqual(map2.Extent, e.NewExtent, 0.000000001))
                        {
                            map2.Extent = e.NewExtent;
                            map3.Extent = e.NewExtent;
                            map4.Extent = e.NewExtent;
                        }   
                        break;
                    case 2:
                        if (!AreEqual(map1.Extent, e.NewExtent, 0.000000001))
                        {
                            map1.Extent = e.NewExtent;
                            map3.Extent = e.NewExtent;
                            map4.Extent = e.NewExtent;
                        }   
                        break;
                    case 3:
                        if (!AreEqual(map4.Extent, e.NewExtent, 0.000000001))
                        {
                            map1.Extent = e.NewExtent;
                            map2.Extent = e.NewExtent;
                            map4.Extent = e.NewExtent;
                        }
                        break;
                    case 4:
                        if (!AreEqual(map3.Extent, e.NewExtent, 0.000000001))
                        {
                            map1.Extent = e.NewExtent;
                            map2.Extent = e.NewExtent;
                            map3.Extent = e.NewExtent;
                        }
                        break;
                }
            }

...

private static bool AreEqual(Envelope e1, Envelope e2, double tolerance)
        {
            return
                Math.Abs(e1.XMin - e2.XMin) < tolerance &&
                Math.Abs(e1.YMin - e2.YMin) < tolerance &&
                Math.Abs(e1.XMax - e2.XMax) < tolerance &&
                Math.Abs(e1.YMax - e2.YMax) < tolerance;
        }
0 Kudos
Hwee_KiangSim
Emerging Contributor
Thanks!!! I tried and it works!
But I'm not sure whether it's because I have an ElementLayer with a relatively big Tiff image, my panning and zooming on the corresponding map flickers and not as smooth as your example.
Anymore ideas on how to make it smoother?
Thanks!
0 Kudos
wangzhifang
Frequent Contributor
Thanks!!! I tried and it works!
But I'm not sure whether it's because I have an ElementLayer with a relatively big Tiff image, my panning and zooming on the corresponding map flickers and not as smooth as your example.
Anymore ideas on how to make it smoother?
Thanks!


You can remove the ElementLayer temporarily to test whether it's caused by it.
Additionally, you can also subscribe to Map's ExtentChanged event, without modifying your code, this event is less cpu intensive.
Have a try.
0 Kudos