ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / MapView Class / Extent Property
Example

In This Topic
    Extent Property (MapView)
    In This Topic
    Gets the current extent of the view.
    Syntax
    public Envelope Extent {get;}
    Public ReadOnly Property Extent As Envelope
    Remarks
    Use this in combination with ZoomTo(Geometry,Nullable<TimeSpan>,Boolean) to get and set the view's extent.
    Example
    Set Grid Origin for Grid Options for a Map
    {
      //assume
      //var map = MapView.Active.Map;
      var center_pt = mapView.Extent.Center;
      var width = mapView.Extent.Width;
      var height = mapView.Extent.Height;
      var qtr_width = width * 0.25;
      var qtr_ht = height * 0.25;
    
      await QueuedTask.Run(() =>
      {
        //If grid options have not been set for the map, then default grid options are added
        //for the map and returned otherwise the existing gridoptions are returned.
        //NOTE: Grid options are by value meaning a copy is returned. Any changes made to the
        //grid options need to be -set- back on the map using SetGridOptions for the changes to persist.
        var gridOptions = GridOptionsEnvironment.Instance.GetGridOptions(map);
    
        //Set the grid origin
        var x1 = center_pt.X + qtr_width;
        var y1 = center_pt.Y + qtr_ht;
        gridOptions.SetGridOrigin(new Coordinate2D(x1, y1));
        //or use Coordinate3D
        gridOptions.SetGridOrigin(new Coordinate3D(x1, y1, 0));
    
        //To apply changes, the gridoptions must be set back on the map
        GridOptionsEnvironment.Instance.SetGridOptions(map, gridOptions);
      });
    }
    Set Various Grid Option parameters for GridOptions for a Map
    {
      var center_pt = mapView.Extent.Center;
      var width = mapView.Extent.Width;
      var height = mapView.Extent.Height;
      var qtr_width = width * 0.25;
      var qtr_ht = height * 0.25;
    
      await QueuedTask.Run(() =>
      {
        //use defaults
        var gridOptions = GridOptionsEnvironment.Instance.DefaultGridOptions;
    
        //customize the options as needed
        gridOptions.RotateWithMap = !gridOptions.RotateWithMap;
        gridOptions.GridLineColor = ColorFactory.Instance.RedRGB;
    
        var gs_rotation = gridOptions.GetGridRotation();
        gridOptions.SetGridRotation(gs_rotation * 1.5);
    
        var gs_meters = gridOptions.GetGridSpacingMeters();
        var gs_in_spacing_units = gridOptions.GetGridSpacing();
        var gs_units = gridOptions.GridSpacingUnits;
    
        //meters assumed
        gridOptions.SetGridSpacingInMeters(gs_meters + 10);
        //specify units explicitly
        gridOptions.SetGridSpacing(1000, LinearUnit.Feet);//internally will be converted to meters
    
        gridOptions.IsGridLines = true;//use lines (default)
    
        var grid_interval = gridOptions.GridMajorInterval;
    
        gridOptions.SnapToGridEnabled = true;
    
        var x1 = center_pt.X - qtr_width;
        var y1 = center_pt.Y + qtr_ht;
        gridOptions.SetGridOrigin(new Coordinate2D(x1, y1));
        //To apply changes, the gridoptions must be set back on the map
        GridOptionsEnvironment.Instance.SetGridOptions(map, gridOptions);
      });
    }
    Change the layer selection color - using CIM
    {
      await QueuedTask.Run(() =>
      {
        // get the CIM definition of the layer
        var layerDef = featureLayer.GetDefinition() as ArcGIS.Core.CIM.CIMBasicFeatureLayer;
      // disable the default symbol
      layerDef.UseSelectionSymbol = false;
      // assign a new color
      layerDef.SelectionColor = ColorFactory.Instance.RedRGB;
      // apply the definition to the layer
      featureLayer.SetDefinition(layerDef);
    
      if (!featureLayer.IsVisible)
      {
        featureLayer.SetVisibility(true);
      }
      //Do a selection
        mapView.SelectFeatures(mapView.Extent);
      });
    }
    Change the layer selection color - using Layer methods
    {
      await QueuedTask.Run(() =>
      {
        if (featureLayer.SupportsCustomSelectionProperties())
        {
        // get the set of properties
        var (useCustomSelectioncolur, customSelectionColor, usecustomSelectioinFilleColor, customSelectionFillColor) = featureLayer.GetCustomSelectionProperties();
    
        // set the layer to use a specific color
        featureLayer.SetCustomSelectionColor(ColorFactory.Instance.RedRGB);
    
        // or set a specific color AND fill color
        featureLayer.SetCustomSelectionColor(ColorFactory.Instance.RedRGB, ColorFactory.Instance.BlueRGB);
    
        // reset to use the default selection color
          featureLayer.SetUseDefaultSelectionColor();
        }
    
        if (!featureLayer.IsVisible)
        {
          featureLayer.SetVisibility(true);
        }
        //Do a selection
        mapView.SelectFeatures(mapView.Extent);
      });
    }
    Get Z values from a layer
    {
      await QueuedTask.Run(() =>
      {
        TinLayer tinLayer = map.GetLayersAsFlattenedList().OfType<TinLayer>().FirstOrDefault();
        MapPoint mapPoint = map.CalculateFullExtent().Center; //Any Map Point
        Polyline polyline = PolylineBuilderEx.CreatePolyline(mapView.Extent); //Any Polyline
    
        if (tinLayer.CanGetZs())
        {
          // get z value for a mapPoint
          var zResult = tinLayer.GetZs(mapPoint);
          if (zResult.Status == SurfaceZsResultStatus.Ok)
          {
            // cast to a mapPoint
            var mapPointZ = zResult.Geometry as MapPoint;
            var z = mapPointZ.Z;
          }
    
          // get z values for a polyline
          zResult = tinLayer.GetZs(polyline);
          if (zResult.Status == SurfaceZsResultStatus.Ok)
          {
            // cast to a mapPoint
            var polylineZ = zResult.Geometry as Polyline;
          }
        }
      });
    }
    Export the contents of a scene to an exchange format such as glTF and STL.
    {
      // Validate the current active view. Only a local scene can be exported.
      bool CanExportScene3DObjects = MapView.Active?.ViewingMode == MapViewingMode.SceneLocal;
      if (CanExportScene3DObjects)
      {
        // Create a scene content export format, export the scene context as a glTF file
        var exportFormat = new ExportSceneContentsFormat()
        {
          Extent = mapView.Extent, // sets Extent property
          FolderPath = @"C:\Temp", // sets FolderPath property
                                   //sets FileName property.The export format is determined by the output file extension (e.g.,.stl, .gltf)
          FileName = "my-3d-objects.gltf",
          IsSingleFileOutput = true, // sets whether to export to one single file
          SaveTextures = true //sets whether to save textures
        };
        // Export the scene content as 3D objects
        mapView.ExportScene3DObjects(exportFormat);
      }
    }
    Expand Extent
    {
      await QueuedTask.Run(() =>
      {
        //Expand the current extent by the given ratio.
        var extent = mapView.Extent;
        var newExtent = GeometryEngine.Instance.Expand(extent, dx, dy, true);
        mapView.ZoomTo(newExtent);
      });
    }
    Graphic Overlay
    {
      // get the current MapView and point
      var myextent = mapView.Extent;
      var point = myextent.Center;
      IDisposable _graphic = null;
    
      // add point graphic to the overlay at the center of the mapView
      await QueuedTask.Run(() =>
      {
        _graphic = mapView.AddOverlay(point,
          SymbolFactory.Instance.ConstructPointSymbol(
                  ColorFactory.Instance.RedRGB, 30.0, SimpleMarkerStyle.Star).MakeSymbolReference());
      });
    
      // update the overlay with new point graphic symbol
      MessageBox.Show("Now to update the overlay...");
      await QueuedTask.Run(() =>
      {
        mapView.UpdateOverlay(_graphic, point, SymbolFactory.Instance.ConstructPointSymbol(
                                      ColorFactory.Instance.BlueRGB, 20.0, SimpleMarkerStyle.Circle).MakeSymbolReference());
      });
    
      // clear the overlay display by disposing of the graphic
      MessageBox.Show("Now to clear the overlay...");
      _graphic.Dispose();
    }
    Point Graphic Element using CIMGraphic
    {
      await QueuedTask.Run(() =>
      {
        //Place symbol in the center of the map
        var extent = mapView.Extent;
        var location = extent.Center;
    
        //specify a symbol
        var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
                              ColorFactory.Instance.GreenRGB);
    
        //create a CIMGraphic
        var graphic = new CIMPointGraphic()
        {
          Symbol = pt_symbol.MakeSymbolReference(),
          Location = location //center of map
        };
        graphicsLayer.AddElement(graphic);
      });
    }
    Line Graphic Element using CIMGraphic
    {
      await QueuedTask.Run(() =>
      {
        //Place a line symbol using the extent's lower left and upper right corner.
        var extent = mapView.Extent;
        //get the lower left corner of the extent
        var pointFromCoordinates = new Coordinate2D(extent.XMin, extent.YMin);
        //get the upper right corner of the extent
        var pointToCoordinates = new Coordinate2D(extent.XMax, extent.YMax);
        List<Coordinate2D> points = new List<Coordinate2D> { pointFromCoordinates, pointToCoordinates };
        //create the polyline
        var lineSegment = PolylineBuilderEx.CreatePolyline(points);
    
        //specify a symbol
        var line_symbol = SymbolFactory.Instance.ConstructLineSymbol(
                              ColorFactory.Instance.GreenRGB);
        //create a CIMGraphic
        var graphic = new CIMLineGraphic()
        {
          Symbol = line_symbol.MakeSymbolReference(),
          Line = lineSegment,
        };
        graphicsLayer.AddElement(graphic);
      });
    }
    Polygon Graphic Element using CIMGraphic
    {
      await QueuedTask.Run(() =>
      {
        //Place a polygon symbol using the mapview extent geometry
        var extent = mapView.Extent;
        //Contract the extent
        var polygonEnv = extent.Expand(-100000, -90000, false);
        //create a polygon using the envelope
        var polygon = PolygonBuilderEx.CreatePolygon(polygonEnv);
    
        //specify a symbol
        var poly_symbol = SymbolFactory.Instance.ConstructPolygonSymbol(
                              ColorFactory.Instance.GreenRGB);
    
        //create a CIMGraphic
        var graphic = new CIMPolygonGraphic()
        {
          Symbol = poly_symbol.MakeSymbolReference(),
          Polygon = polygon,
        };
        graphicsLayer.AddElement(graphic);
      });
    }
    Multi-point Graphic Element using CIMGraphic
    {
      await QueuedTask.Run(() =>
      {
        //Place a multipoint graphic using the mapview extent geometry
        var extent = mapView.Extent;
        //Contract the extent
        var polygonEnv = extent.Expand(-100000, -90000, false);
        //create a polygon using the envelope
        var polygon = PolygonBuilderEx.CreatePolygon(polygonEnv);
        //Create MultipPoints from the polygon
        var multiPoints = MultipointBuilderEx.CreateMultipoint(polygon);
        //specify a symbol
        var point_symbol = SymbolFactory.Instance.ConstructPointSymbol(
                              ColorFactory.Instance.GreenRGB);
        //create a CIMGraphic
        var graphic = new CIMMultipointGraphic
        {
          Symbol = point_symbol.MakeSymbolReference(),
          Multipoint = multiPoints
        };
        graphicsLayer.AddElement(graphic);
      });
    }
    Graphic Element using CIMSymbol
    {
      await QueuedTask.Run(() =>
      {
        //Place symbol in the center of the map
        var extent = mapView.Extent;
        var location = extent.Center;
    
        //specify a symbol
        var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
                              ColorFactory.Instance.GreenRGB);
        graphicsLayer.AddElement(location, pt_symbol);
      });
    }
    Text Graphic Element
    {
      await QueuedTask.Run(() =>
      {
        //Place symbol in the center of the map
        var extent = mapView.Extent;
        var location = extent.Center;
    
        //specify a text symbol
        var text_symbol = SymbolFactory.Instance.ConstructTextSymbol
        (ColorFactory.Instance.BlackRGB, 8.5, "Corbel", "Regular");
    
        graphicsLayer.AddElement(location, text_symbol, "Text Example");
      });
    }
    Spatial selection of elements in one graphics layer
    {
      await QueuedTask.Run(() =>
      {
        //Create an extent to use for the spatial selection
        var extent = mapView.Extent;
        var selectionExtent = extent.Expand(0.5, 0.5, true);
        //Select elements in specified graphics layer using the selection extent.
        var selectedElements = mapView.SelectElements(graphicsLayer, selectionExtent, SelectionCombinationMethod.Add);
      });
    }
    Requirements

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

    ArcGIS Pro version: 3.0 or higher.
    See Also