MapWindow Gis 组件代码示例:

MapWindow Gis 组件代码示例:

官方网站: http://www.mapwindow.org/

该示例使用mapwindow 4 MapWinGIS.ocx X86,其它版本请参考官网相关代码示例。 

View Code
#region 地图相关
        //地图初始化
        private void InitMap()
        {
            axMap.CursorMode = MapWinGIS.tkCursorMode.cmPan;
            axMap.SendMouseMove = true;
            axMap.SendMouseDown = true;
            axMap.DisableWaitCursor = true;

            Shapefile sf_provence_area = new Shapefile();
            Shapefile sf_provence_city = new Shapefile();
            Shapefile sf_diqu_area = new Shapefile();
            Shapefile sf_diqu = new Shapefile();
            Shapefile sf_town = new Shapefile();

            sf_provence_area.Open(Application.StartupPath + "\\Map\\bou2_4p.shp", null);
            sf_provence_city.Open(Application.StartupPath + "\\Map\\res1_4m.shp", null);
            sf_diqu_area.Open(Application.StartupPath + "\\Map\\diquJie_polyline.shp", null);
            sf_diqu.Open(Application.StartupPath + "\\Map\\res2_4m.shp", null);
            //sf_town.Open(Application.StartupPath + "\\Map\\XianCh_point.shp", null);

            int idx1 = axMap.AddLayer(sf_provence_area, true);
            int idx2 = axMap.AddLayer(sf_provence_city, true);
            int idx3 = axMap.AddLayer(sf_diqu_area, true);
            int idx4 = axMap.AddLayer(sf_diqu, true);
            //int idx5 = axMap.AddLayer(sf_town, true);

            axMap.set_ShapeLayerFillColor(idx1, (uint)ColorTranslator.ToOle(Color.FromArgb(0xF2, 0xEF, 0xE9)));
            axMap.set_ShapeLayerLineWidth(idx3,0.3f);
            axMap.set_ShapeLayerPointSize(idx2, 5);
            axMap.set_ShapeLayerPointType(idx2, tkPointType.ptDiamond);
            axMap.set_ShapeLayerPointSize(idx4, 1);
            //axMap.set_ShapeLayerPointSize(idx5, 1);

            #region 设置地区标签
            int idx_diqu = axMap.NewDrawing(tkDrawReferenceList.dlSpatiallyReferencedList);
            //axMap.set_DrawingLabelsScale(idx_diqu, true);
            axMap.DrawingFont(idx_diqu, "Tahoma", 7);
            axMap.set_UseDrawingLabelCollision(idx_diqu, true);
            axMap.set_DrawingLabelsShadow(idx_diqu, true);

            for (int i = 0; i < sf_diqu.NumShapes; i++)
            {
                string labtxt = sf_diqu.get_CellValue(5, i).ToString();
                double x, y;
                MapWinGIS.Shape sh = sf_diqu.get_Shape(i);
                x = sh.get_Point(0).x;
                y = sh.get_Point(0).y;
                axMap.AddDrawingLabel(idx_diqu, labtxt, 0, x, y, tkHJustification.hjNone);
            }
            #endregion

            #region 设置省会标签
            int idx = axMap.NewDrawing(tkDrawReferenceList.dlSpatiallyReferencedList);
            axMap.DrawingFont(idx, "Tahoma", 8);
            axMap.set_UseDrawingLabelCollision(idx, true);
            axMap.set_DrawingLabelsShadow(idx, true);
            axMap.set_DrawingLabelsShadowColor(idx, (uint)(ColorTranslator.ToOle(Color.FromArgb(0xDD, 0xDD, 0xFF))));
            for (int i = 0; i < sf_provence_city.NumShapes; i++)
            {
                string labtxt = sf_provence_city.get_CellValue(5, i).ToString();
                double x, y;
                MapWinGIS.Shape sh = sf_provence_city.get_Shape(i);
                x = sh.get_Point(0).x;
                y = sh.get_Point(0).y;
                axMap.AddDrawingLabel(idx, labtxt, 0, x, y, tkHJustification.hjNone);
            }
            #endregion

            #region  设置省份标签
            string labelText1;
            int _idx = axMap.NewDrawing(tkDrawReferenceList.dlSpatiallyReferencedList);
            axMap.DrawingFont(_idx, "Tahoma", 9);
            axMap.set_UseDrawingLabelCollision(_idx, true);
            axMap.set_DrawingLabelsShadow(_idx, true);
            axMap.set_DrawingLabelsShadowColor(_idx, (uint)(ColorTranslator.ToOle(Color.FromArgb(0xD6, 0xD6, 0xAD))));
            for (int i = 0; i < sf_provence_area.NumShapes; i++)
            {
                labelText1 = sf_provence_area.get_CellValue(6, i).ToString();
                double area = Convert.ToDouble(sf_provence_area.get_CellValue(0, i));
                if (area < 0.2)
                {
                    continue;
                }
                MapWinGIS.Shape sh = sf_provence_area.get_Shape(i);

                double x, y;
                CalcShXY(sh, out x, out y);
                if (!sf_provence_area.PointInShape(i, x, y))
                {
                    x = sh.get_Point(sh.numPoints * 3 / 4).x;
                    y = sh.get_Point(sh.numPoints * 3 / 4).y;
                }
                axMap.AddDrawingLabel(_idx, labelText1, (uint)(ColorTranslator.ToOle(Color.Green)), x, y, tkHJustification.hjNone);
            }
            #endregion

            axMap.set_UseLabelCollision(idx1, true);
            axMap.set_LayerLabelsShadow(idx1, true);
        }
        //计算地图中心点
        private void CalcShXY(MapWinGIS.Shape sh, out double x, out double y)
        {
            double minX = double.MaxValue;
            double minY = double.MaxValue;
            double maxX = 0.0;
            double maxY = 0.0;

            for (int m = 0; m < sh.numPoints; m++)
            {
                if (sh.get_Point(m).x < minX)
                {
                    minX = sh.get_Point(m).x;
                }
                if (sh.get_Point(m).y < minY)
                {
                    minY = sh.get_Point(m).y;
                }
                if (sh.get_Point(m).x > maxX)
                {
                    maxX = sh.get_Point(m).x;
                }
                if (sh.get_Point(m).y > maxY)
                {
                    maxY = sh.get_Point(m).y;
                }
            }
            x = (maxX + minX) / 2;
            y = (maxY + minY) / 2;
        }
        //地图鼠标移动
        private void axMap_MouseMoveEvent(object sender, AxMapWinGIS._DMapEvents_MouseMoveEvent e)
        {
            double x = 0;
            double y = 0;
            axMap.PixelToProj(e.x, e.y, ref x, ref y);
            tsLabTxt.Text = "东经:" + x.ToString("F5") + "北纬:" + y.ToString("F5");
        }
        //地图鼠标点击
        private void axMap_MouseDownEvent(object sender, AxMapWinGIS._DMapEvents_MouseDownEvent e)
        {
            double x = 0;
            double y = 0;

            if (2 == e.button)
            {
                axMap.ClearDrawing(_dictdrawHandels[DrawHandels.MeaSureHandel]);
                ClearMeasurePoints();
                tsOperateInfo.Caption = string.Empty;
                _cur = CurState.Arrow;
                axMap.MapCursor = tkCursor.crsrMapDefault;
                return;
            }
            axMap.PixelToProj(e.x, e.y, ref x, ref y);
            MPoint point = new MPoint()
            {
                x = x,
                y = y
            };
            switch (_cur)
            {
                case CurState.Cross:
                    tsTextPoint.Text = point.x.ToString("F5") + "," + point.y.ToString("F5");
                    break;
                case CurState.MeasurementLenth:
                    _measurePoints.Add(point);
                    axMap_AfterTrackingLayerDraw();
                    tsOperateInfo.Caption = "距离为:" + CalcLenght(_measurePoints).ToString("F2") + "";
                    break;
                case CurState.AddSwerve:
                    if ("FK_Swerve" == gc.DataMember)
                    {
                        if ("-1" == edLookRoad.EditValue.ToString())
                        {
                            return;
                        }
                        int newID;
                        object[] objArray = new object[] { 0, Convert.ToInt32(edLookRoad.EditValue), Convert.ToInt32(point.x * 100000), Convert.ToInt32(point.y * 100000), 0, 0 };
                        EsClass.NewTableRow(esdbDS.t_swerve, objArray, "SwerveID", out newID);
                        _ppt.x = point.x;
                        _ppt.y = point.y;
                        menuItemSwerve.Tag = true;
                        ShowInMap(false, false, false, true, false, false);
                        axMap_AfterTrackingLayerDraw();
                    }
                    break;
                case CurState.SetDepotGps:
                    if ("FK_Depots" == gc.DataMember)
                    {
                        ChgGVGps(gvDepots.DataSource, point);
                        menuItemDeport.Tag = true;
                        menuItemAng.Tag = true;
                        ShowInMap(false, true, true, false, false, false);
                        axMap_AfterTrackingLayerDraw();
                    }
                    break;
                case CurState.SetBroadcastGps:
                    BindingSource bsBroadcast = (BindingSource)gvBroadcast.DataSource;
                    if (null == bsBroadcast.Current)
                    {
                        return;
                    }
                    DataRowView drvBroadcast = (DataRowView)bsBroadcast.Current;
                    gvBroadcast.FocusedColumn = gvBroadcast.Columns[0];
                    if (string.Empty == drvBroadcast.Row["起始时间"].ToString() || string.Empty == drvBroadcast["结束时间"].ToString())
                    {
                        XtraMessageBox.Show("请先设置起止时间!");
                        return;
                    }
                    if ("FK_Broadcast" == gc.DataMember)
                    {
                        ChgGVGps(gvBroadcast.DataSource, point);
                        menuItemBroadcast.Tag = true;
                        ShowInMap(false, false, false, false, false, true);
                        axMap_AfterTrackingLayerDraw();
                    }
                    break;
                case CurState.SetSwerveGps:
                    if ("FK_Swerve" == gc.DataMember)
                    {
                        ChgGVGps(gvSwerve.DataSource, point);
                        menuItemSwerve.Tag = true;
                        menuItemDeport.Tag = true;
                        ShowInMap(false, false, false, true, false, false);
                        axMap_AfterTrackingLayerDraw();
                    }
                    break;
                case CurState.ExtendSpeedArea:
                    if ("FK_Speed" == gc.DataMember)
                    {
                        BindingSource bs = (BindingSource)gvSpeed.DataSource;
                        if (null == bs.Current)
                        {
                            return;
                        }
                        DataRowView drv = (DataRowView)bs.Current;
                        int pot = 0;
                        string currentid = drv["SpeedID"].ToString();
                        int currentspeed = (int)drv["限速"];
                        for (int b = 0; b < esdbDS.t_speed.Rows.Count; b++)
                        {
                            if (esdbDS.t_speed.Rows[b].RowState == DataRowState.Deleted)
                            {
                                continue;
                            }
                            if (esdbDS.t_speed.Rows[b].RowState == DataRowState.Detached)
                            {
                                continue;
                            }
                            if (currentid == esdbDS.t_speed.Rows[b]["SpeedID"].ToString())
                            {
                                pot = b;
                            }
                        }
                        DataTable dt = esdbDS.t_speed;
                        DataRow row = dt.NewRow();
                        row.ItemArray = new object[]{ row.ItemArray[0], Convert.ToInt32(edLookRoad.EditValue), 1, Convert.ToInt32(point.x * 100000),
                                                          Convert.ToInt32(point.y * 100000), currentspeed };
                        dt.Rows.InsertAt(row, pot + 1);
                        dt.Columns["SpeedID"].Unique = true;
                        dt.PrimaryKey = new DataColumn[] { dt.Columns["SpeedID"] };
                        _ppt.x = point.x;
                        _ppt.y = point.y;
                        menuItemSpeed.Tag = true;
                        ShowInMap(false, false, false, false, true, false);
                        axMap_AfterTrackingLayerDraw();
                        bs.MoveLast();
                        bs.EndEdit();
                        gc.MainView.CloseEditor();
                    }
                    break;
            }
        }
        //地图导航工具点击
        private void tool_track_axmap_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            _cur = (CurState)(Convert.ToInt32(e.ClickedItem.Tag));
            axMap.MapCursor = tkCursor.crsrMapDefault;
            axMap.MouseWheelSpeed = 0.8;
            switch (_cur)
            {
                case CurState.Arrow:
                    axMap.CursorMode = tkCursorMode.cmNone;
                    break;
                case CurState.Pan:
                    axMap.CursorMode = tkCursorMode.cmPan;
                    break;
                case CurState.ZoomIn:
                    axMap.CursorMode = tkCursorMode.cmZoomIn;
                    break;
                case CurState.ZoomOut:
                    axMap.CursorMode = tkCursorMode.cmZoomOut;
                    break;
                case CurState.Global:
                    axMap.CursorMode = tkCursorMode.cmNone;
                    ShowFullPath();
                    break;
                case CurState.Cross:
                    axMap.MapCursor = tkCursor.crsrCross;
                    break;
                case CurState.MeasurementLenth:
                    axMap.CursorMode = tkCursorMode.cmNone;
                    ClearMeasurePoints();
                    break;
                case CurState.ChgGpsDirection:
                    axMap.CursorMode = tkCursorMode.cmNone;
                    axMap.MouseWheelSpeed = 1;
                    break;
                default:
                    axMap.CursorMode = tkCursorMode.cmNone;
                    break;
            }
        }
        //显示地图 加载地图数据
        private void ShowInMap(bool istrack, bool isdepotsx, bool isdepotxx, bool isswerve, bool isspeed, bool isbroadcast)
        {
            if (null == edLookRoad.EditValue)
            {
                if (istrack)
                {
                    _arrTrack.Clear();
                }
                if (isdepotsx)
                {
                    _arrDepotsx.Clear();
                }
                if (isdepotxx)
                {
                    _arrDepotxx.Clear();
                }
                if (isswerve)
                {
                    _arrSwerve.Clear();
                }
                if (isspeed)
                {
                    _arrSpeed.Clear();
                }
                if (isbroadcast)
                {
                    _arrBroadCast.Clear();
                }
                return;
            }
            if (istrack)
            {
                DataRow[] drs = esdbDS.t_track.Select("RoadID=" + edLookRoad.EditValue.ToString());
                if (drs.Length > 0)
                {
                    ShowTrack(esdbDS.t_track);
                }
                else
                {
                    _arrTrack.Clear();
                    CreateDTTrack();
                    ShowTrack(dtTrack270);
                }
            }
            if (isdepotsx)
            {
                _arrDepotsx.Clear();
                string filter = string.Format("上下行={0}", 0);
                foreach (DataRow row in esdbDS.Tables["t_depots"].Select(filter))
                {
                    if (row.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }
                    if (row["RoadID"].ToString() == edLookRoad.EditValue.ToString())
                    {
                        MPoint pt = new MPoint();
                        pt.x = Convert.ToDouble(row["经度"]) / 100000;
                        pt.y = Convert.ToDouble(row["纬度"]) / 100000;
                        DepotsSX dpts;
                        dpts._name = (string)row["站名"];
                        dpts._pt = pt;
                        dpts._ang = (int)row["角度"];
                        _arrDepotsx.Add(dpts);
                    }
                }
            }
            if (isdepotxx)
            {
                _arrDepotxx.Clear();
                string filter = string.Format("上下行={0}", 1);
                foreach (DataRow row in esdbDS.Tables["t_depots"].Select(filter))
                {
                    if (row.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }
                    if (row["RoadID"].ToString() == edLookRoad.EditValue.ToString())
                    {
                        MPoint pt = new MPoint();
                        pt.x = Convert.ToDouble(row["经度"]) / 100000;
                        pt.y = Convert.ToDouble(row["纬度"]) / 100000;
                        DepotsXX dpts;
                        dpts._name = (string)row["站名"];
                        dpts._pt = pt;
                        dpts._ang = (int)row["角度"];
                        _arrDepotxx.Add(dpts);
                    }
                }
            }
            if (isswerve)
            {
                _arrSwerve.Clear();
                foreach (DataRow row in esdbDS.t_swerve)
                {
                    if (row.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }
                    if (row["RoadID"].ToString() == edLookRoad.EditValue.ToString())
                    {
                        string state = row.RowState.ToString();
                        MPoint pt = new MPoint();
                        pt.x = Convert.ToDouble(row["经度"]) / 100000;
                        pt.y = Convert.ToDouble(row["纬度"]) / 100000;
                        Swerves swers;
                        swers._pt = pt;
                        swers._ang = (int)row["角度"];
                        _arrSwerve.Add(swers);
                    }
                }
            }
            if (isspeed)
            {
                _arrSpeed.Clear();
                string filter = string.Format("RoadID={0}", edLookRoad.EditValue.ToString());
                DataRow[] drc = esdbDS.Tables["t_speed"].Select(filter);
                for (int i = 0; i < drc.Length; )
                {
                    if (drc[i]["RoadID"].ToString() == edLookRoad.EditValue.ToString())
                    {
                        if (drc[i].RowState == DataRowState.Deleted)
                        {
                            continue;
                        }
                        if ("0" == drc[i]["经度"].ToString() || "0" == drc[i]["纬度"].ToString())
                        {
                            List<MPoint> points = new List<MPoint>();
                            Speed _speed = new Speed();
                            int speed = (int)drc[i]["限速"];
                            if (++i == drc.Length)
                                break;
                            while ("0" != drc[i]["经度"].ToString() || "0" != drc[i]["纬度"].ToString())
                            {
                                MPoint point = new MPoint();
                                point.x = Convert.ToDouble(drc[i]["经度"]) / 100000;
                                point.y = Convert.ToDouble(drc[i]["纬度"]) / 100000;
                                points.Add(point);
                                if (++i == drc.Length)
                                    break;
                            }
                            _speed._points = points;
                            _speed._speed = speed;
                            _arrSpeed.Add(_speed);
                        }
                    }
                }
            }
            if (isbroadcast)
            {
                _arrBroadCast.Clear();
                foreach (DataRow row in esdbDS.t_broadcast)
                {
                    if (row.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }
                    if (row["RoadID"].ToString() == edLookRoad.EditValue.ToString())
                    {
                        MPoint pt = new MPoint();
                        pt.x = Convert.ToDouble(row["经度"]) / 100000;
                        pt.y = Convert.ToDouble(row["纬度"]) / 100000;
                        Broadcast bdct;
                        bdct._pt = pt;
                        bdct._type = row["类型"].ToString();
                        _arrBroadCast.Add(bdct);
                    }
                }
            }
            axMap_AfterTrackingLayerDraw();
        }
        //绘制地图
        private void axMap_AfterTrackingLayerDraw()
        {
            #region draw track
            if ((bool)menuItemTrack.Tag)
            {
                menuItemDeport.Tag = true;
                if (menuItemTrack.Checked)
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.TrackDrawHandel, -1);

                    _dictdrawHandels[DrawHandels.TrackDrawHandel] = axMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);
                    for (int i = 0; i < _arrTrack.Count; i++)
                    {
                        Track tr = (Track)_arrTrack[i];

                        for (int k = 0; k < tr._points.Count; k++)
                        {
                            if (k + 1 < tr._points.Count)
                            {
                                axMap.DrawLineEx(_dictdrawHandels[DrawHandels.TrackDrawHandel], tr._points[k].x, tr._points[k].y, tr._points[k + 1].x,
                                    tr._points[k + 1].y, 1, (uint)(ColorTranslator.ToOle(Color.FromArgb(0, 0x72, 0xE3))));
                            }
                        }
                    }
                }
                else
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.TrackDrawHandel, -1);
                }
                menuItemTrack.Tag = false;
            }
            #endregion
            #region draw depot
            if ((bool)menuItemDeport.Tag)
            {
                if (menuItemDeport.Checked)
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.DepotDrawHandle, -1);

                    _dictdrawHandels[DrawHandels.DepotDrawHandle] = axMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);
                    axMap.DrawingFont(_dictdrawHandels[DrawHandels.DepotDrawHandle], "Tahoma", 8);
                    for (int i = 0; i < _arrDepotsx.Count; i++)
                    {
                        DepotsSX mpt = (DepotsSX)_arrDepotsx[i];
                        if (0 == mpt._pt.x && 0 == mpt._pt.y)
                        {
                            continue;
                        }
                        axMap.DrawCircleEx(_dictdrawHandels[DrawHandels.DepotDrawHandle], mpt._pt.x, mpt._pt.y, 2, (uint)(ColorTranslator.ToOle(Color.Black)), true);
                        AddDrawingLableDepot(_dictdrawHandels[DrawHandels.DepotDrawHandle], mpt._name, (uint)ColorTranslator.ToOle(Color.Green), mpt._pt.x, mpt._pt.y, tkHJustification.hjNone);
                    }
                    for (int i = 0; i < _arrDepotxx.Count; i++)
                    {
                        DepotsXX mpt = (DepotsXX)_arrDepotxx[i];
                        if (0 == mpt._pt.x && 0 == mpt._pt.y)
                        {
                            continue;
                        }
                        axMap.DrawCircleEx(_dictdrawHandels[DrawHandels.DepotDrawHandle], mpt._pt.x, mpt._pt.y, 2, (uint)(ColorTranslator.ToOle(Color.Purple)), true);
                        AddDrawingLableDepot(_dictdrawHandels[DrawHandels.DepotDrawHandle], mpt._name, (uint)ColorTranslator.ToOle(Color.FromArgb(0x6C, 0x6C, 0x6C)), mpt._pt.x, mpt._pt.y, tkHJustification.hjNone);
                    }
                    axMap.set_UseDrawingLabelCollision(_dictdrawHandels[DrawHandels.DepotDrawHandle], true);
                }
                else
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.DepotDrawHandle, -1);
                }
                menuItemDeport.Tag = false;
            }
            #endregion
            #region draw swerve
            if ((bool)menuItemSwerve.Tag)
            {
                if (menuItemSwerve.Checked)
                {
                    Dictionary<int, int> fonts = new Dictionary<int, int>();
                    Removelayer(ref _idx_shafile_swerve);
                    Removelayer(ref _idx_shafile_swerve_ico);

                    Shapefile shapefile_swerve = CreateShapefile("shapefile_swerve", ShpfileType.SHP_POINT, out _idx_shafile_swerve);
                    Shapefile shapefile_swerve_ico = CreateShapefile("shapefile_swerve_ico", ShpfileType.SHP_POINT, out _idx_shafile_swerve_ico);

                    List<MapWinGIS.Point> points = new List<MapWinGIS.Point>();
                    for (int i = 0; i < _arrSwerve.Count; i++)
                    {
                        Swerves sw = (Swerves)_arrSwerve[i];
                        MapWinGIS.Point point = new MapWinGIS.Point
                        {
                            x = sw._pt.x,
                            y = sw._pt.y,
                            Z = sw._ang
                        };
                        points.Add(point);
                    }
                    ShapefileInsertPoints(shapefile_swerve, points);
                    ShapefileInsertPoints(shapefile_swerve_ico, points);

                    SetLayerEsFont(shapefile_swerve, _idx_shafile_swerve, "ES_Font", 14, fonts, 255, false);
                    SetLayerEsFont(shapefile_swerve_ico, _idx_shafile_swerve_ico, "ES_Font", 8, (uint)(ColorTranslator.ToOle(Color.FromArgb(0x70, 0x70, 0x38))), false, 0x32);
                }
                else
                {
                    Removelayer(ref _idx_shafile_swerve);
                    Removelayer(ref _idx_shafile_swerve_ico);
                }
                menuItemSwerve.Tag = false;
            }
            #endregion
            #region draw speed
            if ((bool)menuItemSpeed.Tag)
            {
                if (menuItemSpeed.Checked)
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.SpeedHandel, -1);
                    Removelayer(ref _idx_shfile_speed);

                    _dictdrawHandels[DrawHandels.SpeedHandel] = axMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);
                    axMap.DrawingFont(_dictdrawHandels[DrawHandels.SpeedHandel], "Tahoma", 6);

                    Shapefile shapefile_speed = CreateShapefile("shapefile_broadcast", ShpfileType.SHP_POINT, out _idx_shfile_speed);
                    List<MapWinGIS.Point> points = new List<MapWinGIS.Point>();

                    axMap.set_UseDrawingLabelCollision(_dictdrawHandels[DrawHandels.SpeedHandel], true);
                    axMap.set_DrawingLabelsOffset(_dictdrawHandels[DrawHandels.SpeedHandel], 15);
                    for (int i = 0; i < _arrSpeed.Count; i++)
                    {
                        Speed sp = (Speed)_arrSpeed[i];
                        foreach (MPoint point in sp._points)
                        {
                            MapWinGIS.Point p = new MapWinGIS.Point
                            {
                                x = point.x,
                                y = point.y
                            };
                            points.Add(p);
                        }
                        for (int k = 0; k < sp._points.Count; k++)
                        {
                            axMap.AddDrawingLabel(_dictdrawHandels[DrawHandels.SpeedHandel], sp._speed.ToString(), (uint)ColorTranslator.ToOle(Color.OrangeRed), sp._points[k].x, sp._points[k].y, tkHJustification.hjNone);
                            if (k + 1 < sp._points.Count)
                            {
                                axMap.DrawLineEx(_dictdrawHandels[DrawHandels.SpeedHandel], sp._points[k].x, sp._points[k].y, sp._points[k + 1].x, sp._points[k + 1].y, 1,
                                    (uint)(ColorTranslator.ToOle(Color.FromArgb(0xFF, 0xC3, 0x45))));
                            }
                        }
                    }
                    ShapefileInsertPoints(shapefile_speed, points);
                    SetLayerEsFont(shapefile_speed, _idx_shfile_speed, "ES_Font", 7, (uint)(ColorTranslator.ToOle(Color.Purple)), false, 0x23);
                }
                else
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.SpeedHandel, -1);
                    Removelayer(ref _idx_shfile_speed);
                }
                menuItemSpeed.Tag = false;
            }
            #endregion
            #region draw broadcast
            if ((bool)menuItemBroadcast.Tag)
            {
                if (menuItemBroadcast.Checked)
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.BroadCastHandel, -1);
                    Removelayer(ref _idx_shfile_broadcast);

                    _dictdrawHandels[DrawHandels.BroadCastHandel] = axMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);
                    axMap.set_UseDrawingLabelCollision(_dictdrawHandels[DrawHandels.BroadCastHandel], true);
                    axMap.DrawingFont(_dictdrawHandels[DrawHandels.BroadCastHandel], "Tahoma", 8);
                    axMap.set_DrawingLabelsOffset(_dictdrawHandels[DrawHandels.BroadCastHandel], 15);

                    Shapefile shapefile_broadcast = CreateShapefile("shapefile_broadcast", ShpfileType.SHP_POINT, out _idx_shfile_broadcast);

                    List<MapWinGIS.Point> points = new List<MapWinGIS.Point>();
                    for (int i = 0; i < _arrBroadCast.Count; i++)
                    {
                        Broadcast bc = (Broadcast)_arrBroadCast[i];
                        MapWinGIS.Point point = new MapWinGIS.Point
                        {
                            x = bc._pt.x,
                            y = bc._pt.y,
                        };
                        points.Add(point);
                        string type = bc._type == "0" ? "安全信息" : "广告";
                        axMap.AddDrawingLabel(_dictdrawHandels[DrawHandels.BroadCastHandel], type, (uint)ColorTranslator.ToOle(Color.FromArgb(0xC4, 0xC4, 0)), bc._pt.x, bc._pt.y, tkHJustification.hjNone);
                    }
                    ShapefileInsertPoints(shapefile_broadcast, points);
                    SetLayerEsFont(shapefile_broadcast, _idx_shfile_broadcast, "ES_Font", 12, 255, false, 0x2F);
                }
                else
                {
                    ClearspecificDrawing(_dictdrawHandels, DrawHandels.BroadCastHandel, -1);
                    Removelayer(ref _idx_shfile_broadcast);
                }
                menuItemBroadcast.Tag = false;
            }
            #endregion
            #region draw measure line & point
            if (_measurePoints.Count > 0)
            {
                ClearspecificDrawing(_dictdrawHandels, DrawHandels.MeaSureHandel, -1);
                _dictdrawHandels[DrawHandels.MeaSureHandel] = axMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);
                for (int i = 0; i < _measurePoints.Count; i++)
                {
                    MPoint point = _measurePoints[i];
                    for (int k = 0; k < _measurePoints.Count; k++)
                    {
                        if (k + 1 < _measurePoints.Count)
                        {
                            axMap.DrawLineEx(_dictdrawHandels[DrawHandels.MeaSureHandel], _measurePoints[k].x, _measurePoints[k].y, _measurePoints[k + 1].x, _measurePoints[k + 1].y, 1,
                                (uint)(ColorTranslator.ToOle(Color.FromArgb(0x64, 0x21, 0))));
                        }
                        axMap.DrawCircleEx(_dictdrawHandels[DrawHandels.MeaSureHandel], _measurePoints[k].x, _measurePoints[k].y, 1.8, 255, true);
                    }
                }
            }
            #endregion
            #region draw ang
            if ((bool)menuItemAng.Tag)
            {
                if (menuItemAng.Checked)
                {
                    if (_idx_shpfile_ang > 1)
                    {
                        axMap.RemoveLayer(_idx_shpfile_ang);
                        _idx_shpfile_ang = -1;
                    }
                    Dictionary<int, int> fonts = new Dictionary<int, int>();
                    Shapefile shapefile_ang = CreateShapefile("shapefile_ang", ShpfileType.SHP_POINT, out _idx_shpfile_ang);

                    List<MapWinGIS.Point> points = new List<MapWinGIS.Point>();
                    for (int i = 0; i < _arrDepotsx.Count; i++)
                    {
                        DepotsSX mpt = (DepotsSX)_arrDepotsx[i];
                        if(0 == mpt._pt.x && 0 == mpt._pt.y)
                        {
                            continue;
                        }
                        MapWinGIS.Point point = new MapWinGIS.Point()
                        {
                            x = mpt._pt.x,
                            y = mpt._pt.y,
                            Z = mpt._ang,
                        };
                        points.Add(point);
                    }
                    for (int i = 0; i < _arrDepotxx.Count; i++)
                    {
                        DepotsXX mpt = (DepotsXX)_arrDepotxx[i];
                        if (0 == mpt._pt.x && 0 == mpt._pt.y)
                        {
                            continue;
                        }
                        MapWinGIS.Point point = new MapWinGIS.Point()
                        {
                            x = mpt._pt.x,
                            y = mpt._pt.y,
                            Z = mpt._ang,
                        };
                        points.Add(point);
                    }

                    ShapefileInsertPoints(shapefile_ang, points);
                    SetLayerEsFont(shapefile_ang, _idx_shpfile_ang, "ES_Font", 14, fonts, 255, false);
                }
                else
                {
                    if (_idx_shpfile_ang > 1)
                    {
                        axMap.RemoveLayer(_idx_shpfile_ang);
                        _idx_shpfile_ang = -1;
                    }
                }
                menuItemAng.Tag = false;
            }
            #endregion
            if (tsChBox.Checked && _ppt.x != 0.0 && _ppt.x != 0.0)
            {
                CenterAt(_ppt);
            }
        }
        //创建一个shapefile
        private Shapefile CreateShapefile(string shName, ShpfileType type, out int index)
        {
            Shapefile shapefile = new Shapefile();
            shapefile.CreateNew(shName, ShpfileType.SHP_POINT);
            index = axMap.AddLayer(shapefile, true);
            return shapefile;
        }
        //给Shapefile添加点
        private void ShapefileInsertPoints(Shapefile sh, List<MapWinGIS.Point> points)
        {
            foreach (MapWinGIS.Point point in points)
            {
                Shape shape = new Shape();
                shape.Create(ShpfileType.SHP_POINT);
                int idxshape_ang_SX = 0;
                int pointidx = 0;
                shape.InsertPoint(point, ref pointidx);
                sh.EditInsertShape(shape, ref idxshape_ang_SX);
            }
        }
        //地图以某点为中心
        private void CenterAt(MPoint _ppt)
        {
            MapWinGIS.Extents myExtents = (Extents)axMap.Extents;
            MPoint center = new MPoint
            {
                x = (myExtents.xMax + myExtents.xMin) / 2,
                y = (myExtents.yMax + myExtents.yMin) / 2
            };
            double _x = center.x - _ppt.x;
            double _y = center.y - _ppt.y;

            double minX = myExtents.xMin - _x;
            double maxX = myExtents.xMax - _x;
            double minY = myExtents.yMin - _y;
            double maxY = myExtents.yMax - _y;

            myExtents.SetBounds(minX, minY, 0, maxX, maxY, 0);
            axMap.Extents = myExtents;
        }
        //显示全局
        private void ShowFullPath()
        {
            if (_minx == 360.0)
            {
                _minx = 53.0;
            }
            if (_maxx == 0.0)
            {
                _maxx = 135.0;
            }
            if (_miny == 360.0)
            {
                _miny = 18.0;
            }
            if (_maxy == 0)
            {
                _maxy = 53.0;
            }
            MapWinGIS.Extents myExtents = (Extents)axMap.Extents;
            myExtents.SetBounds(_minx - 0.003, _miny - 0.003, 0, _maxx + 0.003, _maxy + 0.003, 0);
            axMap.Extents = myExtents;
        }
        //ToolStripMenuItem反选
        private void menuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem ts = sender as ToolStripMenuItem;
            ts.Checked = !ts.Checked;
            ts.Tag = true;
            axMap_AfterTrackingLayerDraw();
        }
        //显示地图
        private void btnShowMap_ItemClick(object sender, ItemClickEventArgs e)
        {
            dockPanelMap.Show();
            gvDepots.OptionsView.ColumnAutoWidth = false;
        }
        //关闭地图
        private void btnClosMap_ItemClick(object sender, ItemClickEventArgs e)
        {
            dockPanelMap.Close();
            gvDepots.OptionsView.ColumnAutoWidth = true;
        }
        //定位
        private void tsBtnPointTo_Click(object sender, EventArgs e)
        {
            Pointo(tsTextPoint.Text.Trim());
        }
        //定位某个点
        private void Pointo(string gpspt)
        {
            if (0 == gpspt.Length)
            {
                return;
            }
            try
            {
                string delimStr = ",";
                char[] sp = delimStr.ToCharArray();
                List<string> ss = new List<string>();
                string[] ssn = null;
                ssn = gpspt.Split(sp);
                for (int i = 0; i < ssn.Length; i++)
                {
                    string s = ssn[i].Trim();
                    if (s == "," || s == ":" || s.Length <= 0)
                    {
                        continue;
                    }
                    ss.Add(s);
                }
                if (ss.Count != 2)
                {
                    XtraMessageBox.Show("请输入一对经纬度值!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (Convert.ToDouble(ss[0]) > 135.0 || Convert.ToDouble(ss[0]) < 74.0 || Convert.ToDouble(ss[1]) > 53.0 || Convert.ToDouble(ss[1]) < 18.0)
                {
                    XtraMessageBox.Show("请输入一个有效的中国范围内的经纬度值!", "经纬度超出范围", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                double x = Convert.ToDouble(ss[0]);
                double y = Convert.ToDouble(ss[1]);
                MapWinGIS.Point pt = new MapWinGIS.Point();
                pt.x = x;
                pt.y = y;
                MapWinGIS.Extents myExtents = (Extents)axMap.Extents;
                myExtents.SetBounds(pt.x - 0.03, pt.y - 0.03, 0, pt.x + 0.03, pt.y + 0.03, 0);
                axMap.Extents = myExtents;
                ClearspecificDrawing(_dictdrawHandels, DrawHandels.PointToHandel, -1);
                _dictdrawHandels[DrawHandels.PointToHandel] = axMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);
                axMap.AddDrawingLabel(_dictdrawHandels[DrawHandels.PointToHandel], "", 255, pt.x, pt.y, tkHJustification.hjNone);
                new System.Threading.Thread(new System.Threading.ThreadStart(ClearPoint)).Start();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return;
            }
        }
        //清除定位点
        private void ClearPoint()
        {
            System.Threading.Thread.Sleep(3000);
            axMap.ClearDrawing(_dictdrawHandels[DrawHandels.PointToHandel]);
        }
        //站点选择改变
        private void gvDepots_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            object datasouse;
            if ("FK_Depots" == gc.DataMember)
            {
                datasouse = (sender as DevExpress.XtraGrid.Views.BandedGrid.BandedGridView).DataSource;
            }
            else
            {
                datasouse = (sender as DevExpress.XtraGrid.Views.Grid.GridView).DataSource;
            }
            BindingSource bs = (BindingSource)datasouse;
            if (null != bs)
            {
                if (null != bs.Current)
                {
                    DataRowView drv = (DataRowView)bs.Current;
                    _ppt.x = Convert.ToDouble(drv.Row["经度"]) / 100000;
                    _ppt.y = Convert.ToDouble(drv.Row["纬度"]) / 100000;
                    SetAllMenuItmeTags();
                    axMap_AfterTrackingLayerDraw();
                }
            }
        }
        //自动定位勾选
        private void tsChBox_CheckedChanged(object sender, EventArgs e)
        {
            if (tsChBox.Checked)
            {
                SetAllMenuItmeTags();
                axMap_AfterTrackingLayerDraw();
            }
        }
        //鼠标滚珠滚动
        private void dockPanelMap_MouseWheel(object sender, MouseEventArgs e)
        {
            if (CurState.ChgGpsDirection == _cur)
            {
                if ("FK_Depots" == gc.DataMember)
                {
                    if (!(menuItemAng.Checked))
                    {
                        return;
                    }
                    ChgDirection(gvDepots.DataSource, e, false);
                    ShowInMap(false, true, true, false, false, false);
                }
                else if ("FK_Swerve" == gc.DataMember)
                {
                    ChgDirection(gvSwerve.DataSource, e, true);
                    ShowInMap(false, false, false, true, false, false);
                }
                axMap_AfterTrackingLayerDraw();
            }
        }
        //改变GPS方位
        private void ChgDirection(object dataSource, MouseEventArgs e, bool swerve)
        {
            BindingSource bs = (BindingSource)dataSource;
            if (null != bs)
            {
                if (null != bs.Current)
                {
                    DataRowView drv = (DataRowView)bs.Current;
                    int value = (int)drv.Row["角度"];
                    if (e.Delta > 0)
                    {
                        value = value + 5 >= 360 ? 0 : value + 5;
                    }
                    else if (e.Delta < 0)
                    {
                        value = value - 5 < 0 ? 359 : value - 5;
                    }
                    drv.Row["角度"] = value;
                    menuItemAng.Tag = true;
                    if (swerve)
                    {
                        menuItemSwerve.Tag = true;
                    }
                }
            }

        }
        //清除测距点
        private void ClearMeasurePoints()
        {
            int count = _measurePoints.Count;
            for (int i = 0; i < count; i++)
            {
                _measurePoints.RemoveAt(0);
            }
            axMap.Refresh();
        }
        //导航条按钮点击
        private void gc_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (null != e.Button.Tag)
            {
                if (0 == (int)e.Button.Tag)
                {
                    axMap.MapCursor = tkCursor.crsrCross;
                    if ("FK_Swerve" == gc.DataMember)
                    {
                        _cur = CurState.AddSwerve;
                    }
                    else if ("FK_Depots" == gc.DataMember)
                    {
                        _cur = CurState.SetDepotGps;
                    }
                    else if ("FK_Speed" == gc.DataMember)
                    {
                        AddSpeedControlCustomerBtnClick();
                        axMap.CursorMode = tkCursorMode.cmNone;
                    }
                    else if ("FK_Broadcast" == gc.DataMember)
                    {
                        _cur = CurState.SetBroadcastGps;
                    }
                }
                else if (1 == (int)e.Button.Tag)
                {
                    axMap.MapCursor = tkCursor.crsrCross;
                    if ("FK_Swerve" == gc.DataMember)
                    {
                        _cur = CurState.SetSwerveGps;
                    }
                    else if ("FK_Speed" == gc.DataMember)
                    {
                        _cur = CurState.ExtendSpeedArea;
                    }
                }
                else if (2 == (int)e.Button.Tag)
                {
                    axMap.CursorMode = tkCursorMode.cmNone;
                    if ("FK_Swerve" == gc.DataMember)
                    {
                        DelSwerveControlCustomerBtnClick();
                    }
                    else if ("FK_Speed" == gc.DataMember)
                    {
                        DelSpeedControlCustomerBtnClick();
                    }
                    else if ("FK_Broadcast" == gc.DataMember)
                    {
                        DelBroadCastControlCustomerBtnClick();
                    }
                }
                else if (3 == (int)e.Button.Tag)
                {
                    if ("FK_Depots" == gc.DataMember)
                    {
                        UpDepotControlCustomerBtnClick();
                    }
                }
                else if (4 == (int)e.Button.Tag)
                {
                    if ("FK_Depots" == gc.DataMember)
                    {
                        DownDepotControlCumtomerBtnClick();
                    }
                }
                else if (5 == (int)e.Button.Tag)
                {
                    gvDepots.OptionsView.ColumnAutoWidth = !gvDepots.OptionsView.ColumnAutoWidth;
                }
            }
        }
        //改变GPS方位
        private void ChgGVGps(object datasource, MPoint pt)
        {
            BindingSource bs = (BindingSource)datasource;
            if (null == bs.Current)
            {
                return;
            }
            DataRowView drv = (DataRowView)bs.Current;
            drv.Row["经度"] = Convert.ToInt32(pt.x * 100000);
            drv.Row["纬度"] = Convert.ToInt32(pt.y * 100000);
            _ppt.x = pt.x;
            _ppt.y = pt.y;
        }
        //设置某图层的字体为ES_Font
        private void SetLayerEsFont(Shapefile sp, int spIdx, string fontName, int fontsize, Dictionary<int, int> fonts, uint color, bool isBold)
        {
            axMap.set_ShapeLayerPointType(spIdx, MapWinGIS.tkPointType.ptFontChar);
            axMap.set_UDPointFontCharFont(spIdx, fontName, fontsize, isBold, false, false);
            for (int i = 0x7B; i > 0x32; i--)
            {
                int idx = axMap.set_UDPointFontCharListAdd(spIdx, i, color);
                fonts.Add((0x7B - i) * 5, idx);
            }
            int numShapes = sp.NumShapes;
            for (int i = 0; i < numShapes; i++)
            {
                MapWinGIS.Shape sh = sp.get_Shape(i);
                MapWinGIS.Point point = sh.get_Point(0);

                int ang = ((int)point.Z / 5) * 5;
                int idx = fonts[ang];
                axMap.set_ShapePointType(spIdx, i, MapWinGIS.tkPointType.ptFontChar);
                axMap.set_ShapePointFontCharListID(spIdx, i, idx);
            }
        }
        //SetLayerEsFont重载
        private void SetLayerEsFont(Shapefile sp, int spIdx, string fontName, int fontsize, uint color, bool isBold, int val)
        {
            axMap.set_ShapeLayerPointType(spIdx, MapWinGIS.tkPointType.ptFontChar);
            axMap.set_UDPointFontCharFont(spIdx, fontName, fontsize, isBold, false, false);
            int idx = axMap.set_UDPointFontCharListAdd(spIdx, val, color);

            int numShapes = sp.NumShapes;
            for (int i = 0; i < numShapes; i++)
            {
                axMap.set_ShapePointType(spIdx, i, MapWinGIS.tkPointType.ptFontChar);
                axMap.set_ShapePointFontCharListID(spIdx, i, idx);
            }
        }
        //移除指定Layer
        private void Removelayer(ref int idx)
        {
            if (idx > 1)
            {
                axMap.RemoveLayer(idx);
                idx = -1;
            }
        }
        //ShapeFile添加标签
        private void ShapeFileAddLabel(int hndl, int field, UInt32 col)
        {
            MapWinGIS.Shapefile sf;
            MapWinGIS.Shape sh = new MapWinGIS.Shape();
            double x, y;
            string text;

            sf = (MapWinGIS.Shapefile)axMap.get_GetObject(hndl);

            for (int i = 0; i < sf.NumShapes; i++)
            {
                sh = sf.get_Shape(i);
                text = sf.get_CellValue(field, i).ToString();
                MapWinGIS.Extents ext = sf.QuickExtents(i);
                x = (ext.xMax + ext.xMin) / 2;
                y = (ext.yMax + ext.yMin) / 2;
                axMap.AddLabel(hndl, text, col, x, y, MapWinGIS.tkHJustification.hjCenter);
            }
        }
        //Depot添加一个标签
        private void AddDrawingLableDepot(int handle, string drawTxt, uint color, double x, double y, tkHJustification fication)
        {
            axMap.AddDrawingLabel(handle, drawTxt, color, x, y, fication);
            axMap.set_DrawingLabelsShadow(handle, true);
            axMap.set_DrawingLabelsShadowColor(handle, (uint)(ColorTranslator.ToOle(Color.FromArgb(0xEC, 0xF5, 0xFF))));
            axMap.set_DrawingLabelsOffset(handle, 20);
        }
        //清除指定图层的绘制
        private void ClearspecificDrawing(Dictionary<DrawHandels, int> dict, DrawHandels handel, int val)
        {
            axMap.ClearDrawing(dict[handel]);
            dict[handel] = val;
        }
        #endregion
原文地址:https://www.cnblogs.com/kakaliush/p/2981507.html