ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / MapProjectItem Class / GetMap Method
Example

In This Topic
    GetMap Method
    In This Topic
    Returns a map by loading it first if necessary. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public Map GetMap()
    Public Function GetMap() As Map

    Return Value

    A Map
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    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
    await QueuedTask.Run(() =>
    {
      //Build 2D envelope geometry
      Coordinate2D mf_ll = new Coordinate2D(6.0, 8.5);
      Coordinate2D mf_ur = new Coordinate2D(8.0, 10.5);
      //At 2.x - Envelope mf_env = EnvelopeBuilder.CreateEnvelope(mf_ll, mf_ur);
      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");
    
      //At 2.x - MapFrame mfElm =
      //                  LayoutElementFactory.Instance.CreateMapFrame(
      //                                             layout, mf_env, mfMap);
      //         mfElm.SetName("New Map Frame");
      //
      MapFrame mfElm = ElementFactory.Instance.CreateMapFrameElement(
                           layout, mf_env, mfMap, "New Map Frame");
    
      //Zoom to bookmark
      mfElm.SetCamera(bookmark);
    });
    Find a map within a project and open it
    public static async Task<Map> FindOpenExistingMapAsync(string mapName)
    {
      return await QueuedTask.Run(async () =>
      {
        Map map = null;
        Project proj = Project.Current;
    
        //Finding the first project item with name matches with mapName
        MapProjectItem mpi = proj.GetItems<MapProjectItem>()
          .FirstOrDefault(m => m.Name.Equals(mapName, StringComparison.CurrentCultureIgnoreCase));
        if (mpi != null)
        {
          map = mpi.GetMap();
          //Opening the map in a mapview
          await ProApp.Panes.CreateMapPaneAsync(map);
        }
        return map;
      });
    
    }
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also