Skip to content

ProGuide MapView Interaction

UmaHarano edited this page May 6, 2024 · 20 revisions
Language:      C#
Subject:       Map Exploration
Contributor:   ArcGIS Pro SDK Team <arcgisprosdk@esri.com>
Organization:  Esri, http://www.esri.com
Date:          04/04/2024
ArcGIS Pro:    3.3
Visual Studio: 2022

This guide demonstrates how to create a map tool for zoom in, zoom out. A left-click will center the map on the clicked location and zoom in, and a right-click will center the map on the clicked location and zoom out. It also shows how to override the mouse and keyboard events. Note that the map tool does not use sketching (IsSketchTool is not set in the constructor). See ProConcepts Map Exploration, MapTool for more information on the MapTool pattern.

Prerequisites

  • Download and install the sample data required for this guide as instructed in Arcgis Pro SDK Community Samples Releases.
  • Create a new ArcGIS Pro Module Add-in, and name the project MapToolZoom. If you are not familiar with the ArcGIS Pro SDK, you can follow the steps in the ProGuide Build your first add-in to get started.
  • Add a new ArcGIS Pro Add-ins | ArcGIS Pro Map Tool item to the add-in project, and name the item MapToolZoomInOut.
    3MapTool.png

Step 1

Modify the Config.daml file tool item as follows:

  • Change the caption to "Zoom In/Out".
  • Change the tool heading to "Zoom In/Out" and the ToolTip text to "Left click on the map to zoom in, or right-click on the map to zoom out."
...
<tool id="MapToolZoom_MapToolZoomInOut" 
      caption="Zoom In/Out" className="MapToolZoomInOut" 
      keytip="TZ" loadOnClick="true" 
      smallImage="GenericButtonRed16" largeImage="GenericButtonRed32" 
      condition="esri_mapping_mapPane">
  <tooltip heading="Zoom In/Out">
    Left click on the map to zoom in, or right-click on the map to zoom out.
    <disabledText />
  </tooltip>
</tool>
...

Build the sample and validate the UI on the ArcGIS Pro ribbon:

3Tooltip.png

Step 2

The required zoom in/out functionality must be implemented in the MapToolZoomInOut class.
First, remove all stubbed out code in the MapToolZoomInOut constructor since you are not using the sketch functionality of the MapTool base class.

public MapToolZoomInOut()
{
}

Next, add code to support zoom in/out. First, override OnToolMouseDown to signal to the ArcGIS Pro FrameworkApplication that MapToolZoomInOut will handle these mouse events. Then, override HandleMouseDownAsync to implement the actual handler for your mouse-controlled zoom in and out. In the handler, use the map point that was clicked as the new center (looked at) point of your map view, and zoom in or out depending on left or right mouse clicks.

protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
  // On mouse down, check if the mouse button pressed is:
  // the left mouse button to handle zoom in
  // or the right mouse button to handle zoom out.
  // If it is handle the event.
  switch (e.ChangedButton)
  {
    case MouseButton.Right:
      e.Handled = true;
      break;
    case MouseButton.Left:
      e.Handled = true;
      break;
  }
}

protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
  // Get the map coordinates from the click point and change the Camera to zoom in or out.
  return QueuedTask.Run(() =>
  {
    var mapClickPnt = MapView.Active.ClientToMap(e.ClientPoint);        
    ActiveMapView.LookAt(mapClickPnt, TimeSpan.FromSeconds(1));
    // zoom out
    if (e.ChangedButton == MouseButton.Right)
    {
      ActiveMapView.ZoomOutFixed(TimeSpan.FromSeconds(1));
    }
    // zoom in
    else if (e.ChangedButton == MouseButton.Left)
    {
      ActiveMapView.ZoomInFixed(TimeSpan.FromSeconds(1));
    }
  });
}

In addition to the mouse clicks, We will also support zoom in/out through the cursor keys. Add code to support this by overriding the OnToolKeyDown and HandleKeyDownAsync routines. Override OnToolKeyDown to signal to the ArcGIS Pro FrameworkApplication that MapToolZoomInOut will handle these keys when they are pressed. Override HandleKeyDownAsync to implement the actual handler for the key-controlled zoom in and out. In the handler, zoom in or out depending on the cursor key that was pressed.

protected override void OnToolKeyDown(MapViewKeyEventArgs k)
{
  // using key up and down in order to zoom out and in
  // if those keys are used we need to mark them as handled
  if (k.Key == Key.Up || k.Key == Key.Down)
    k.Handled = true;
  base.OnToolKeyDown(k);
}

protected override Task HandleKeyDownAsync(MapViewKeyEventArgs k)
{
  // only called when 'handled' in OnToolKeyDown
  if (k.Key == Key.Up)
  {
    // Key.Up => zoom out
    return ActiveMapView.ZoomOutFixedAsync(TimeSpan.FromSeconds(1));
  }
  // Key.Down => zoom in
  // else if (k.Key == Key.Down)
  {
    return ActiveMapView.ZoomInFixedAsync(TimeSpan.FromSeconds(1));
  }
}

Step 3

Rebuild the add-in. Fix any compilation errors.

Step 4

Debug the add-in. Run the debugger and start ArcGIS Pro. Open the C:\Data\Interacting with Maps\Interacting with Maps.aprx project that contains a 3D map with feature data.

Try the mouse click on the 2D map first. Verify the zoom.

Try the mouse click on key up and down on the 3D scene. Verify the zoom.

Developing with ArcGIS Pro

    Migration


Framework

    Add-ins

    Configurations

    Customization

    Styling


Arcade


Content


CoreHost


DataReviewer


Editing


Geodatabase

    3D Analyst Data

    Plugin Datasources

    Topology

    Object Model Diagram


Geometry

    Relational Operations


Geoprocessing


Knowledge Graph


Layouts

    Reports


Map Authoring

    3D Analyst

    CIM

    Graphics

    Scene

    Stream

    Voxel


Map Exploration

    Map Tools


Networks

    Network Diagrams


Parcel Fabric


Raster


Sharing


Tasks


Workflow Manager Classic


Workflow Manager


Reference

Clone this wiki locally