【C# + Argis Engine】地图上绘制圆形并且实时的显示半径源码

主要思路:

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;


        List<double> ObtainedValues;

       //构造函数

        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);


                    pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, null);
                }
            }
        }
        -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //鼠标抬起
        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);
        }




    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值