Performing basic map functions


Summary
This topic contains code examples demonstrating a few of the basic ways in which to work with a map.


Zooming in on the map

The following code example shows how to zoom in on the map in the active view:
[C#]
public void ZoomInCenter(IApplication application)
{
    IMxDocument mxDocument = application.Document as IMxDocument;

    // Try the following different options to see how the code works in data or layout view:
    IActiveView activeView = mxDocument.ActiveView;
    //Dim activeView As IActiveView = mxDocument.PageLayout
    //Dim activeView As IActiveView = mxDocument.FocusMap

    // Get an envelope corresponding to the view extent.
    IEnvelope envelope = activeView.Extent;

    // Get the center point of the envelope.
    IPoint centerPoint = new PointClass();
    centerPoint.X = ((envelope.XMax - envelope.XMin) / 2) + envelope.XMin;
    centerPoint.Y = ((envelope.YMax - envelope.YMin) / 2) + envelope.YMin;

    //Resize the envelope width.
    envelope.Width = envelope.Width / 2;

    //Resize the envelope height.
    envelope.Height = envelope.Height / 2;

    //Move the envelope to the center point.
    envelope.CenterAt(centerPoint);

    //Set the envelope to view extent.
    activeView.Extent = envelope;
    activeView.Refresh();
}
[VB.NET]
Public Sub ZoomInCenter(ByVal application As IApplication)
    
    Dim mxDocument As IMxDocument = TryCast(application.Document, IMxDocument)
    
    ' Try the following different options to see how the code works in data or layout view:
    Dim activeView As IActiveView = mxDocument.ActiveView
    'Dim activeView As IActiveView = mxDocument.PageLayout
    'Dim activeView As IActiveView = mxDocument.FocusMap
    
    ' Get an envelope corresponding to the view extent.
    Dim envelope As IEnvelope = activeView.Extent
    
    ' Get the center point of the envelope.
    Dim centerPoint As IPoint = New PointClass()
    centerPoint.X = ((envelope.XMax - envelope.XMin) / 2) + envelope.XMin
    centerPoint.Y = ((envelope.YMax - envelope.YMin) / 2) + envelope.YMin
    
    'Resize the envelope width.
    envelope.Width = envelope.Width / 2
    
    'Resize the envelope height.
    envelope.Height = envelope.Height / 2
    
    'Move the envelope to the center point.
    envelope.CenterAt(centerPoint)
    
    'Set the envelope to view extent.
    activeView.Extent = envelope
    activeView.Refresh()
    
End Sub

Listening to map events

The following code example demonstrates listening to map events:
[C#]
private IMapEvents_FeatureClassChangedEventHandler dFeatClsChangedE;
private IMapEvents_VersionChangedEventHandler dVerChangedE;

private void ListenToMapEvents(IApplication application)
{
    IDocument document = application.Document;
    IMapDocument mapDoc = document as IMapDocument;
    IActiveView activeView = mapDoc.ActiveView;
    IMap map = activeView.FocusMap;

    IMapEvents_Event mapEvents = map as IMapEvents_Event;

    //Initialize the delegate to point to a function where you respond to the event being raised.
    dFeatClsChangedE = new IMapEvents_FeatureClassChangedEventHandler
        (OnFeatureClassChangedFunction);
    dVerChangedE = new IMapEvents_VersionChangedEventHandler
        (OnVersionChangedFunction);

    mapEvents.VersionChanged += dVerChangedE;

    // Wire the delegate to the FeatureClassChanged event of the mapEvents variable.
    mapEvents.FeatureClassChanged += dFeatClsChangedE;
}

private void OnFeatureClassChangedFunction(IFeatureClass oldClass, IFeatureClass
    newClass)
{
    // Listen to the FeatureClassChanged event of IMapEvents.
    MessageBox.Show("Feature Class changed");
}

private void OnVersionChangedFunction(IVersion oldVersion, IVersion newVersion)
{
    MessageBox.Show("Version Changed");
}
[VB.NET]
Private dFeatClsChangedE As IMapEvents_FeatureClassChangedEventHandler
Private dVerChangedE As IMapEvents_VersionChangedEventHandler

Private Sub ListenToMapEvents(ByVal application As IApplication)
    
    Dim document As IDocument = application.Document
    Dim mapDoc As IMapDocument = CType(document, IMapDocument)
    Dim activeView As IActiveView = mapDoc.ActiveView
    Dim map As IMap = activeView.FocusMap
    
    Dim mapEvents As IMapEvents_Event = TryCast(map, IMapEvents_Event)
    
    'Initialize the delegate to point to a function where you respond to the event being raised.
    dFeatClsChangedE = New IMapEvents_FeatureClassChangedEventHandler(AddressOf OnFeatureClassChangedFunction)
    dVerChangedE = New IMapEvents_VersionChangedEventHandler(AddressOf OnVersionChangedFunction)
    
    AddHandler mapEvents.VersionChanged, dVerChangedE
    
    ' Wire the delegate to the FeatureClassChanged event of the mapEvents variable.
    AddHandler mapEvents.FeatureClassChanged, dFeatClsChangedE
    
End Sub

Private Sub OnFeatureClassChangedFunction(ByVal oldClass As IFeatureClass, ByVal newClass As IFeatureClass)
    
    ' Listen to the FeatureClassChanged event of IMapEvents.
    MessageBox.Show("Feature Class changed")
    
End Sub

Private Sub OnVersionChangedFunction(ByVal oldVersion As IVersion, ByVal newVersion As IVersion)
    
    MessageBox.Show("Version Changed")
    
End Sub

Loading a table

The following code example loads a table into the focus map:
[C#]
public void AddTable(IMap map, IMxDocument mxDocument)
{
    ITableCollection tableCollection = map as ITableCollection;
    string tablePathName = 
        "C:\\Program Files\\ArcGIS\\DeveloperKit10.0\\Samples\\data\\Y2000HurricaneData";
    string tableName = "2000_hrcn.dbf";
    ITable table = OpenTable(tablePathName, tableName);

    if (table == null)
    {
        return ;
    }
    else
    {
        tableCollection.AddTable(table);
        mxDocument.UpdateContents();
    }
}

public ITable OpenTable(string pathName, string tableName)
{
    // Create the workspace name object.
    IWorkspaceName workspaceName = new WorkspaceNameClass();
    workspaceName.PathName = pathName;
    workspaceName.WorkspaceFactoryProgID = 
        "esriDataSourcesFile.shapefileworkspacefactory";

    // Create the table name object.
    IDatasetName dataSetName = new TableNameClass();
    dataSetName.Name = tableName;
    dataSetName.WorkspaceName = workspaceName;

    // Open the table.
    IName name = dataSetName as IName;
    ITable table = name.Open()as ITable;
    return table;
}
[VB.NET]
Public Sub AddTable(ByVal map As IMap, ByVal mxDocument As IMxDocument)
    
    Dim tableCollection As ITableCollection = TryCast(map, ITableCollection)
    Dim tablePathName As String = "C:\Program Files\ArcGIS\DeveloperKit10.0\Samples\data\Y2000HurricaneData"
    Dim tableName As String = "2000_hrcn.dbf"
    Dim table As ITable = OpenTable(tablePathName, tableName)
    
    If (table Is Nothing) Then
        Return
    Else
        tableCollection.AddTable(table)
        mxDocument.UpdateContents()
    End If
    
End Sub

Public Function OpenTable(ByVal pathName As String, ByVal tableName As String) As ITable
    
    ' Create the workspace name object.
    Dim workspaceName As IWorkspaceName = New WorkspaceNameClass()
    workspaceName.PathName = pathName
    workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileworkspacefactory"
    
    ' Create the table name object.
    Dim dataSetName As IDatasetName = New TableNameClass()
    dataSetName.Name = tableName
    dataSetName.WorkspaceName = workspaceName
    
    ' Open the table.
    Dim Name As IName = TryCast(dataSetName, IName)
    Dim table As ITable = TryCast(Name.Open(), ITable)
    Return table
    
End Function






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced