主要思路:
1.用到AxMapControl的三个事件
2.自写绘制方法,从中获得想要的数据
3.将数据跟随Move事件,标注的地图上
*文本标注的局部刷新,防止连带圆形一块刷新导致模糊的情况,可参考http://blog.csdn.net/fuyouche/article/details/8618788
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using stdole;
namespace 地图绘圆
{
public partial class MapViwer : Form
{
//用到的全局变量
IActiveView pActiveView;IMap pMap;
IGraphicsContainer pGraphicscon;
INewCircleFeedback pCircleFeed;
ITextElement textElement;
IElement element;
IPoint StartPoint;
//构造函数
public MapViwer(){
InitializeComponent();
}
private void MapViwer_Load(object sender, EventArgs e)
{
InitializeGlobeVar();
}
//初始化全局变量
private void InitializeGlobeVar()
{
pMap = axMapControl1.Map;
pActiveView = pMap as IActiveView;
pGraphicscon = pActiveView as IGraphicsContainer;
ObtainedValues = new List<double>();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
FixCircleCenter(e);
CreateTextNote();
}
private void FixCircleCenter(ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
StartPoint = new PointClass();
StartPoint.PutCoords(e.mapX, e.mapY);
if (pCircleFeed == null)
{
pCircleFeed = new NewCircleFeedbackClass();
pCircleFeed.Display = pActiveView.ScreenDisplay;
pCircleFeed.Start(StartPoint);
}
}
private void CreateTextNote()
{
IRgbColor pColor = new RgbColorClass()
{
Red = 255,
Blue = 0,
Green = 0
};
IFontDisp pFont = new StdFont()
{
Name = "宋体",
Size = 3
} as IFontDisp;
ITextSymbol pTextSymbol = new TextSymbolClass()
{
Color = pColor,
Font = pFont,
Size = 11
};
textElement = new TextElementClass()
{
Symbol = pTextSymbol,
ScaleText = true,
Text = "10公里"
};
element = textElement as IElement;
element.Geometry = StartPoint;
pGraphicscon.AddElement(element, 1);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//鼠标移动
private void axMapControl1_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
RepaintCircleNote(e);
}
/// <summary>
/// 重新绘制圆和半径标注
/// </summary>
/// <param name="e"></param>
private void RepaintCircleNote(ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
ObtainedValues.Clear();
IPoint MovePoint = new PointClass();
MovePoint.PutCoords(e.mapX, e.mapY);
if (pCircleFeed != null)
{
pCircleFeed.MoveTo(MovePoint);
if (element != null)
{
ILine line = new LineClass();
line.FromPoint = StartPoint;
line.ToPoint = MovePoint;
textElement.Text = Math.Round((Math.Abs(line.Length * 100)), 0).ToString() + "公里";
ObtainedValues.Add(Math.Round((Math.Abs(line.Length * 100)), 0));
ObtainedValues.Add(StartPoint.X);
ObtainedValues.Add(StartPoint.Y);
//此段代码是测试获得的值用的,可以不要
double x1 = StartPoint.X;
double y1 = StartPoint.Y;
double x2 = MovePoint.X;
double y2 = MovePoint.Y;
double l =Math.Round(Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1)*(y2 - y1)),2);
ObtainedValues.Add(l);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//鼠标抬起
private void axMapControl1_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
{
ReloadMap();
}
private void ReloadMap()
{
pGraphicscon.DeleteElement(element);
pCircleFeed = null;
element = null;
pActiveView.Refresh();
MessageBox.Show(ObtainedValues[0].ToString() +"\r\n" + ObtainedValues[3].ToString(),"",MessageBoxButtons.OK);
}
}
}