Dynamic layers
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.io.File;
import java.util.Vector;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IDynamicMap;
import com.esri.arcgis.carto.Map;
import com.esri.arcgis.controls.BaseCommand;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.system._WKSPoint;
import com.esri.arcgis.system.esriDrawPhase;

public class DynamicTrackingCommand extends BaseCommand {

  private static final long serialVersionUID = 1L;
  private HookHelper hookHelper = null;
  private IActiveView activeView;

  public DynamicTrackingCommand() {
    super();
    this.caption ="Dynamic Tracking";
    this.enabled = true;

  }

  /* This method sets the map in dynamic mode
   * and adds a new dynamic layer to the map
   */
  public void onClick() {
    try{

      Vector <_WKSPoint> points = new Vector<_WKSPoint>();
      IDynamicMap dynamicMap = (IDynamicMap)hookHelper.getFocusMap();
      if (!dynamicMap.isDynamicMapEnabled()){
        //Step 1: Enable Dynamic Map
        dynamicMap.setDynamicMapEnabled(true);
        //add the US highways layer to the map
        addHighwaysLayer();
        points=generateNavigationData();

        // Step 2: Create new dynamic layer and add to Map 
        MyDynamicLayer dynamiclayer = new MyDynamicLayer(points);
        dynamiclayer.setSpatialReferenceByRef(activeView.getFocusMap().getSpatialReference());
        Map map=(Map)hookHelper.getFocusMap();
        map.addLayer(dynamiclayer);
        //set Map scale and new envelope so the changes to the marker are visible
        Envelope newEnv = (Envelope) hookHelper.getActiveView().getExtent();
        newEnv.expand(10.0,10.0,true);
        hookHelper.getFocusMap().setMapScale(500000);
        //Disable the command button
        this.enabled=false;          
      }
    }catch(Exception e){
      e.printStackTrace();
      System.out.println("Exception in method DynamicTrackingCommand#onClick" +e);
    }      
  }

  public void onCreate(Object hook) {
    try{
      hookHelper = new HookHelper();
      hookHelper.setHookByRef( hook );
      activeView=hookHelper.getActiveView();    
    }catch(Exception e){
      e.printStackTrace();
      System.out.println("Exception in method DynamicTrackingCommand#onCreate" +e);
    }
  }

  /*This method receives the marker location already recorded in NavigationData.xml 
   * and populates the vector
   */
  private Vector<_WKSPoint> generateNavigationData() throws Exception
  {
    final Vector <_WKSPoint> points = new Vector<_WKSPoint>();  
    String navFile = this.getClass().getClassLoader().getResource("NavigationData.xml").getPath();
    if(System.getProperty("os.name").toLowerCase().indexOf("win")>-1){
      navFile = navFile.substring(1).replaceAll("%20"," ");
    }
    SAXParserFactory.newInstance().newSAXParser().parse(new File(navFile),new DefaultHandler(){
      @Override
      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equalsIgnoreCase("navigationItem")){
          _WKSPoint p = new _WKSPoint();
          p.x = Double.parseDouble(attributes.getValue("x"));
          p.y = Double.parseDouble(attributes.getValue("y"));
          points.add(p);
        }
      }

    });
    return points;
  }

  /*
   * This method adds the highways shape file at "%ArcGIS%/java/samples/data/usa"
   * to the current map
   */
  private void addHighwaysLayer(){
    IFeatureClass fClass = null;
    try{
      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");
      String path = devKitHome+File.separator+"java"+File.separator+"samples"+File.separator+"data"+File.separator+"usa";
      ShapefileWorkspaceFactory fact = new ShapefileWorkspaceFactory();
      IFeatureWorkspace workspace = (IFeatureWorkspace)fact.openFromFile(path,0);
      fClass = workspace.openFeatureClass("ushigh.shp");
      FeatureLayer layer = new FeatureLayer();
      layer.setFeatureClassByRef(fClass);
      layer.setName(fClass.getAliasName());
      hookHelper.getFocusMap().addLayer(layer);
      hookHelper.getActiveView().partialRefresh(esriDrawPhase.esriDPGeography, null, null);
    }catch(Exception e){
      e.printStackTrace();
      System.out.println("Exception in method DynamicTrackingCommand#addHighwaysLayers" +e);
    }

  }

}