ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / MapFrame Class / Map Property
Example

In This Topic
    Map Property (MapFrame)
    In This Topic
    Gets the Map associated with the MapFrame.
    Syntax
    public Map Map {get;}
    Public ReadOnly Property Map As Map

    Property Value

    Type: Map
    Example
    Create Table Frame
    {
      //Create a table frame.
      await QueuedTask.Run(() =>
      {
    
      //Build 2D envelope geometry
      Coordinate2D rec_ll = new Coordinate2D(1.0, 3.5);
      Coordinate2D rec_ur = new Coordinate2D(7.5, 4.5);
      Envelope rec_env = EnvelopeBuilderEx.CreateEnvelope(rec_ll, rec_ur);
    
      //Reference map frame and layer
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
      FeatureLayer lyr = mf.Map.FindLayers("GreatLakes").First() as FeatureLayer;
    
      //Build fields list
      var fields = new[] { "NAME", "Shape_Area", "Shape_Length" };
    
      //Construct the table frame
    
      var tableFrameInfo = new TableFrameInfo()
      {
        FieldNames = fields,
        MapFrameName = mf.Name,
        MapMemberUri = lyr.URI
      };
      var tabFrame = ElementFactory.Instance.CreateMapSurroundElement(
        layout, rec_env, tableFrameInfo) as TableFrame;
      });
    }
    Apply a style to Grid and Graticules
    {
      var project = Project.Current;
      await QueuedTask.Run(() =>
      {
        //Get the Style project items in the project
        var styleProjectItems = project?.GetItems<StyleProjectItem>();
        //Get the ArcGIS 2D Style Project Item
        var styleProjectItem =
        styleProjectItems.OfType<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS 2D");
        if (styleProjectItem == null)
        {
          return;
        }
        //Get the grid style item you need
        var gridStyleItem =
        styleProjectItem.SearchSymbols(StyleItemType.Grid, "Blue Vertical Label Graticule").FirstOrDefault();
        if (gridStyleItem == null)
        {
          return;
        }
    
        var symbolItemName = gridStyleItem.Name;
        var girdGraticuleObject = gridStyleItem.GetObject() as CIMMapGrid;
        var cmf = mapFrame.GetDefinition() as CIMMapFrame;
        //note, if page units are _not_ inches then grid's gridline
        //lengths and offsets would need to be converted to the page units
        var mapGrids = new List<CIMMapGrid>();
        if (cmf.Grids != null)
        {
          mapGrids.AddRange(cmf.Grids);
        }
    
        switch (girdGraticuleObject)
        {
          case CIMGraticule:
            var gridGraticule = girdGraticuleObject as CIMGraticule;
            gridGraticule.Name = symbolItemName;
            gridGraticule.SetGeographicCoordinateSystem(mapFrame.Map.SpatialReference);
            //assign grid to the frame
            mapGrids.Add(gridGraticule);
    
            break;
          case CIMMeasuredGrid:
            var gridMeasure = girdGraticuleObject as CIMMeasuredGrid;
            gridMeasure.Name = symbolItemName;
            gridMeasure.SetProjectedCoordinateSystem(mapFrame.Map.SpatialReference);
            //assign grid to the frame
            mapGrids.Add(gridMeasure);
    
            break;
          case CIMReferenceGrid:
            var gridReference = girdGraticuleObject as CIMReferenceGrid;
            gridReference.Name = symbolItemName;
            //assign grid to the frame
            mapGrids.Add(gridReference);
            break;
        }
        cmf.Grids = mapGrids.ToArray();
        mapFrame.SetDefinition(cmf);
      });
    }
    Zoom map frame to extent of a single layer
    {
      //Zoom map frame to the extent of a single layer.
      await QueuedTask.Run(() =>
      {
        //Reference map and layer
        Map m = mapFrame.Map;
        FeatureLayer lyr = m.FindLayers("GreatLakes").First() as FeatureLayer;
    
        //Set the map frame extent to all features in the layer
        mapFrame.SetCamera(lyr, false);
      });
    }
    Change map frame extent to selected features in multiple layers
    {
      //Change the extent of a map frame to the selected features multiple layers.
      await 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
      await 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);
            }
          }
        }
      });
    }
    SetAutoCameraFixedExtent
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.Fixed;
      autoCamera.AutoCameraType = AutoCameraType.Extent;
    
      var mf_extent = mapFrame.GetViewExtent();
    
      var extent = EnvelopeBuilderEx.CreateEnvelope(
        400748.62, 800296.4, 1310669.05, 1424520.74, mapFrame.Map.SpatialReference);
      autoCamera.Extent = extent;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
      {
        mapFrame.SetAutoCamera(autoCamera);
      }
    }
    SetAutoCameraFixedCenter
    {
      var activeLayoutView = LayoutView.Active;
      await QueuedTask.Run(() =>
      {
        var autoCamera = mapFrame.GetAutoCamera();
        autoCamera.Source = AutoCameraSource.Fixed;
        autoCamera.AutoCameraType = AutoCameraType.Center;
    
        var camera = mapFrame.GetMapView(activeLayoutView).Camera;
        var center = mapFrame.GetViewCenter();
    
      //var extent = EnvelopeBuilderEx.CreateEnvelope(
      //	400748.62, 800296.4, 1310669.05, 1424520.74, mf.Map.SpatialReference);
      //autoCamera.Extent = extent;
    
        var camera2 = new CIMViewCamera()
        {
          Heading = 0,
          Pitch = -90,
          Roll = 0,
          Scale = 21169571,
          X = 855708,
          Y = 1112409,
          Z = double.NaN
        };
        autoCamera.Camera = camera2;
        var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
        autoCamera.IntersectLayerPath = states.URI;
    
        if (mapFrame.ValidateAutoCamera(autoCamera) &&
          !mapFrame.IsMapSeriesMapFrame())
        {
          mapFrame.SetAutoCamera(autoCamera);
        }
      });
    }
    SetAutoCameraFixedCenterAndScale
    {
      var activeLayoutView = LayoutView.Active;
      await QueuedTask.Run(() =>
      {
        var autoCamera = mapFrame.GetAutoCamera();
        autoCamera.Source = AutoCameraSource.Fixed;
        autoCamera.AutoCameraType = AutoCameraType.CenterAndScale;
    
        var camera = mapFrame.GetMapView(activeLayoutView).Camera;
        var center = mapFrame.GetViewCenter();
    
      //var extent = EnvelopeBuilderEx.CreateEnvelope(
      //	400748.62, 800296.4, 1310669.05, 1424520.74, mf.Map.SpatialReference);
      //autoCamera.Extent = extent;
    
        var camera2 = new CIMViewCamera()
        {
          Heading = 0,
          Pitch = -90,
          Roll = 0,
          Scale = 21169571,
          X = 1310669.0 + ((400748.5 - 1310669.0) / 2.0),
          Y = 800296.4 + ((1424520.74 - 800296.4) / 2.0),
          Z = double.NaN
        };
        autoCamera.Camera = camera2;
        var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
        //autoCamera.IntersectLayerPath = states.URI;
        if (mapFrame.ValidateAutoCamera(autoCamera) &&
          !mapFrame.IsMapSeriesMapFrame())
        {
          mapFrame.SetAutoCamera(autoCamera);
        }
      });
    }
    SetAutoCameraFixedScale
    {
      var activeLayoutView = LayoutView.Active;
      await QueuedTask.Run(() =>
      {
        var autoCamera = mapFrame.GetAutoCamera();
        autoCamera.Source = AutoCameraSource.Fixed;
        autoCamera.AutoCameraType = AutoCameraType.Scale;
    
        var camera = mapFrame.GetMapView(activeLayoutView).Camera;
        var center = mapFrame.GetViewCenter();
    
      //var extent = EnvelopeBuilderEx.CreateEnvelope(
      //	400748.62, 800296.4, 1310669.05, 1424520.74, mf.Map.SpatialReference);
      //autoCamera.Extent = extent;
    
        var camera2 = new CIMViewCamera()
        {
          Heading = 0,
          Pitch = -90,
          Roll = 0,
          Scale = 20000571,
          X = 1310669.0 + ((400748.5 - 1310669.0) / 2.0),
          Y = 800296.4 + ((1424520.74 - 800296.4) / 2.0),
          Z = double.NaN
        };
        autoCamera.Camera = camera2;
        var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
        //autoCamera.IntersectLayerPath = states.URI;
    
        if (mapFrame.ValidateAutoCamera(autoCamera) &&
          !mapFrame.IsMapSeriesMapFrame())
        {
          mapFrame.SetAutoCamera(autoCamera);
        }
      });
    }
    SetAutoCameraLinkedCenter
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.Center;
      autoCamera.MapFrameLinkName = "mapFrameLink";
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
      {
        mapFrame.SetAutoCamera(autoCamera);
      }
    }
    SetAutoCameraLinkedCenterAndScale
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.CenterAndScale;
      autoCamera.MapFrameLinkName = "mapFrameLink";
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
      {
        mapFrame.SetAutoCamera(autoCamera);
      }
    }
    SetAutoCameraLinkedScale
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.Scale;
      autoCamera.MapFrameLinkName = "mapFrameLink";
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
      {
        mapFrame.SetAutoCamera(autoCamera);
      }
    }
    SetAutoCameraLinkedMapSeriesShape
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapSeriesLink;
      autoCamera.AutoCameraType = AutoCameraType.Extent;
      //autoCamera.MapFrameLinkName = mapFrameLink;
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
      {
        mapFrame.SetAutoCamera(autoCamera);
      }
    }
    SetAutoCameraLinkedMapSeriesCenter
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapSeriesLink;
      autoCamera.AutoCameraType = AutoCameraType.Center;
    
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
      {
        mapFrame.SetAutoCamera(autoCamera);
      }
    }
    Requirements

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

    ArcGIS Pro version: 3.0 or higher.
    See Also