⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oaaaaaaa.a

📁 gps
💻 A
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GPSClassLibrary
{
    /// <summary>
    /// 图片号,坐标的结果
    /// </summary>
    public struct MapPoint
    {
        public Point point;// 
        public int IndexOfMap;// 
        public MapPoint(Point p, int idx)
        {
            point = p;
            IndexOfMap = idx;
        }
        public MapPoint(MapPoint obj)
        {
            point = obj.point;
            IndexOfMap = obj.IndexOfMap;
        }
        /// <summary>
        /// MapPoint 的!= 定义
        /// </summary>
        /// <param name="A">左操作数</param>
        /// <param name="B">右操作数</param>
        /// <returns>不等为真,等为假</returns>
        public static bool operator != (MapPoint A,MapPoint B)
        {
            if (A.Equals(B))
                return false;
            return true;
        }
        /// <summary>
        /// 等于判断
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static bool operator ==(MapPoint A, MapPoint B)
        {
            if (A.Equals(B))
                return true;
            return false;
        }
        /// <summary>
        /// Equals重载
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            MapPoint B = (MapPoint)obj;
            if (point == B.point && IndexOfMap == B.IndexOfMap)
                return true;
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
    public class Map
    {
        // 轨迹记录
        List<MapPoint> Trace = new List<MapPoint>();
        // 是否显示轨迹
        bool ShowTrace = true;

        // 最大坐标和地图大小
        Size maxSize = new Size(4999, 4499);
        Size MapSize = new Size(1000, 900);
        #region Private
        // 现实位置
        Point realPoint = Point.Empty;
        // 当前图显位置
        MapPoint curPosition = new MapPoint(new Point(0, 0), 0);
        // 当前坐标
        Point curPoint = new Point(0, 0);
        // 前一坐标
        Point  lstPoint = new Point(0,0);
        // 方向角度
        float angle_direct = 0.0f;
        // 图形设备
        Graphics graphics;
        // 地图左上角的点
        Point left_top;
        // 方向标图
        Image dr_gif;
        // 地图图片的径
        string MapPath;
        // 显示大小
        Size clientSize;
        // 是否定位现在位置
        bool InditeToDisplay = true;
        #endregion

        #region Attribute
        public Point RealPoint
        {
            set 
            { 
                this.CurPoint = new Point(value.X / 200, value.Y / 200);
                realPoint = value;
            }
        }
        /// <summary>
        /// 光标的当前坐标
        /// </summary>
        private Point CurPoint
        {
            set
            {
                lstPoint = curPoint;
                curPoint = value;
                // 坐标错误处理
                curPoint.X = curPoint.X < 0 ? 0 : curPoint.X;
                curPoint.Y = curPoint.Y < 0 ? 0 : curPoint.Y;
                curPoint.X = curPoint.X > maxSize.Width ? maxSize.Width : curPoint.X;
                curPoint.Y = curPoint.Y > maxSize.Height ? maxSize.Height : curPoint.Y;
                
                // 计算方向
                if (lstPoint != curPoint)
                {
                    int x = curPoint.X - lstPoint.X;
                    int y = curPoint.Y - lstPoint.Y;
                    if (x == 0)
                        angle_direct = y >= 0 ? 90.0f : -90.0f;
                    else if (y == 0)
                        angle_direct = x > 0 ? 0.0f : 180.0f;
                    else
                        angle_direct = (float)(Math.Atan(y / x) * 180.0 / Math.PI);
                }
                // 计算显示方位
                int i = curPoint.X / MapSize.Width, j = curPoint.Y / MapSize.Height;
                curPosition.IndexOfMap = i * 5 + j;
                curPosition.point = new Point(curPoint.X % MapSize.Width, curPoint.Y % MapSize.Height);

                // 跟踪显示
                if (InditeToDisplay)
                {
                    left_top.X = curPosition.point.X - clientSize.Width / 2;
                    left_top.Y = curPosition.point.Y - clientSize.Height / 2;
                }
                // 记录轨迹
                Trace.Add(new MapPoint(curPosition));
                Draw();
            }
            get { return curPoint; }
        }
        /// <summary>
        /// 地图左上角的起始点
        /// </summary>
        public Point Left_Top
        {
            set 
            {
                left_top = value;
                this.Draw();
            }
            get { return left_top; }
        }
        #endregion

        #region Construction
        public Map()
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="g">绘制的设备</param>
        /// <param name="dr">方向标图形</param>
        /// <param name="mapPath">地图的路径</param>
        /// <param name="size">绘制区大小</param>
        public Map(Graphics g, Image dr,string mapPath,Size size)
        {
            graphics = g;
            dr_gif = dr;
            MapPath = mapPath;
            clientSize = size;
        }
        #endregion

        #region Method
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x">地图坐标X</param>
        /// <param name="y">地图坐标Y</param>
        /// <param name="IndexOfMap">地图编号</param>
        /// <param name="disLatitude">显示纬度</param>
        /// <param name="disLatitudeType">显示纬度类型</param>
        /// <param name="disLongitude">显示经度</param>
        /// <param name="disLongitudeType">显示经度类型</param>
        /// <param name="disHeight">显示高度</param>
        public void Draw(int x, int y, int IndexOfMap, float angle_dr,double disLatitude, LatitudeType disLatitudeType, double disLongitude, LongitudeType disLongitudeType, double disHeight)
        {
            GraphicsContainer contrainer = graphics.BeginContainer();
            {
                // 地图
                Image map = Image.FromFile(MapPath + "m_chn_" + IndexOfMap.ToString("d2") + ".gif");
                // 调整坐标
                if (left_top.X < 0)
                    left_top.X = 0;
                else
                    if (left_top.X > map.Size.Width - clientSize.Width)
                        left_top.X = map.Size.Width - clientSize.Width;

                if (left_top.Y < 0)
                    left_top.Y = 0;
                else
                    if (left_top.Y > map.Size.Height - clientSize.Height)
                        left_top.Y = map.Size.Height - clientSize.Height;
                // 设置
                graphics.SmoothingMode = SmoothingMode.HighSpeed;
                
                // 方向标旋转
                Bitmap dr_bmp = new Bitmap(dr_gif);

                // 绘制
                Bitmap bmp = new Bitmap(map);
                Graphics g = Graphics.FromImage(bmp);
                //RotateDraw(g, dr_gif, angle_direct, curPosition.point.X - dr_gif.Width / 2, curPosition.point.Y - dr_gif.Height / 2, curPosition.point.X, curPosition.point.Y);
                RotateDraw(g, dr_gif, angle_dr, x - dr_gif.Width / 2, y - dr_gif.Height / 2, x, y);

                // 显示轨迹
                if (ShowTrace)
                {
                    List<Point> ps = new List<Point>();
                    foreach (MapPoint mp in Trace)
                        if (mp.IndexOfMap == curPosition.IndexOfMap)
                            ps.Add(new Point(mp.point.X, mp.point.Y));
                    if(ps.Count >1)
                        g.DrawLines(new Pen(Brushes.Blue,2), ps.ToArray());
                }

                // 输出到界面
                graphics.DrawImage(bmp, 0, 0, new Rectangle(left_top.X, left_top.Y,clientSize.Width, clientSize.Height), GraphicsUnit.Pixel);
                // 显示文字提示
                graphics.DrawString("地图" + curPosition.IndexOfMap.ToString() + ",(" + curPosition.point.X.ToString() + "," + curPosition.point.Y.ToString() + ")", new Font("宋体", 8), Brushes.Purple, new PointF(10.0f, 10.0f));
            }
            graphics.EndContainer(contrainer);
        }
        /// <summary>
        /// 进行旋转画图
        ///     图像image将以指定的旋转角度旋转后呈现在graphic上
        /// </summary>
        /// <param name="angle">图像偏转的角度0~360</param>
        /// <param name="image">要显示的图像</param>
        /// <param name="x">图像显示在图形设备上的位置x</param>
        /// <param name="y">图像显示在图形设备上的位置y</param>
        /// <param name="graphic">图形设备</param>
        /// <param name="ceterX">旋转的中心点x,相对于graphic</param>
        /// <param name="ceterY">旋转的中心点y,相对于graphic</param>
        public static void RotateDraw(Graphics graphic, Image image, float angle, int x, int y, int ceterX, int ceterY)
        {
            // 作图开始
            System.Drawing.Drawing2D.GraphicsContainer container = graphic.BeginContainer();    // 嵌套新容器
            {
                graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;

                // 防止图像旋转时溢出,取最大边
                int maxLen = image.Width;
                if (image.Height > image.Width) maxLen = image.Height;
                maxLen *= 2;        // 取2保证包含全部图像(> x*1.14,太大,有待改进)
                // 建立显示区域
                Rectangle rect = new Rectangle((image.Width - maxLen) / 2 + x, (image.Height - maxLen) / 2 + y, maxLen, maxLen);
                if (!rect.IsEmpty)
                    graphic.SetClip(rect, System.Drawing.Drawing2D.CombineMode.Intersect);   // 组合两个显示区域

                // 中心点
                Point centerPt = new Point(ceterX, ceterY);
                // 设置变换矩阵
                System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
                matrix.RotateAt(angle, centerPt, System.Drawing.Drawing2D.MatrixOrder.Append);    // 旋转
                graphic.Transform = matrix;

                graphic.DrawImage(image, x, y);
            }
            // 作图结束
            graphic.EndContainer(container);    // 容器还原
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -