Dynamic tracking
arcgissamples\display\DynamicTrackingCommand.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.display;

import java.util.HashMap;
import java.util.Random;

import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IDynamicMap;
import com.esri.arcgis.controls.BaseCommand;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.geometry.IEnvelopeGEN;

public class DynamicTrackingCommand extends BaseCommand {
  private static final long serialVersionUID = 1L;
  HookHelper hookHelper = null;
  IActiveView activeView = null;
  boolean isRunning = false;
  boolean once = true;
  MyDynamicLayer layer = null;
  int index = 0;
  int numOfItems = 100;
    
  public DynamicTrackingCommand() {
    caption ="Add Dynamic Tracking";
    enabled = true;    
  }
  
  public void onCreate(Object hook) {
    try{
      hookHelper = new HookHelper();
      hookHelper.setHookByRef( hook );
      activeView = hookHelper.getActiveView();

    }catch(Exception e){
      System.out.println("Exception in DynamicTrackingCommand#onCreate");
      e.printStackTrace();
    }
  }

  public void onClick() {
    if(once){
      try{
        IDynamicMap dynamicMap = (IDynamicMap)hookHelper.getFocusMap();
        if (!dynamicMap.isDynamicMapEnabled()){
          dynamicMap.setDynamicMapEnabled(true);           
        }
        //Create initial random location for 100 items
        HashMap<Integer, NavigationData> map = generateNavigationData();
          //Add Dynamic Layer to map
        layer = new MyDynamicLayer(map);
        hookHelper.getFocusMap().addLayer(layer);
        once = false;
      }catch(Exception e){
        System.out.println("Exception in DynamicTracingCommand#onClick");
        e.printStackTrace();
      }
    }
    
    if(!isRunning){
      //Update the dynamic items location
      layer.start();
      System.out.println("Started!");
      isRunning = true;
    }else{
      //Stop updating dynamic items location
      layer.stop();
      System.out.println("Stopped!");
      isRunning = false;
    }
  }
  /*
   * @see com.esri.arcgis.controls.BaseCommand#isChecked()
   * This method shows the command button to be depressed when clicked
   */
  public boolean isChecked() {
    return isRunning;
  }
  
  private HashMap<Integer, NavigationData> generateNavigationData() throws Exception
  {
    HashMap<Integer, NavigationData> map=new HashMap<Integer, NavigationData>();
    //get the map's fitted bounds
    IEnvelopeGEN extent = (IEnvelopeGEN)hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().getFittedBounds();
    //calculate the step for which will be used to increment the items
    double XStep = extent.getWidth() / 2000.0;
    double YStep = extent.getHeight() / 2000.0;

    Random rnd = new Random();
    double stepX, stepY;

    //generate the random location for the items
    for (int i = 0; i < numOfItems; i++)
    {
      //calculate the step for each item
      stepX = XStep * rnd.nextDouble();
      stepY = YStep * rnd.nextDouble();
      double x = extent.getXMin() + rnd.nextDouble() * (extent.getXMax() - extent.getXMin());
      double y = extent.getYMin() + rnd.nextDouble() * (extent.getYMax() - extent.getYMin());
      //calculate the heading
      double azimuth = (360.0 + 90.0 - Math.atan2(stepY, stepX) * 180 / Math.PI) % 360.0;
      //add a type ID in order to define the symbol for the item
      int state = 0;
      switch (i % 3)
      {
      case 0:
        state = 0;
        break;
      case 1:
        state= 1;
        break;
      case 2:
        state = 2;
        break;
      }

      //add the item's location to the hash map
      NavigationData data = new NavigationData(x,y,azimuth,stepX, stepY, state);
      map.put(new Integer(i), data);
    }
    return map;
  }

    
   
    
   
    
  
}