AO+Java实现比例尺、指北针、图名、图例的添加(ArcGIS Add-in for Java)


公司前段时间,项目需要用ArcGIS Add-in for Java对ArcMap进行二次开发,简化制图。目前做了一些工作,记录下来和大家交流一下心得!由于网上资料关于Java的资料比较少,大多都是用C#做的,平时在查资料是参考的最多是官方给的API文档和参考C#+AE实现相关功能,然后修改实现自己要实现的功能。
入门看的一篇文档写很不错,URL:http://www.docin.com/p-884474446.html;不过里面讲的那个bug,我在反复测试之后发现,只要自己用管理员的身份运行Java ConfigTool.exe一次即可解决看见插件的问题。
官方API文档URL(AO):
http://resources.arcgis.com/en/help/arcobjects-java/api/arcobjects/index.html;

别的不说了直接上代码:

1、添加指北针


    /**
	 * 添加指北针
	 * 
	 * @param pageLayout
	 * @throws AutomationException
	 * @throws IOException
	 */
	public static void AddNorthArrow(PageLayout pageLayout,double x,double y) throws AutomationException, IOException {
		// IGraphicsContainert方便Map、PageLayout等对象对其里面的元素进行操作
		IGraphicsContainer container = pageLayout;


		IActiveView activeView = pageLayout.getActiveView();
        
		/* 获得MapFrame */
		// 框架元素
		IFrameElement frameElement = container.findFrame(activeView.getFocusMap());
		// 地图框架
		IMapFrame mapFrame = (IMapFrame) frameElement;


		/* 根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround */
		// UID唯一标识码
		UID uid = new UID();
		uid.setValue(com.esri.arcgis.carto.MarkerNorthArrow.class);
		
		IMapSurroundFrame mapSurroundFrame = mapFrame.createSurroundFrame(uid, null);


		// 设置MapSurroundFrame中指北针的点符号 
		IMapSurround mapSurround = mapSurroundFrame.getMapSurround();
		
		MarkerNorthArrow markerNorthArrow =  (MarkerNorthArrow) mapSurround;
		//IArrowMarkerSymbol arrowMs = new ArrowMarkerSymbol();
		ICharacterMarkerSymbol chMS=new CharacterMarkerSymbol();
		Font font=new StdFont();
		font.setName("ESRI North");
		chMS.setFont(font);
		chMS.setCharacterIndex(177);
		chMS.setSize(47);
		markerNorthArrow.setMarkerSymbol(chMS);
		// QI,确定mapSurroundFrame的位置 
		IElement element = (IElement) mapSurroundFrame;
		IEnvelope envelope = new Envelope();
		envelope.putCoords(x,y,x+0.5,y+1);
		element.setGeometry(envelope);


		// 使用IGraphicsContainer接口添加显示 
		container.addElement(element, 0);
		
		activeView.refresh();
	}

2、添加图名

/**
	 * 添加图名
	 * 
	 * @param pageLayout
	 * @throws AutomationException
	 * @throws IOException
	 */
	public static void AddMapTitle(PageLayout pageLayout,String sName,double x,double y) throws AutomationException, IOException {
		// IGraphicsContainert方便Map、PageLayout等对象对其里面的元素进行操作
		IGraphicsContainer container = pageLayout;
		// container.deleteAllElements();
		IActiveView activeView = pageLayout.getActiveView();
		Point titlePoint;
		TextElement textElement = new TextElement();
		titlePoint = new Point();
		titlePoint.setX(x);
		titlePoint.setY(y);
		
		textElement.setGeometry(titlePoint);
		textElement.setText(sName);
		textElement.setFontName("宋体");
		if (sName.length() >= 30) {
			textElement.setSize(20);
		} else {
			textElement.setSize(24);
		}
		container.addElement(textElement, 0);

		activeView.refresh();
	}


