Mapx加载gst文件,自定义图层,Mapx画自定义样式矩形,圆形,椭圆形

很久没有写博文了,也是第一次写关于Gis方面的博文。今天就和大家分享一个Mapx的一个简单的例子。

1、安装mapx 5.0安装包。

2、在VS工具箱中添加Mapx控件。

3、下面来看一下具体实现:

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;

namespace MapxDemo
{
    /// <summary>
    ///功能: Mapx画形状的Demo
    ///作者:胡耕永
    /// </summary>
    public partial class FormMapxDemo : Form
    {
        #region Parameters
        Dictionary<string, MapXLib.Feature> featureList = null;//图元集合
        int feaNumber;//图元编号
        const string feaPrefix = "fea";//图元标识前缀
        MapXLib.Layer m_visionLayer;//图层
        #endregion

        #region Constructor
        public FormMapxDemo()
        {
            InitializeComponent();
        }
        #endregion

        #region OnLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            axMap.GeoSet = Application.StartupPath + "\\MapInfo\\广州.gst";
            axMap.CreateCustomTool(11, MapXLib.ToolTypeConstants.miToolTypeCircle, MapXLib.CursorConstants.miCrossCursor);
            axMap.CreateCustomTool(12, MapXLib.ToolTypeConstants.miToolTypeMarquee, MapXLib.CursorConstants.miCrossCursor);
            axMap.CreateCustomTool(13, MapXLib.ToolTypeConstants.miToolTypeMarquee, MapXLib.CursorConstants.miCrossCursor);
            featureList = new Dictionary<string, MapXLib.Feature>();
            CreateLayer();
        }
        #endregion

        #region Events
        
        private void toolStripButtonRect_Click(object sender, EventArgs e)
        {
            axMap.CurrentTool = (MapXLib.ToolConstants)12; //画矩形

        }

        private void axMap_ToolUsed(object sender, AxMapXLib.CMapXEvents_ToolUsedEvent e)
        {
            if(m_visionLayer==null)
            CreateLayer();
            MapXLib.Feature feaObj;
        
            MapXLib.Point pt1 = new MapXLib.Point();
            pt1.Set(e.x1, e.y1);
            MapXLib.Point pt2 = new MapXLib.Point();
            pt2.Set(e.x2, e.y2);
            if (e.toolNum == 11)
            {
                double radius = Math.Sqrt(Math.Pow((pt2.X - pt1.X), 2) + Math.Pow((pt2.Y - pt1.Y), 2));
                #region 画圆
                try
                {
                    feaObj = axMap.FeatureFactory.CreateCircularRegion(MapXLib.CircleTypeConstants.miCircleTypeScreen, pt1, radius, MapXLib.MapUnitConstants.miUnitDegree, 100, axMap.DefaultStyle);
                    axMap.Layers._Item(m_visionLayer).AddFeature(feaObj, Type.Missing);
                    feaObj.Update(Type.Missing, Type.Missing);
                    feaObj.KeyValue = feaPrefix + feaNumber++.ToString();
                    featureList.Add(feaObj.KeyValue, feaObj);
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
                #endregion
            }
            if (e.toolNum == 12)
            {
                #region 画矩形
                MapXLib.Style redline = new MapXLib.StyleClass();
                redline.LineColor = (uint)MapXLib.ColorConstants.miColorRed;
                redline.LineWidthUnit = MapXLib.StyleUnitConstants.miStyleUnitPixel;
                redline.LineWidth = 2;
                redline.LineStyle = (MapXLib.PenStyleConstants)2; //画笔样式
                redline.RegionPattern = MapXLib.FillPatternConstants.miPatternNoFill;
                MapXLib.Points pts = new MapXLib.Points();
                pts.Add(pt1, 1);
                pts.Add(pt2, 3);
                pts.AddXY(e.x1, e.y2, 4);
                pts.AddXY(e.x2, e.y1, 2);
                try
                {
                    feaObj = axMap.FeatureFactory.CreateRegion(pts, redline);
                    axMap.Layers._Item(m_visionLayer).AddFeature(feaObj, Type.Missing);
                    feaObj.KeyValue = feaPrefix + feaNumber++.ToString();
                    featureList.Add(feaObj.KeyValue, feaObj);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                #endregion
            }
            if (e.toolNum == 13)
            {
                #region 画椭圆
                try
                {
                    MapXLib.Rectangle rect = new MapXLib.Rectangle();
                    MapXLib.Style redline = new MapXLib.StyleClass();
                    redline.LineColor = (uint)MapXLib.ColorConstants.miColorRed;
                    redline.LineWidthUnit = MapXLib.StyleUnitConstants.miStyleUnitPixel;
                    redline.LineWidth = 2;
                    redline.LineStyle = (MapXLib.PenStyleConstants)2; //画笔样式
                    redline.LineInterleaved = false;
                    rect.Set(e.x1, e.y1, e.x2, e.y2);
                    feaObj = axMap.FeatureFactory.CreateEllipticalRegion(rect, 2, 100, redline);
                    axMap.Layers._Item(m_visionLayer).AddFeature(feaObj, Type.Missing);
                    feaObj.KeyValue = feaPrefix + feaNumber++.ToString();
                    featureList.Add(feaObj.KeyValue, feaObj);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
        }

        private void toolStripButtonRadius_Click(object sender, EventArgs e)
        {
            axMap.CurrentTool = (MapXLib.ToolConstants)11; //画圆形
        }

        private void toolStripButtonEllipse_Click(object sender, EventArgs e)
        {
            axMap.CurrentTool = (MapXLib.ToolConstants)13; //画椭圆
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            ClearLayer();
        }

        #endregion

        #region Custom Method

        /// <summary>
        /// 创建一个图层
        /// </summary>
        private void CreateLayer()
        {
            MapXLib.LayerInfo redlayer = new MapXLib.LayerInfoClass();
            redlayer.AddParameter("FileSpec", "");
            redlayer.AddParameter("name", "red");
            redlayer.AddParameter("TableStorageType", "MemTable");
            redlayer.Type = MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTemp;
            MapXLib.Fields flds = new MapXLib.FieldsClass();
            redlayer.AddParameter("fields", flds);
            redlayer.Type = MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTemp;
            m_visionLayer = axMap.Layers.Add(redlayer, 0);
        }
        /// <summary>
        /// 清空图层
        /// </summary>
        private void ClearLayer()
        {
            axMap.Layers.Remove(m_visionLayer);
            m_visionLayer = null;
        }
        #endregion
    }
}


 

 Mapx加载gst文件,自定义图层,Mapx画自定义样式矩形,圆形,椭圆形_第1张图片

 

你可能感兴趣的:(圆形,椭圆形,Mapx画自定义样式矩形,Mapx加载gst文件,自定义图层)