How do I zoom the map frame in layout view to a layer in the TOC using ArcGIS Pro SDK (C#)?

1647
7
09-18-2018 11:26 AM
Highlighted
New Contributor II

Hello,

I can not figure out how to zoom the map frame in layout view to a specific layer using ArcGIS Pro SDK (C#)?

 

Thank you!

Tags (1)
Reply
0 Kudos
7 Replies
Highlighted
Esri Regular Contributor

Hi Joshua

Here is a code snippet you can use to zoom the map frame in layout view to a specific layer:

            LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));

            QueuedTask.Run( () => {
                //Get the layout from the layout project item
                Layout layout = layoutItem.GetLayout();
                //Get the map frame in the layout
                MapFrame mapFrame = layout.FindElement("New Map Frame") as MapFrame;
                //Get map
                var mapFrameMap = mapFrame.Map;
                //Get the specific layer you want from the map
                var lyrOfInterest = mapFrameMap.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
                //Get the Layer's extent
                var lyrExtent = lyrOfInterest.QueryExtent();
                //Zoom to this extent in the MapFrame
                //Note: This method will be deprecated at 2.3.
                //But you can use mapFrame.SetCamera(lyrOfInterest) instead. (This will be a new overload available in 2.3)
                mapFrame.ZoomTo(lyrExtent);
            });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks

Uma

Reply
0 Kudos
Highlighted
New Contributor II

Thank you this worked great! But is there a way to 1) zoom to only a selected feature in a layer rather than the whole layer, and then 2) zoom out a certain percentage more? For example, zoom to California in the states layer. And then zoom out a bit more so that it is not zoomed all the way in to California?

Thank you

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Hi Joshua,

The way I got this to work was to create a query and iterate using a cursor. You want to make sure your query returns one feature.

////Zoom to the envelope of each feature
//Reference the layout
Layout layout = LayoutView.Active.Layout;

//Select the feature
await QueuedTask.Run(() =>
{
  //Reference the mapframe, associated map, and build a query
  MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
  Map m = mf.Map;
  FeatureLayer fl = m.FindLayers("GreatLakes").First() as FeatureLayer;
  QueryFilter qf = new QueryFilter();
  string whereClause = "NAME = 'Lake Erie'";
  qf.WhereClause = whereClause;

  //Zoom to the feature
  using (ArcGIS.Core.Data.RowCursor rowCursor = fl.Search(qf))
  {
    while (rowCursor.MoveNext())
    {
      // get the shape from the row
      ArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature;
      Polygon polygon = feature.GetShape() as Polygon;
      Envelope env = polygon.Extent as Envelope;
      mf.ZoomTo(env);
    }
  }
});

I hope this helps,

Jeff

Highlighted
New Contributor II

Thank you! I am trying to zoom out a certain percentage further, so for example, zoom to Lake Erie, but then zoom out 10% further. Is that possible? Also, I am trying to do a select by location (select state that intersects with a polygon project boundary, and then zoom to that state (but out a bit further such as 10%).

Thank you

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Try adding the last 3 lines to the code I already provided.  I simply multiply the scale by 110%

// get the shape from the row
ArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature;
Polygon polygon = feature.GetShape() as Polygon;
Envelope env = polygon.Extent as Envelope;
mf.ZoomTo(env);
Camera cam = mf.Camera;
cam.Scale = cam.Scale * 1.1;
mf.SetCamera(cam);

 

Also, concerning your select by location question ... see: https://community.esri.com/thread/210362-how-can-i-make-a-select-by-location-in-pro-sdk 

Jeff

Highlighted
New Contributor II

Thank you Jeff! It looks like the select by location answer that you pointed me to involves point features and sketching. I am trying to select the state (California, Florida, etc.) from a "States" polygon dataset that my polygon project boundary intersects with. And then zooming the map to that state.

Your help and input is greatly appreciated.

Thank you for your past reply. Setting the camera scale worked.

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Go back to this site: How can I make a "Select by location" in PRO SDK 

I added a code snippet.

The original question on this thread was answered.  I'm trying to keep examples / threads relevant to the topic so we have better success searching for stuff.  Note - my example may not be the most concise but it works. I'm not sure how to get the selection's geometry without using a cursor but I'm sure there is an easier way.  I'm a layout SDK guy.  🙂

Reply
0 Kudos