MapXtreme中圆操作

在winfrom中的mapxtreme的地图控件上画圆分为下面几步:

第一步:添加圆图层对应的table并添加table对应的图层到map上,其中添加ID字段到表中,后面删除此圆需用到

Table tableCircleTemp ;
 if (tableCircleTemp != null)
                {
                    if (tableCircleTemp.IsOpen)
                    {
                        tableCircleTemp.Close();
                    }
                }
                TableInfo ti11 = TableInfoFactory.CreateTemp("TempCircle");
                ti11.Columns.Add(ColumnFactory.CreateStringColumn("ID", 20));
                tableCircleTemp = Session.Current.Catalog.CreateTable(ti11);
                FeatureLayer lyr11 = new FeatureLayer(tableCircleTemp);
                mapControl.Map.Layers.Insert(0, lyr11);

第二步:在画圆的触发事件中为mapcontrol设置相应的工具:

 public class MapToolKit
    {
        public static String Default = "Arrow";
        public static String Zoomin = "ZoomIn";
        public static String Zoomout = "Zoomout";
        public static String Center = "Center";
        public static String Pan = "Pan";
        public static String Distance = "AddPolyline";
        public static String DrawRect = "AddRectangle";
        public static String DrawCircle = "AddCircle";
        public static String DrawPolygon = "AddPolygon";
    }


mainFrm.MapControl.Tools.LeftButtonTool = MapToolKit.DrawCircle;

第三步:为mapcontrol添加mousemove mousedown mouseup事件,其中在down事件中捕捉圆心点

 private bool mouseDowned = false;
 private DPoint startPoint = new DPoint();

  private void mapControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                startPoint = GetCoordPt(e.X, e.Y);
                mouseDowned = true;
            }
        }

在move事件中捕捉圆的半径点,并画圆,下面代码是实现画圆时鼠标移动显示不同大小圆

 private void mapControl_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                    if (e.Button == MouseButtons.Left && mouseDowned)
                    {

                        if (mapControl.Tools.LeftButtonTool == MapToolKit.DrawCircle)
                        {
                            (tableCircleTemp as IFeatureCollection).Clear();
                            DrawCircle(startPoint, GetCoordPt(e.X, e.Y),-1);
                        }
                      
                    }
                }
            }
            catch (Exception ex)
            {
             
            }
        }
  //临时图层画圆
        private void DrawCircle(DPoint dpt1, DPoint dpt2,int id)
        {
            try
            {
                if (dpt1.x == dpt2.x || dpt1.y == dpt2.y)
                    return;
                Feature ftr = new Feature(tableCircleTemp.TableInfo.Columns);

                double xRadius, yRadius;
                xRadius = yRadius = (double)(Math.Sqrt(Math.Pow(dpt1.y - dpt2.y, 2) + Math.Pow(dpt1.x - dpt2.x, 2)));
                CoordSys coordSys = this.mapControl.Map.GetDisplayCoordSys();
                Ellipse ellipse = new Ellipse(coordSys, dpt1, xRadius, yRadius, MapInfo.Geometry.DistanceUnit.Degree, DistanceType.Spherical);

                ftr.Geometry = ellipse as FeatureGeometry;
                ftr.Style = new AreaStyle(new SimpleLineStyle(new LineWidth(), 1), new SimpleInterior(16, Color.Red, Color.DarkRed, true));
                ftr["ID"] = id.ToString();
                tableCircleTemp.InsertFeature(ftr);
                tableCircleTemp.Refresh();
            }
            catch (Exception ex)
            {
                
            }
        }
 //屏幕坐标转换到经纬度
        private DPoint GetCoordPt(int x, int y)
        {
            DPoint dpt = new DPoint();
            if (SystemConfig.LocalMapType == MapType.MapInfo)
            {

                mapControl.Map.DisplayTransform.FromDisplay(new System.Drawing.Point(x, y), out dpt);

            }
            DPoint dp = new DPoint();
            dp.x = double.Parse(dpt.x.ToString("0.000000"));
            dp.y = double.Parse(dpt.y.ToString("0.000000"));
            return dp;
        }

mouseup说明圆画好了,重新画圆即可

 private void mapControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                endPoint = GetCoordPt(e.X, e.Y);
                
                if ((mapControl.Tools.LeftButtonTool == MapToolKit.DrawCircle)
                    && startPoint.x != endPoint.x && startPoint.y != endPoint.y)
                {
                  DrawCircle(startPoint, GetCoordPt(e.X, e.Y),-1);
                }
                mouseDowned = false;
            }
        }

在数据库中对圆的记录需要包括 当前地图的缩放系数即:mainFrm.MapControl.Map.Zoom.Value * 1.609 / mainFrm.MapControl.Width  ;起始坐标点

定位圆:在地图上显示圆的时候需要三个参数:strP= 画圆时比例系数;p1.x,p1.y;p2.x,p2.y,pmix=当前地图的比例系数,id=当前圆的编号

private void DrawCircle(String strP,double pmix,int id)
        {
            try
            {
                if (strP != "")
                {
                    if (strP.Contains(";"))
                    {
                        string[] str = strP.Split(';');
                        double minPix = double.Parse(str[0]);
                        
                        if(str[1].Contains(","))
                        {
                            string[] ss = str[1].Split(',');
                            DPoint dpt1 = new DPoint(double.Parse(ss[0]), double.Parse(ss[1]));
                            ss = str[2].Split(',');
                            DPoint dpt2 = new DPoint(double.Parse(ss[0]), double.Parse(ss[1]));

                            double radius1 = Math.Sqrt((dpt1.x - dpt2.x) * (dpt1.x - dpt2.x)
                                   + (dpt1.y - dpt2.y) * (dpt1.y - dpt2.y)); ;
                            double radius2 = radius1 * pmix / minPix;

                            Feature ftr = new Feature(tableCircleTemp.TableInfo.Columns);
                            CoordSys coordSys = this.mapControl.Map.GetDisplayCoordSys();
                            Ellipse ellipse = new Ellipse(coordSys, dpt1, radius2, radius2, MapInfo.Geometry.DistanceUnit.Degree, DistanceType.Spherical);

                            ftr.Geometry = ellipse as FeatureGeometry;
                            ftr.Style = new AreaStyle(new SimpleLineStyle(new LineWidth(), 1), new SimpleInterior(16, Color.Red, Color.DarkRed, true));
                            ftr["ID"] = id.ToString();
                            tableCircleTemp.InsertFeature(ftr);
                            tableCircleTemp.Refresh();
                            mapControl.Map.SetView(dpt1, mapControl.Map.GetDisplayCoordSys(), mapControl.Map.Zoom);
                        }                       
                    }
                }
            }
            catch (Exception ex)
            {
               
            }
        }

删除圆,引用为:DelFenceInTable(tableCircleTemp, "TempCircle",Id);

 private void DelInTable(Table table,String tableAlias,int id)
        {
            try
            {
                SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere("");

                IResultSetFeatureCollection ifs;

                if (table != null) //Table exists close it
                {
                    si = MapInfo.Data.SearchInfoFactory.SearchWhere("");

                    ifs = MapInfo.Engine.Session.Current.Catalog.Search(tableAlias, si);

                    foreach (Feature ft in ifs)
                    {
                        string ID = ft["ID"].ToString();
                        if (ID == id.ToString())
                        {
                            table.DeleteFeature(ft);
                            break;
                        }
                    }
                    table.Refresh();

                }
            }
            catch (Exception ex)
            {
                ExceptionLog.Log("DelFenceInTable " + ex.ToString());
            }
        }
原文地址:https://www.cnblogs.com/nygfcn1234/p/3159541.html