How can you get a list of MapViews?

1644
5
Jump to solution
02-16-2021 10:41 AM
kingrollo
New Contributor II

Let's say I want to know the extent of every MapView in my current project. I can retrieve the extent by doing something with the MapView.Extent object.

What I can't see is how to get a list of MapViews? I can:

  • Extract a list of every Map in the current project by using ArcGIS.Desktop.Core.Project.Current.GetItems<MapProjectItem>() and then calling GetMap() on each item
  • Extract the parent Map for each MapView by calling MapView.Map
  • Extract the active MapView extent by calling MapView.Active
  • Do things with MapPanes, but only if the map is in a layout

I might be missing something obvious, so does anyone have any clues?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
kingrollo
New Contributor II

Thanks, this is useful, although it doesn't really address the question. It seems the actual answer is something along these lines...

var mapViews = new List<MapView>();
foreach(var mp in ProApp.Panes.OfType<IMapPane>())
{
    mapViews.Add(mp.MapView);
}
foreach(var lp in ProApp.Panes.OfType<ILayoutPane>())
{
    foreach(var mp in lp.LayoutView.Layout.Elements.OfType<IMapPane>())
    {
        mapViews.Add(mp.MapView);
    }
}

 

View solution in original post

5 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I think this sums it up.   And yes regarding your last bullet:

var mapPanes = ProApp.Panes.OfType<IMapPane>();

  mapPanes only contains 'panes' that are actually open.  Whereas

var mapProjItems = Project.Current.GetItems<MapProjectItem>();

mapProjItems contains all map project items regardless of whether the mapview is open or not.

 

0 Kudos
kingrollo
New Contributor II

Thanks. How do you get the MapViews that are related to a MapProjectItem?

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

MapProjectItems contains maps which you can access through the GetMap() method.  Each map can be displayed by multiple MapViews.  To match MapProjectItems with MapViews that use the same map you would have to iterate through the collections and match the maps using the equals operator.  Below is the button code-behind to find the MapProjectItem for the active MapView:

protected override async void OnClick()
{
  if (MapView.Active?.Map == null) return;
  var findThisMap = MapView.Active?.Map;
  var result = await QueuedTask.Run<string>(() =>
  {
    var mpis = Project.Current.GetItems<MapProjectItem>();
    foreach (var mpi in mpis)
    {
      if (findThisMap == mpi.GetMap())
      {
        return $@"Found map in this project: {mpi.Path}";
      }
    }
    return "not found";
  });
  MessageBox.Show(result);
}

 

0 Kudos
kingrollo
New Contributor II

Thanks, this is useful, although it doesn't really address the question. It seems the actual answer is something along these lines...

var mapViews = new List<MapView>();
foreach(var mp in ProApp.Panes.OfType<IMapPane>())
{
    mapViews.Add(mp.MapView);
}
foreach(var lp in ProApp.Panes.OfType<ILayoutPane>())
{
    foreach(var mp in lp.LayoutView.Layout.Elements.OfType<IMapPane>())
    {
        mapViews.Add(mp.MapView);
    }
}

 

KirkKuykendall1
Occasional Contributor III

Keep in mind a project can have zero or many Layouts.

A layout can have zero or many MapFrames

Each MapFrame references one (and only one) Map.

A Map can be referenced by more than one MapView, each with a different extent.

To illustrate this: Add a layout to the project and add 2 MapFrames, both pointing to the same map.

Activate one of the MapFrames and zoom to some different extent.

Run code to show how the same Map can is referenced by two different MapViews each having a different extent, via MapFrame.GetMapView().

KirkKuykendall1_0-1613757350637.png

 

 

0 Kudos