ArcGIS Pro 3.7 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 Function GetMap() As Map
    public Map GetMap()

    Return Value

    A Map
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    How to access Geoprocessing History
    {
      string openProjectPath = @"D\DATA\IGPHistoryItemTestProject\IGPHistoryItemTestProject.aprx";
      Project.OpenAsync(openProjectPath);
      MapProjectItem mapProjItem = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Equals("Map", StringComparison.CurrentCultureIgnoreCase));
    
      // Note: Needs QueuedTask to run
      var map = mapProjItem.GetMap();
      var ftrLayer = map.Layers[0] as FeatureLayer;
      string tool1 = "management.GetCount";
      var args1 = Geoprocessing.MakeValueArray(ftrLayer);
      var env = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
      GPExecuteToolFlags executeFlags = GPExecuteToolFlags.AddToHistory;
      var t = Geoprocessing.ExecuteToolAsync(tool1, args1, env, null, null, executeFlags);
    
      IEnumerable<IGPHistoryItem> hisItems = Project.Current.GetProjectItemContainer(Geoprocessing.HistoryContainerKey) as IEnumerable<IGPHistoryItem>;
    
      string hitemID = "";
      string hitemToolPath = "";
      IGPResult hitemGPResult = null;
      DateTime hitemTimeStamp;
    
      foreach (var hitem in hisItems)
      {
        // common IGPHistoryItem and Item properties
        hitemID = (hitem as Item).ID;
        hitemToolPath = hitem.ToolPath;
        hitemGPResult = hitem.GPResult;
        hitemTimeStamp = hitem.TimeStamp;
    
        // use the properties as needed
      }
    }
    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);
    }
    Find a map within a project and open it
    {
        //Finding the first project item with name matches with mapName
        MapProjectItem? mpi = Project.Current.GetItems<MapProjectItem>()
          .FirstOrDefault(m => m.Name.Equals("The Map", StringComparison.CurrentCultureIgnoreCase));
        //Note: Needs QueuedTask to run
        var mapFromItem = mpi?.GetMap();
        if (mapFromItem != null)
        {
            //Open the map in a new map pane
            //Must be called from the UI thread
            await ProApp.Panes.CreateMapPaneAsync(map);
        }
    }
    Requirements

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

    ArcGIS Pro version: 3.0 or higher.
    See Also