ArcGIS Pro 3.1 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / MapFrame Class / SetCamera Method / SetCamera(Layer,Boolean) Method
Layer
Example

In This Topic
    SetCamera(Layer,Boolean) Method
    In This Topic
    Sets the map frame extent using the a layer's extent. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void SetCamera( 
       Layer layer,
       bool useSelectedFeatures
    )
    Public Overloads Sub SetCamera( _
       ByVal layer As Layer, _
       Optional ByVal useSelectedFeatures As Boolean _
    ) 

    Parameters

    layer
    Layer
    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_MapFrame
    //Creates a new map frame and changes the camera's scale.
    
    //Constuct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build geometry
      Coordinate2D ll = new Coordinate2D(6.0, 8.5);
      Coordinate2D ur = new Coordinate2D(8.0, 10.5);
      //At 2.x - Envelope env = EnvelopeBuilder.CreateEnvelope(ll, ur);
      Envelope env = EnvelopeBuilderEx.CreateEnvelope(ll, 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();
      //At 2.x - mfElm = LayoutElementFactory.Instance.CreateMapFrame(layout, env, mfMap);
      //         mfElm.SetName("New Map Frame"); 
      mfElm = ElementFactory.Instance.CreateMapFrameElement(layout, env, mfMap, "New Map Frame");
    
      //Set the camera
      Camera camera = mfElm.Camera;
      camera.Scale = 24000;
      mfElm.SetCamera(camera);
    });
    Zoom map frame to extent of a single layer
    //Zoom map frame to the extent of a single layer.
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference MapFrame
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
    
      //Reference map and layer
      Map m = mf.Map;
      FeatureLayer lyr = m.FindLayers("GreatLakes").First() as FeatureLayer;
    
      //Set the map frame extent to all features in the layer
      mf.SetCamera(lyr, false);
    });
    Change map frame extent to single feature with 15 percent buffer
    //Change map frame extent to single feature with 10 percent buffer
    
    //Process on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference the mapframe and its associated map
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
      Map m = mf.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;
            mf.SetCamera(env);
    
            //Zoom out 15 percent
            Camera cam = mf.Camera;
            cam.Scale = cam.Scale * 1.15;
            mf.SetCamera(cam);
          }
        }
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.0 or higher.
    See Also