You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects General forum

How to enable dynamic map in Java   Matthew Tucknott Dec 21, 2006
Re: How to enable dynamic map in Java   Eric Bader Dec 21, 2006
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject How to enable dynamic map in Java 
Author Matthew Tucknott 
Date Dec 21, 2006 
Message Hi,

For several years now we have been using ESRI. The one big issue is the speed of the map control. So I am really excited about assessing the speed improvements promised in 9.2.

I am still a die-hard C++ programmer, However we have migrated to Java.

So I thought I would give 9.2 a play before Christmas. I am particularly intested in the dynamic map capability.

I have a mapbean (i.e. the ActiveX MapControl) and I want to get to the IDynamicMap interface, to do this I go:

IDynamicMap dm = null;
dm = (IDynamicMap) mapBean1.getMap();

if (dm != null)
{
dm.setDynamicMapEnabled(true);
}

On the call to setDynamicMapEnabled I get the following error message in my NetBeans environment

AutomationException: 0x80004005 - Unspecified error
at com.esri.arcgis.carto.Map.setDynamicMapEnabled(Unknown Source)
at com.esri.arcgis.daae.engine.MappingControls.(MappingControls.java:72)
at com.esri.arcgis.daae.engine.MappingControls.main(MappingControls.java:406)


I assume as I don't really know my way around java that I am doing something fundamentally wrong. BTW the code works without a call to the dynmaic map.

Does anyone have any idea?

Thanks and happy Christmas

Tuckers

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: How to enable dynamic map in Java 
Author Eric Bader 
Date Dec 21, 2006 
Message Hi Matthew,

The best way to to this is to create a custom command and use hookHelper to get the handle to the MapBean from within your command code. The HookHelper will give you the focus map that you can cast to IDynamicMap:

IDynamicMap dynamicMap = (IDynamicMap)hookHelper.getFocusMap();

The following example code comes from the Engine Java SDK sample called Display.CustomLayer.DynamicDisplay. It's the source for a custom command. Please refer to the onClick method. I hope this is helpfull... 
 
/*
 * Copyright (c) 2006 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 use restrictions at /arcgis/java/samples/userestrictions.
 */

 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.Timer;

import com.esri.arcgis.carto.FeatureLayer;
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.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geometry.IEnvelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.system.Cleaner;
import com.esri.arcgis.system._WKSPoint;

public class DynamicTrackingCommand extends BaseCommand {

	HookHelper hookHelper = null;
	Timer timer = null;
	ActionListener task = null;
	IPoint point = null;
	IActiveView activeView = null;
	Vector points = new Vector();
	boolean isRunning = false;
	boolean once = true;
	MyDynamicLayer layer = null;
	int index = 0;
	
	public DynamicTrackingCommand() {
		super();
		caption ="Add Dynamic Layer";
		enabled = true;
		
	}

	public void onClick() {
		if(once){
			try{
				  IDynamicMap dynamicMap = (IDynamicMap)hookHelper.getFocusMap();
			      if (!dynamicMap.isDynamicMapEnabled()){
			        dynamicMap.setDynamicMapEnabled(true);
			     
			      }
		GenerateNavigationData();
		 layer = new MyDynamicLayer();
		 layer.setSpatialReferenceByRef(activeView.getFocusMap().getSpatialReference());
		hookHelper.getFocusMap().addLayer(layer);
		once = false;
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		if(!isRunning){
			timer.start();
			System.out.println("Started!");
			isRunning = true;
		}else{
			timer.stop();
			isRunning = false;
		}
		
		
		
	}

	public void onCreate(Object arg0) {
	try{
		hookHelper = new HookHelper();
		hookHelper.setHookByRef( arg0 );
		activeView = hookHelper.getActiveView();
		task = new ActionListener() {
		      public void actionPerformed(ActionEvent evt) {
		    	updateLayer();
		      }
		  };
		timer = new Timer(100, task);
	}catch(Exception e){
		
	}
	}
	
	
	public IFeatureClass openFeatureClass(){
	IFeatureClass fClass = null;
	try{
		String arcgishome = System.getenv("ARCGISHOME");
		String path = arcgishome+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);
	}catch(Exception e){
		e.printStackTrace();
	}
	return fClass;
	}
	
	
    private void GenerateNavigationData() throws Exception
    {
  
    	IEnvelope envelope = hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().getVisibleBounds();

      double densificationFactor = envelope.getWidth() / 5000.0;

      //open a polyline featurelayer that would serve the real-time feed GPS simulator
      IFeatureClass featureclass = openFeatureClass();
if(featureclass == null){
	JOptionPane.showMessageDialog(null,"ERROR");
}
      //iterate through the features of the featurelayer
      IFeatureCursor featureCursor = featureclass.search(null, true);
      IFeature feature = null;
      IPolyline polyLine = null;
      IPoint pt = null;
      while ((feature = featureCursor.nextFeature()) != null)
      {
        polyLine = (IPolyline)feature.getShapeCopy();
        polyLine.smooth(0);
        polyLine.densify(densificationFactor, 0);


        IPointCollection pointCollection = (IPointCollection)polyLine;


        int count = pointCollection.getPointCount();
        for (int i = 0; i < count; i++)
        {
          pt = pointCollection.getPoint(i);
         
          _WKSPoint p = new _WKSPoint();
         
          p.x =  pt.getX();
          p.y = pt.getY();
         points.add(p);
        }

        if (points.size() > 10000)
          break;
      }

   Cleaner.release(featureCursor);
    }
    
    
    
    
    public void updateLayer(){
    	try{
    		 if (index == (points.size() - 1))
    	      {
    			 System.out.println("stopping...");
    	      timer.stop();
    	        return;
    	      }

    	      _WKSPoint currentPoint = (_WKSPoint)points.elementAt(index);
    	      _WKSPoint nextPoint =  (_WKSPoint)points.elementAt(index+1);

    	      double azimuth = (180.0 / Math.PI) * Math.atan2(nextPoint.x - currentPoint.x, nextPoint.y - currentPoint.y);

    	      //create the navigation data structure
    	      NavigationData navigationData = new NavigationData(currentPoint.x, currentPoint.y, azimuth);
    	      
    	      //update the map extent and rotation
    	 //***********************************
    	      CenterMap(navigationData);
    	      //m_invokeHelper.InvokeMethod(navigationData);

    	      //add the navigation data to the loction layer in order to draw it
    	     layer.addRow(navigationData);
    	     
    	    
    	      //m_objectLocation.LayrIsDirty = true;

    	      index++;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    
    
    public void CenterMap(NavigationData navigationData) throws Exception
    {
      //get the current map visible extent
      IEnvelope envelope = activeView.getScreenDisplay().getDisplayTransformation().getVisibleBounds();
      if (null == point)
      {
        point = new Point();
      }
      //set the new map center coordinate
      point.putCoords(navigationData.getX(), navigationData.getY());
      //center the map around the new coordinate
      envelope.centerAt(point);
      activeView.getScreenDisplay().getDisplayTransformation().setVisibleBounds(envelope);
      //rotate the map to the new rotation angle
      activeView.getScreenDisplay().getDisplayTransformation().setRotation(navigationData.getAzimuth());
    }
}