ArcGIS Mobile 自定义MapGraphicLayer绘制图形

从事过ArcEngine相关工作知道,在ArcEngine中绘制图形有两种存在方式,一种是以要素(feature)的形式,一种是以元素(element)的形式。添加两种不同形式的图形,绘制的方法是不一样的。其中feature需要通过在内存新建或者添加图层,运用编辑工具在图层中添加feature,而element是通过在graphicContainers中添加。grapicContainers相当于在MapControl 中上覆盖的一个透明图层,在该图层中添加上element,而feature是在map中的图层上进行了更改。

ArcGIS Mobile应用程序中添加图形与ArcEngine类似也存在两种方式。一种方式直接编辑feature,这需要素图层含有GlobalID字段,另一种方式就是定义图层图层,继承ArcGIS Mobile sdk中的MapGraphicLayer类,相当于AE中的graphicContainer. 下面就以介绍这种自定义的方式绘制图形。

实现环境:

win7专业版+visual studio 2008 sp1+.net compact framwork 3.5+windows mobile 6.0 professional sdk+ArcGIS Runtime SDK 10.1.1

实现过程:

Step1:在模拟器中构建界面

图1:构建程序界面图

Step2:新建一个类,该类继承MapGrapicLayer.代码如下:

View Code
class CustomGraphicLayer : MapGraphicLayer
    {

        /// 创建表现symbol
        private Symbol m_pointSymbol;

        //创建集合的坐标点集合
        private CoordinateCollection m_coordinateCollection = new CoordinateCollection();
        private Coordinate m_coordinate = new Coordinate();
        /// Get or set coordinate collection stored in custom graphic layer 
        public CoordinateCollection Coordinates
        {
            get
            {
                return m_coordinateCollection;
            }
            set
            {
                m_coordinateCollection = value;
            }
        }
        public Coordinate Coordinate
        {

            get { return m_coordinate; }
            set { m_coordinate = value; }
        }
        /// 构造函数
        public CustomGraphicLayer()
            : base("CustomGraphicLayer")
        {

            //  m_pointSymbol = new Symbol(new PointPaintOperation(Color.LightGreen, 2, 0, Color.LightGreen, 50, 25, PointPaintStyle.Circle));
            m_pointSymbol = new SimpleMarkerSymbol(Color.Red, 50, Color.Red, 25, SimpleMarkerStyle.Circle);

        }
        /// 重写绘制方法
        protected override void Draw(MapSurface pMapSurface)
        {


            if (pMapSurface == null)
            { return; }
            if (pMapSurface.DrawingCanceled)
            {
                return;
            }
            m_pointSymbol.DrawPoint(pMapSurface, m_coordinate);

        }

    }

Step 3:对自定义的图层的调用

View Code
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string ss = "\\Storage Card\\Continents";

            this.mobileCache1.StoragePath = ss;

            if (!mobileCache1.IsValid)
            {

                MessageBox.Show("invalid file");
            }
            mobileCache1.Open();
            map1.MapLayers.Add(mobileCache1);

            map1.MapAction = null;

        }
        bool flag = false;
        private void menuItem1_Click(object sender, EventArgs e)
        {
            flag = true;

        }
        Coordinate m_startCoordinate;
        CustomGraphicLayer _graphicLayer = null;
        private void map1_MouseDown(object sender, ESRI.ArcGIS.Mobile.MapMouseEventArgs e)
        {
            if (flag)
            {
                m_startCoordinate = e.MapCoordinate;
                drawGraphicsLayer();
            
            }
        }

        void drawGraphicsLayer()
        {

            _graphicLayer = new CustomGraphicLayer();
            _graphicLayer.Coordinate = m_startCoordinate;
            //向MapGrahicLayer中添加绘制的几何对象,自动调用“Draw”方法
            map1.MapGraphicLayers.Add(_graphicLayer);
            map1.Refresh();
        }
    }

Step4:生成结果

图2:结果图

扩展阅读:

Developing custom layers


作者:suwenjiang
出处:http://www.cnblogs.com/myyouthlife/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/myyouthlife/p/3012228.html