ArcObjects Library Reference  

GTMapForm

About the GraphicTracker with the map Sample

[C#]

GTMapForm.cs

#region using statements
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.EngineCore;
using ESRI.ArcGIS.SystemUI;
#endregion

namespace GraphicTrackerMap
{
  public partial class GTMapForm : Form
  {

      #region class private members
      private IMapControl3 m_mapControl = null;
      private string m_mapDocumentName = string.Empty;
      public IGraphicTracker m_graphicTracker = null;
      public Dictionary<int, IGraphicTrackerSymbol> m_GTSymbols = null;
      public Dictionary<int, IGeometry> m_GTGeometries = null;
      private Boolean m_move = false;
      private Boolean m_move_all = false;
      private double m_move_dist = 1;

      #endregion

      public GTMapForm()
    {
      InitializeComponent();
    }

      private void GTMapForm_Load(object sender, EventArgs e)
      {
          //Get the MapControl
          m_mapControl = (IMapControl3)axMapControl1.Object;

          //Initialize the GraphicTracker with the Map
          m_graphicTracker = new GraphicTrackerClass();
          m_graphicTracker.Initialize(m_mapControl.Map as IBasicMap);
          Calculate_Move_Distance();

          //Create dictionaries and store the 3 GraphicTracker Symbols
          m_GTGeometries = new Dictionary<int, IGeometry>(20);
          m_GTSymbols = new Dictionary<int, IGraphicTrackerSymbol>(3);
          CreateGTSymbols();

          //Add the custom Addgraphic and Selectgraphic tools to a ToolbarControl  
          axToolbarControl2.AddItem(new AddGT());
          axToolbarControl2.AddItem(new SelectGT());     

          //Form controls initialize
          radioButton1.Checked = true;
          GTUpdateTimer.Enabled = true;
          m_mapControl.CustomProperty = this as object;
      }

      private void btnHighlight_Click(object sender, EventArgs e)
      {
          if (comboTrackerID.SelectedIndex != -1 && m_graphicTracker != null)
              m_graphicTracker.Highlight(comboTrackerID.SelectedIndex, true);
      }

      private void btnUnHighlight_Click(object sender, EventArgs e)
      {
          if (comboTrackerID.SelectedIndex != -1 && m_graphicTracker != null)
              m_graphicTracker.Highlight(comboTrackerID.SelectedIndex, false);
      }

      private void comboSymbolID_SelectedIndexChanged(object sender, EventArgs e)
      {
          if (comboTrackerID.SelectedIndex != -1 && comboSymbolID.SelectedIndex != -1 && m_graphicTracker != null)
          {
              int id = comboTrackerID.SelectedIndex;
              if (m_GTGeometries.ContainsKey(id))
              {
                  m_graphicTracker.Replace(id, m_GTGeometries[id], m_GTSymbols[comboSymbolID.SelectedIndex]);
              }
          }
      }

      private void textLabel_TextChanged(object sender, EventArgs e)
      {
          if (comboTrackerID.SelectedIndex != -1 && m_graphicTracker != null)
          {
              int id = comboTrackerID.SelectedIndex;
              if (m_GTGeometries.ContainsKey(id))
              {
                  m_graphicTracker.SetLabel(id, textLabel.Text);
              }
          }
      }

      private void radioButton1_CheckedChanged(object sender, EventArgs e)
      {
          m_move = false;
          m_move_all = false;
      }

      private void radioButton3_CheckedChanged(object sender, EventArgs e)
      {
          m_move = false;

          if (m_move_all == false)
              m_move_all = true;
          else
              m_move_all = false;
      }

      private void radioButton2_CheckedChanged(object sender, EventArgs e)
      {
          m_move_all = false;

          if (m_move == false)
              m_move = true;
          else
              m_move = false;
      }

      private void CreateGTSymbols()
      {
          if (m_graphicTracker == null)
              return;

          //1st point symbol
          IColor color = new RgbColorClass() { Red = 0, Green = 0, Blue = 255 };
          stdole.IFontDisp symbolFont = new stdole.StdFontClass() as stdole.IFontDisp;
          symbolFont.Name = "ESRI Default Marker";
          ICharacterMarkerSymbol characterMarkerSymbol = new CharacterMarkerSymbolClass();
          characterMarkerSymbol.Font = symbolFont;
          characterMarkerSymbol.Color = color;
          characterMarkerSymbol.CharacterIndex = 98;//pin
          characterMarkerSymbol.Size = 20.0;
          IGraphicTrackerSymbol gtSymbol1 = m_graphicTracker.CreateSymbol(characterMarkerSymbol as ISymbol, null);
          m_GTSymbols.Add(0, gtSymbol1);
          comboSymbolID.Items.Add("Blue Pin");

          //2nd point symbol
          color = new RgbColorClass() { Red = 244, Green = 0, Blue = 0 };
          symbolFont.Name = "ESRI Default Marker";
          ICharacterMarkerSymbol characterMarkerSymbol2 = new CharacterMarkerSymbolClass();
          characterMarkerSymbol2.Font = symbolFont;
          characterMarkerSymbol2.Color = color;
          characterMarkerSymbol2.CharacterIndex = 111;//aeroplane
          characterMarkerSymbol2.Size = 70.0;
          IGraphicTrackerSymbol gtSymbol2 = m_graphicTracker.CreateSymbol(characterMarkerSymbol2 as ISymbol, null);
          m_GTSymbols.Add(1, gtSymbol2);
          comboSymbolID.Items.Add("Red Plane");

          //3rd point symbol
          color = new RgbColorClass() { Red = 244, Green = 255, Blue = 0 };
          ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
          simpleMarkerSymbol.Color = color;
          simpleMarkerSymbol.Size = 40;
          simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
          IGraphicTrackerSymbol gtSymbol3 = m_graphicTracker.CreateSymbol(simpleMarkerSymbol as ISymbol, null);
          m_GTSymbols.Add(2, gtSymbol3);
          comboSymbolID.Items.Add("Yellow Circle");
      }

      private void GTUpdateTimer_Tick(object sender, EventArgs e)
      {
          //To control the rate that the data is updated by setting the GTUpdateTimer.Interval property
          //This timer is used to artificially replicate changing data (e.g. GPS Feed)
          //Instead of using a timer you could provide an event that triggers an update

          if (m_graphicTracker == null)
              return;

          //Move the selected graphic
          if (m_move)
          {
              if (comboTrackerID.SelectedIndex != -1 && m_graphicTracker != null)
              {
                  int id = comboTrackerID.SelectedIndex;
                  if (m_GTGeometries.ContainsKey(id))
                  {
                      IPoint pnt = (IPoint)m_GTGeometries[id];
                      double x = pnt.X + m_move_dist;
                      double y = pnt.Y + m_move_dist;
                      m_graphicTracker.MoveTo(id, x, y, 0);

                      m_GTGeometries.Remove(id);

                      IPoint point = new PointClass();
                      point.PutCoords(x, y);
                      m_GTGeometries.Add(id, point);

                  }
              }
          }

          //Move all graphics
          if (m_move_all)
          {
              if (m_graphicTracker != null)
              {
                  for (int i = 0; i < m_graphicTracker.Count; ++i)
                  {
                      int id = i;
                      if (m_GTGeometries.ContainsKey(id))
                      {
                          IPoint pnt = (IPoint)m_GTGeometries[id];
                          double x = pnt.X + m_move_dist;
                          double y = pnt.Y + m_move_dist;
                          m_graphicTracker.MoveTo(id, x, y, 0);

                          m_GTGeometries.Remove(id);

                          IPoint point = new PointClass();
                          point.PutCoords(x, y);
                          m_GTGeometries.Add(id, point);

                      }
                  }
              }

          }

          if (m_graphicTracker.Count == comboTrackerID.Items.Count)
              return;

          comboTrackerID.Items.Clear();

          for (int i = 0; i < m_graphicTracker.Count; ++i)
              comboTrackerID.Items.Add(i);

      }

      private void Calculate_Move_Distance()
      {
          //Call this every time the map document is replaced
          //This move distance is set to simulate changing data location
          //Change the divisor to change the distance that the graphic moves
          //You may need to call this every time you zoom in/out but that will depend on your application
          m_move_dist = m_mapControl.Extent.Width / 500;
      }

      private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
      {
          //Every time you change the map document don't forget to initialize the GraphicTracker with
          //the Map. In this sample you must calculate the move distance again.
          m_graphicTracker.Initialize(m_mapControl.Map as IBasicMap);
          Calculate_Move_Distance();
      }
  }
}
[Visual Basic .NET]

GTMapForm.vb

#Region "using statements"
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.EngineCore
Imports ESRI.ArcGIS.SystemUI
#End Region

Public Partial Class GTMapForm
	Inherits Form

	#Region "class private members"
	Private m_mapControl As IMapControl3 = Nothing
	Private m_mapDocumentName As String = String.Empty
	Public m_graphicTracker As IGraphicTracker = Nothing
	Public m_GTSymbols As Dictionary(Of Integer, IGraphicTrackerSymbol) = Nothing
	Public m_GTGeometries As Dictionary(Of Integer, IGeometry) = Nothing
	Private m_move As [Boolean] = False
	Private m_move_all As [Boolean] = False
	Private m_move_dist As Double = 1

	#End Region

	Public Sub New()
		InitializeComponent()
	End Sub

    Private Sub GTMapForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Get the MapControl
        m_mapControl = DirectCast(axMapControl1.[Object], IMapControl3)

        'Initialize the GraphicTracker with the Map
        m_graphicTracker = New GraphicTrackerClass()
        m_graphicTracker.Initialize(TryCast(m_mapControl.Map, IBasicMap))
        Calculate_Move_Distance()

        'Create dictionaries and store the 3 GraphicTracker Symbols
        m_GTGeometries = New Dictionary(Of Integer, IGeometry)(20)
        m_GTSymbols = New Dictionary(Of Integer, IGraphicTrackerSymbol)(3)
        CreateGTSymbols()

        'Add the custom Addgraphic and Selectgraphic tools to a ToolbarControl  
        axToolbarControl2.AddItem(New AddGT())
        axToolbarControl2.AddItem(New SelectGT())

        'Form controls initialize
        radioButton1.Checked = True
        GTUpdateTimer.Enabled = True
        m_mapControl.CustomProperty = TryCast(Me, Object)
    End Sub



    Private Sub btnHighlight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnHighlight.Click
        If comboTrackerID.SelectedIndex <> -1 AndAlso m_graphicTracker IsNot Nothing Then
            m_graphicTracker.Highlight(comboTrackerID.SelectedIndex, True)
        End If
    End Sub

    Private Sub btnUnHighlight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUnHighlight.Click
        If comboTrackerID.SelectedIndex <> -1 AndAlso m_graphicTracker IsNot Nothing Then
            m_graphicTracker.Highlight(comboTrackerID.SelectedIndex, False)
        End If
    End Sub

    Private Sub comboSymbolID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboSymbolID.SelectedIndexChanged
        If comboTrackerID.SelectedIndex <> -1 AndAlso comboSymbolID.SelectedIndex <> -1 AndAlso m_graphicTracker IsNot Nothing Then
            Dim id As Integer = comboTrackerID.SelectedIndex
            If m_GTGeometries.ContainsKey(id) Then
                m_graphicTracker.Replace(id, m_GTGeometries(id), m_GTSymbols(comboSymbolID.SelectedIndex))
            End If
        End If
    End Sub

    Private Sub textLabel_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles textLabel.TextChanged
        If comboTrackerID.SelectedIndex <> -1 AndAlso m_graphicTracker IsNot Nothing Then
            Dim id As Integer = comboTrackerID.SelectedIndex
            If m_GTGeometries.ContainsKey(id) Then
                m_graphicTracker.SetLabel(id, textLabel.Text)
            End If
        End If
    End Sub

    Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioButton1.CheckedChanged
        m_move = False
        m_move_all = False
    End Sub

    Private Sub radioButton3_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioButton3.CheckedChanged
        m_move = False

        If m_move_all = False Then
            m_move_all = True
        Else
            m_move_all = False
        End If
    End Sub

    Private Sub radioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioButton2.CheckedChanged
        m_move_all = False

        If m_move = False Then
            m_move = True
        Else
            m_move = False
        End If
    End Sub

	Private Sub CreateGTSymbols()
		If m_graphicTracker Is Nothing Then
			Return
		End If

		'1st point symbol
		Dim color As IColor = New RgbColorClass() With { _
			.Red = 0, _
			.Green = 0, _
			.Blue = 255 _
		}
		Dim symbolFont As stdole.IFontDisp = TryCast(New stdole.StdFontClass(), stdole.IFontDisp)
		symbolFont.Name = "ESRI Default Marker"
		Dim characterMarkerSymbol As ICharacterMarkerSymbol = New CharacterMarkerSymbolClass()
		characterMarkerSymbol.Font = symbolFont
		characterMarkerSymbol.Color = color
		characterMarkerSymbol.CharacterIndex = 98
		'pin
		characterMarkerSymbol.Size = 20.0
		Dim gtSymbol1 As IGraphicTrackerSymbol = m_graphicTracker.CreateSymbol(TryCast(characterMarkerSymbol, ISymbol), Nothing)
		m_GTSymbols.Add(0, gtSymbol1)
		comboSymbolID.Items.Add("Blue Pin")

		'2nd point symbol
		color = New RgbColorClass() With { _
			.Red = 244, _
			.Green = 0, _
			.Blue = 0 _
		}
		symbolFont.Name = "ESRI Default Marker"
		Dim characterMarkerSymbol2 As ICharacterMarkerSymbol = New CharacterMarkerSymbolClass()
		characterMarkerSymbol2.Font = symbolFont
		characterMarkerSymbol2.Color = color
		characterMarkerSymbol2.CharacterIndex = 111
		'aeroplane
		characterMarkerSymbol2.Size = 70.0
		Dim gtSymbol2 As IGraphicTrackerSymbol = m_graphicTracker.CreateSymbol(TryCast(characterMarkerSymbol2, ISymbol), Nothing)
		m_GTSymbols.Add(1, gtSymbol2)
		comboSymbolID.Items.Add("Red Plane")

		'3rd point symbol
		color = New RgbColorClass() With { _
			.Red = 244, _
			.Green = 255, _
			.Blue = 0 _
		}
		Dim simpleMarkerSymbol As ISimpleMarkerSymbol = New SimpleMarkerSymbolClass()
		simpleMarkerSymbol.Color = color
		simpleMarkerSymbol.Size = 40
		simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle
		Dim gtSymbol3 As IGraphicTrackerSymbol = m_graphicTracker.CreateSymbol(TryCast(simpleMarkerSymbol, ISymbol), Nothing)
		m_GTSymbols.Add(2, gtSymbol3)
		comboSymbolID.Items.Add("Yellow Circle")
	End Sub

    Private Sub GTUpdateTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles GTUpdateTimer.Tick
        'To control the rate that the data is updated by setting the GTUpdateTimer.Interval property
        'This timer is used to artificially replicate changing data (e.g. GPS Feed)
        'Instead of using a timer you could provide an event that triggers an update

        If m_graphicTracker Is Nothing Then
            Return
        End If

        'Move the selected graphic
        If m_move Then
            If comboTrackerID.SelectedIndex <> -1 AndAlso m_graphicTracker IsNot Nothing Then
                Dim id As Integer = comboTrackerID.SelectedIndex
                If m_GTGeometries.ContainsKey(id) Then
                    Dim pnt As IPoint = DirectCast(m_GTGeometries(id), IPoint)
                    Dim x As Double = pnt.X + m_move_dist
                    Dim y As Double = pnt.Y + m_move_dist
                    m_graphicTracker.MoveTo(id, x, y, 0)

                    m_GTGeometries.Remove(id)

                    Dim point As IPoint = New PointClass()
                    point.PutCoords(x, y)

                    m_GTGeometries.Add(id, point)
                End If
            End If
        End If

        'Move all graphics
        If m_move_all Then
            If m_graphicTracker IsNot Nothing Then
                For i As Integer = 0 To m_graphicTracker.Count - 1
                    Dim id As Integer = i
                    If m_GTGeometries.ContainsKey(id) Then
                        Dim pnt As IPoint = DirectCast(m_GTGeometries(id), IPoint)
                        Dim x As Double = pnt.X + m_move_dist
                        Dim y As Double = pnt.Y + m_move_dist
                        m_graphicTracker.MoveTo(id, x, y, 0)

                        m_GTGeometries.Remove(id)

                        Dim point As IPoint = New PointClass()
                        point.PutCoords(x, y)

                        m_GTGeometries.Add(id, point)
                    End If
                Next

            End If
        End If

        If m_graphicTracker.Count = comboTrackerID.Items.Count Then
            Return
        End If

        comboTrackerID.Items.Clear()

        For i As Integer = 0 To m_graphicTracker.Count - 1
            comboTrackerID.Items.Add(i)
        Next

    End Sub

	Private Sub Calculate_Move_Distance()
		'Call this every time the map document is replaced
		'This move distance is set to simulate changing data location
		'Change the divisor to change the distance that the graphic moves
		'You may need to call this every time you zoom in/out but that will depend on your application
		m_move_dist = m_mapControl.Extent.Width / 500
	End Sub

    Private Sub axMapControl1_OnMapReplaced(ByVal sender As Object, ByVal e As IMapControlEvents2_OnMapReplacedEvent) Handles axMapControl1.OnMapReplaced
        'Every time you change the map document don't forget to initialize the GraphicTracker with
        'the Map. In this sample you must calculate the move distance again.
        m_graphicTracker.Initialize(TryCast(m_mapControl.Map, IBasicMap))
        Calculate_Move_Distance()
    End Sub

End Class