Build a map layout

This tutorial demonstrates how to create a custom map layout using the ArcGIS Pro SDK for .NET.

With the ArcGIS Pro SDK for .NET, you can extend ArcGIS Pro with your own unique tools and workflows. Using Microsoft Visual Studio and the Pro SDK, developers can build Pro add-ins and solution configurations that provide users with custom functionality specific to their organization or industry.

The ArcGIS Pro SDK for .NET includes a Map Layout API, which provides classes and members to support managing layouts, layout elements, and working with layout views. You can create new layouts and layout elements, modify existing elements, and manage selections, layout view control, and navigation.

In this tutorial, you will create a new map layout, add layout elements including a map frame and text elements, and then open the layout in a new layout view.

Prerequisites

Steps

Create a new ArcGIS Pro Add-In Visual Studio Project

  1. Start Visual Studio.

  2. Choose File > New > Project and then from the ArcGIS templates group, select ArcGIS Pro Module Add-in. Name the add-in project "MapLayout".

Create a new Pro button and edit DAML

  1. Right-click the project and choose Add > New Item. From the ArcGIS Pro Add-ins group > list of templates, select ArcGIS Pro Button.

  2. Name the new class file "BuildNewLayout.cs" and press Add to close the dialog box.

  3. Open the Config.daml file and modify the button item as follows:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
         <group id="MapLayout_Group1" caption="Layout Tools" appearsOnAddInTab="true">
           <!-- host controls within groups -->
           <button refID="MapLayout_BuildNewLayout" size="large" />
         </group>
       </groups>
       <controls>
         <!-- add your controls here -->
         <button id="MapLayout_BuildNewLayout" caption="Build New Layout"
           className="BuildNewLayout" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png">
           <tooltip heading="Build New Layout">Create a new map layout with layout elements.<disabledText /></tooltip>
         </button>

Create and set up a new layout page

  1. Open the BuildNewLayout.cs file and add the following using directives:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
     using ArcGIS.Core.CIM;
     using ArcGIS.Core.Geometry;
     using ArcGIS.Desktop.Core;
     using ArcGIS.Desktop.Framework;
     using ArcGIS.Desktop.Framework.Contracts;
     using ArcGIS.Desktop.Framework.Threading.Tasks;
     using ArcGIS.Desktop.Layouts;
     using ArcGIS.Desktop.Mapping;
  2. In the BuildNewLayout.cs file, update the code to match the following for the OnClick method. This creates a new layout and CIM page and applies it to the layout:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
             // Create a layout which will be returned from the QueuedTask
             Layout newLayout = await QueuedTask.Run<Layout>(() =>
             {
                 // Create a new CIM page
                 CIMPage newPage = new CIMPage();
    
                 // Add properties
                 newPage.Width = 17;
                 newPage.Height = 11;
                 newPage.Units = LinearUnit.Inches;
    
                 // Add rulers
                 newPage.ShowRulers = true;
                 newPage.SmallestRulerDivision = 0.5;
    
                 // Apply the CIM page to a new layout and set name
                 newLayout = LayoutFactory.Instance.CreateLayout(newPage);
                 newLayout.SetName("Census Data");

Create a map and map elements and add to the layout

You will continue adding code to the OnClick method.

  1. Next you will add code to create a map with census data, add it to the layout, and set the camera. Update the code to match the following:
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
             // Create a new map with an ArcGIS Online basemap
             Map newMap = MapFactory.Instance.CreateMap("Census Map", MapType.Map, MapViewingMode.Map, Basemap.NationalGeographic);
             string url = @"https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer";
             Uri uri = new Uri(url);
             LayerFactory.Instance.CreateLayer(uri, newMap);

             // Build a map frame geometry / envelope
             Coordinate2D ll = new Coordinate2D(1, 0.5);
             Coordinate2D ur = new Coordinate2D(13, 9);
             Envelope mapEnv = EnvelopeBuilderEx.CreateEnvelope(ll, ur);

             // Create a map frame and add it to the layout
             MapFrame newMapframe = ElementFactory.Instance.CreateMapFrameElement(newLayout, mapEnv, newMap);
             newMapframe.SetName("Map Frame");

             // Create and set the camera
             Camera camera = newMapframe.Camera;
             camera.X = -118.465;
             camera.Y = 33.988;
             camera.Scale = 30000;
             newMapframe.SetCamera(camera);
  1. Continue adding code to the OnClick method. This code creates a title and North arrow:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
                 // Add text for title
                 Coordinate2D titleTxt_ll = new Coordinate2D(4.5, 9.5);
                 CIMTextSymbol arial36bold = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlueRGB, 80, "Arial", "Bold");
                 GraphicElement titleTxtElm = ElementFactory.Instance.CreateTextGraphicElement
                     (newLayout, TextType.PointText, titleTxt_ll.ToMapPoint(), arial36bold, "Census Data", "Title");
                 titleTxtElm.SetName("Title");
    
                 // Add north arrow
                 // Reference a North Arrow in a style
                 StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name == "ArcGIS 2D");
                 NorthArrowStyleItem naStyleItm = stylePrjItm.SearchNorthArrows("ArcGIS North 8")[0];
    
                 // Set the center coordinate and create the arrow on the layout
                 Coordinate2D center = new Coordinate2D(15, 7);
                 var naInfo = new NorthArrowInfo() { MapFrameName = newMapframe.Name, NorthArrowStyleItem = naStyleItm };
                 var arrowElm = ElementFactory.Instance.CreateMapSurroundElement(newLayout, center.ToMapPoint(), naInfo);
    
                 arrowElm.SetName("New North Arrow");
                 arrowElm.SetHeight(2);

Finalize code and test

In this step, you continue to use the ElementFactory to create and add new map elements to your layout.

  1. Finalize the OnClick method with additional code creating a map legend and then opening the layout pane.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
                 // Add legend - first build 2D envelope geometry
                 Coordinate2D leg_ll = new Coordinate2D(13.5, 0.5);
                 Coordinate2D leg_ur = new Coordinate2D(16.5, 4.0);
                 Envelope leg_env = EnvelopeBuilderEx.CreateEnvelope(leg_ll, leg_ur);
    
                 // Create legend
                 var legInfo = new LegendInfo() { MapFrameName = newMapframe.Name };
                 var legendElm = ElementFactory.Instance.CreateMapSurroundElement(newLayout, leg_env, legInfo);
    
                 legendElm.SetVisible(true);
                 legendElm.SetName("New Legend");
    
                 return newLayout;
              });
    
             var layoutPane = await ProApp.Panes.CreateLayoutPaneAsync(newLayout);
  2. Once your code is completed, build your project and debug any issues. Start the project, which will launch ArcGIS Pro.

  1. Create a new project when the ArcGIS Pro start page appears. From the Add-in tab and find your new button.

  2. Press your new button to run the code and open the new layout.

What's Next?

Learn how to use additional ArcGIS Pro SDK for .NET features in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.