📄 map.cs
字号:
}
/// <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="Latitude">纬度:0~90</param>
/// <param name="LatitudeType">纬度类型:南或北</param>
/// <param name="Longitude">经度:0~180</param>
/// <param name="LongitudeType">经度类型:西或东</param>
/// <param name="Height">海拔高度</param>
public void SetPosition(double Latitude, LatitudeType LatitudeType, double Longitude, LongitudeType LongitudeType, double Height,string carSpeed)
{
int halfWidthInt = (maxSize.Width + 1) / 2,
halfHeightInt = (maxSize.Height + 1) / 2;
float halfWidthF = realSize.Width / 2,
halfHeightF = realSize.Height / 2;
// 计算在地图上的坐标
Point cp = new Point();
cp.Y = (LatitudeType == LatitudeType.North) ? (halfHeightInt - (int)(halfHeightInt * Latitude / 90.0)) : (halfHeightInt + (int)(halfHeightInt * Latitude / 90.0));
cp.X = (LongitudeType == LongitudeType.West) ? (halfWidthInt - (int)(halfWidthInt * Longitude / 180.0)) : (halfWidthInt + (int)(halfWidthInt * Longitude / 180.0));
// 计算在真实地图的位置
PointF curRealPoint = new PointF((float)((LongitudeType == LongitudeType.West) ? (halfWidthF - halfWidthF * Longitude / 180.0) : (halfWidthF + halfWidthF * Longitude / 180.0)),
(float)((LatitudeType == LatitudeType.North) ? (halfHeightF - halfHeightF * Latitude / 90.0) : (halfHeightF + halfHeightF * Latitude / 90.0)));
// 计算速度
//speed = Math.Sqrt(Math.Pow(curRealPoint.X - lstRealPoint.X, 2) + Math.Pow(curRealPoint.Y - lstRealPoint.Y, 2)) / 3;
//speed = Math.Sqrt(Math.Pow((curPoint.X - lstPoint.X)*50, 2) + Math.Pow((curPoint.Y - lstPoint.Y)*50, 2)) / 2;
speed = double.Parse(carSpeed);
// 保存以计算速度
lstRealPoint = curRealPoint;
lstPoint = curPoint;
// 定位信息记录
this.Latitude = Latitude;
this.LatitudeType = LatitudeType;
this.Height = Height;
this.Longitude = Longitude;
this.LongitudeType = LongitudeType;
// 定位信息记录
this.CurPoint = cp;
}
#region position
public void SetPosition(double Latitude, LatitudeType LatitudeType, double Longitude, LongitudeType LongitudeType, double Height)
{
int halfWidthInt = (maxSize.Width + 1) / 2,
halfHeightInt = (maxSize.Height + 1) / 2;
float halfWidthF = realSize.Width / 2,
halfHeightF = realSize.Height / 2;
// 计算在地图上的坐标
Point cp = new Point();
cp.Y = (LatitudeType == LatitudeType.North) ? (halfHeightInt - (int)(halfHeightInt * Latitude / 90.0)) : (halfHeightInt + (int)(halfHeightInt * Latitude / 90.0));
cp.X = (LongitudeType == LongitudeType.West) ? (halfWidthInt - (int)(halfWidthInt * Longitude / 180.0)) : (halfWidthInt + (int)(halfWidthInt * Longitude / 180.0));
// 计算在真实地图的位置
PointF curRealPoint = new PointF((float)((LongitudeType == LongitudeType.West) ? (halfWidthF - halfWidthF * Longitude / 180.0) : (halfWidthF + halfWidthF * Longitude / 180.0)),
(float)((LatitudeType == LatitudeType.North) ? (halfHeightF - halfHeightF * Latitude / 90.0) : (halfHeightF + halfHeightF * Latitude / 90.0)));
// 计算速度
//speed = Math.Sqrt(Math.Pow(curRealPoint.X - lstRealPoint.X, 2) + Math.Pow(curRealPoint.Y - lstRealPoint.Y, 2)) / 3;
speed = Math.Sqrt(Math.Pow((curPoint.X - lstPoint.X) * 50, 2) + Math.Pow((curPoint.Y - lstPoint.Y) * 50, 2)) / 2;
// 保存以计算速度
lstRealPoint = curRealPoint;
lstPoint = curPoint;
// 定位信息记录
this.Latitude = Latitude;
this.LatitudeType = LatitudeType;
this.Height = Height;
this.Longitude = Longitude;
this.LongitudeType = LongitudeType;
// 定位信息记录
this.CurPoint = cp;
}
#endregion
/// <summary>
/// 更新界面
/// </summary>
public void Draw()
{
Draw(curPosition.point.X, curPosition.point.Y, curPosition.IndexOfMap, angle_direct, ref left_top, clientSize, Latitude, LatitudeType, Longitude, LongitudeType, Height,speed);
}
/// <summary>
/// 带参数的更新界面函数
/// </summary>
/// <param name="x">地图坐标X</param>
/// <param name="y">地图坐标Y</param>
/// <param name="IndexOfMap">地图编号</param>
/// <param name="angle_dr">方向角度</param>
/// <param name="ClientSize">显示域的大小</param>
/// <param name="lt">左上角的起始座标</param>
/// <param name="disLatitude">显示纬度</param>
/// <param name="disLatitudeType">显示纬度类型</param>
/// <param name="disLongitude">显示经度</param>
/// <param name="disLongitudeType">显示经度类型</param>
/// <param name="disHeight">显示高度</param>
/// <param name="disSpeed">显示速度</param>
private void Draw(int x, int y, int IndexOfMap, float angle_dr,
ref Point lt,Size ClientSize,
double disLatitude, LatitudeType disLatitudeType, double disLongitude, LongitudeType disLongitudeType, double disHeight,double disSpeed)
{
GraphicsContainer contrainer = graphics.BeginContainer();
{
// 地图
Image map = Image.FromFile(MapPath + "m_chn_" + IndexOfMap.ToString("d2") + ".gif");
// 调整坐标
if (lt.X < 0)
lt.X = 0;
else
if (lt.X > map.Size.Width - clientSize.Width)
lt.X = map.Size.Width - clientSize.Width;
if (lt.Y < 0)
lt.Y = 0;
else
if (lt.Y > map.Size.Height - clientSize.Height)
lt.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 (this.traceRecord.ShowTrace)
{
List<Point> ps = this.traceRecord[curPosition.IndexOfMap];
if (ps != null)
g.DrawLines(new Pen(Brushes.Blue, 2), ps.ToArray());
}
// 输出到界面
graphics.DrawImage(bmp, 0, 0, new Rectangle(lt.X, lt.Y, ClientSize.Width, ClientSize.Height), GraphicsUnit.Pixel);
// 显示文字提示
string disString = "纬度:" + Math.Round(disLatitude, 3).ToString() + ((char)(LatitudeType)).ToString() + "\n"
+ "经度:" + Math.Round(disLongitude, 3).ToString() + ((char)disLongitudeType).ToString() + "\n"
+ "海拔:" + Math.Round(disHeight, 3).ToString() + "\n"
+ "速度:" + Math.Round(speed, 3).ToString() + " km/s";
graphics.DrawString(disString, new Font("宋体", 8), Brushes.Purple, new PointF(this.clientSize.Width - 100, 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); // 容器还原
}
/// <summary>
/// 清除轨迹
/// </summary>
public void ClearTrace()
{
this.traceRecord.Clear();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -