ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / MapFrame Class / SetCamera Method / SetCamera(IEnumerable<Layer>,Boolean) Method
A list of layers.
Example

In This Topic
    SetCamera(IEnumerable<Layer>,Boolean) Method
    In This Topic
    Sets the map frame extent using the extent of multiple layers. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Overloads Sub SetCamera( _
       ByVal layers As IEnumerable(Of Layer), _
       Optional ByVal useSelectedFeatures As Boolean _
    ) 

    Parameters

    layers
    A list of layers.
    useSelectedFeatures
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Remarks
    SetCamera will have no effect a map frame's extent being driven by a map series.
    Example
    Create Map Frame and Set Camera
    {
      //Create a map frame and set its camera by zooming to the extent of an existing bookmark.
      //Construct on the worker thread
    
      //Build 2D envelope geometry
      Coordinate2D mf_ll = new Coordinate2D(6.0, 8.5);
      Coordinate2D mf_ur = new Coordinate2D(8.0, 10.5);
      Envelope mf_env = EnvelopeBuilderEx.CreateEnvelope(mf_ll, mf_ur);
    
      //Reference map, create MF and add to layout
      MapProjectItem mapPrjItem = Project.Current.GetItems<MapProjectItem>()
                           .FirstOrDefault(item => item.Name.Equals("Map"));
      Map mfMap = mapPrjItem.GetMap();
      Bookmark bookmark = mfMap.GetBookmarks().FirstOrDefault(
                            b => b.Name == "Great Lakes");
    
      MapFrame mfElm = ElementFactory.Instance.CreateMapFrameElement(
                           layout, mf_env, mfMap, "New Map Frame");
    
      //Zoom to bookmark
      mfElm.SetCamera(bookmark);
    }
    Change map frame extent to selected features in multiple layers
    {
      //Change the extent of a map frame to the selected features multiple layers.
      //Note: Must be on QueuedTask.Run
      //Reference MapFrame
    
      //Reference map, layers and create layer list
      Map m = mapFrame.Map;
      FeatureLayer fl_1 = m.FindLayers("GreatLakes").First() as FeatureLayer;
      FeatureLayer fl_2 = m.FindLayers("States_WithRegions").First() as FeatureLayer;
      var layers = new[] { fl_1, fl_2 };
      //IEnumerable<Layer> layers = m.Layers;  //This creates a list of ALL layers in map.
    
      //Set the map frame extent to the selected features in the list of layers
      mapFrame.SetCamera(layers, true);
    }
    Change map frame extent to single feature with 15 percent buffer
    {
      //Change map frame extent to single feature with 10 percent buffer
    
      //Note: Must be on QueuedTask.Run
    
      //Reference the mapframe and its associated map
      Map m = mapFrame.Map;
    
      //Reference a feature layer and build a query (to return a single feature)
      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 and set extent
          using (var feature = rowCursor.Current as ArcGIS.Core.Data.Feature)
          {
            Polygon polygon = feature.GetShape() as Polygon;
            Envelope env = polygon.Extent as Envelope;
            mapFrame.SetCamera(env);
    
            //Zoom out 15 percent
            Camera cam = mapFrame.Camera;
            cam.Scale = cam.Scale * 1.15;
            mapFrame.SetCamera(cam);
          }
        }
      }
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also