3、添加比例尺
    /**
	 * 添加比例尺
	 * 
	 * @param pageLayout
	 * @throws AutomationException
	 * @throws IOException
	 */
	public void AddScalebar(PageLayout pageLayout) throws AutomationException, IOException {
		// IGraphicsContainert方便Map、PageLayout等对象对其里面的元素进行操作
		IGraphicsContainer container = pageLayout;
		// container.deleteAllElements();
		IActiveView activeView = pageLayout.getActiveView();

		/* 获得MapFrame */
		// 框架元素
		IFrameElement frameElement = container.findFrame(activeView.getFocusMap());
		// 地图框架
		IMapFrame mapFrame = (IMapFrame) frameElement;

		/* 根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround */
		// UID唯一标识码
		UID uid = new UID();
		uid.setValue(com.esri.arcgis.carto.AlternatingScaleBar.class);
		IMapSurroundFrame mapSurroundFrame = mapFrame.createSurroundFrame(uid, null);

		// 设置MapSurroundFrame中指北针的点符号 
		IMapSurround mapSurround = mapSurroundFrame.getMapSurround();
		IScaleBar markerScaleBar = (IScaleBar) mapSurround;
		markerScaleBar.setLabelPosition(esriVertPosEnum.esriBelow);
		// markerScaleBar.useMapSettings();
		// 设置比例尺的高度
		markerScaleBar.setBarHeight((double) 5);

		// 设置比例尺单位
		markerScaleBar.setUnits(9);
		// 设置比例尺单位长度
		markerScaleBar.setDivision(1);
		// 设置比例尺分度尺
		markerScaleBar.setDivisions((short) 2);
		// 设置比例尺尺标
		markerScaleBar.setLabelFrequency(markerScaleBar.getDivisions());
		// 设置二级分度尺
		markerScaleBar.setSubdivisions((short) 2);
		// 设置尺标单位与尺标之间的间距
		markerScaleBar.setUnitLabelGap(5);
		// 设置尺标单位标签内容
		markerScaleBar.setUnitLabel("M");
		markerScaleBar.setDivisionsBeforeZero((short) 0);
		markerScaleBar.setLabelGap(5);

		// QI,确定mapSurroundFrame的位置 
		IElement element = (IElement) mapSurroundFrame;
		IEnvelope envelope = new Envelope();
		envelope.putCoords(22.5136, 1.5, 22.5136, 1.5);
		envelope.setWidth(6.2397);
		envelope.setHeight(1);
		element.setGeometry(envelope);

		// 使用IGraphicsContainer接口添加显示 
		container.addElement(element, 0);
		activeView.refresh();
	}

4、添加图例
    /**
	 * 添加图例
	 * 
	 * @param pageLayout
	 * @throws AutomationException
	 * @throws IOException
	 */
	
	public void AddLegend(PageLayout pageLayout) throws AutomationException, IOException {
		// IGraphicsContainert方便Map、PageLayout等对象对其里面的元素进行操作
		IGraphicsContainer container = pageLayout;
		// container.deleteAllElements();
		IActiveView activeView = pageLayout.getActiveView();

		/* 获得MapFrame */
		// 框架元素
		IFrameElement frameElement = container.findFrame(activeView.getFocusMap());
		// 地图框架
		IMapFrame mapFrame = (IMapFrame) frameElement;

		/* 根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround */
		// UID唯一标识码
		UID uid = new UID();
		uid.setValue(com.esri.arcgis.carto.Legend.class);
		IMapSurroundFrame mapSurroundFrame = mapFrame.createSurroundFrame(uid, null);

		// 设置MapSurroundFrame中指北针的点符号 
		IMapSurround mapSurround = mapSurroundFrame.getMapSurround();
		ILegend2 legend = (ILegend2) mapSurround;
		legend.setTitle("图例");
		ILegendFormat format = new LegendFormat();
		ITextSymbol symbol = new TextSymbol();

		/* 要先设置字体样式,然后设置大小 */
		Font font = new StdFont();
		font.setName("宋体");
		symbol.setFont(font);
		symbol.setSize(14);
		format.setTitleSymbol(symbol);
		legend.setFormatByRef(format);
		
		// QI,确定mapSurroundFrame的位置 
		IElement element = (IElement) mapSurroundFrame;
		IEnvelope envelope = new Envelope();
		envelope.putCoords(1.5471, 2.3012, 1.5471, 2.3012);
		element.setGeometry(envelope);

		// 使用IGraphicsContainer接口添加显示 
		container.addElement(element, 0);
		activeView.refresh();
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值