C# 中的地球地图





4.00/5 (14投票s)
在栅格图像地图上检测坐标点

引言
为了开发一个特殊的应用程序,我需要一个C#的地球地图或地图组件,能够将一个点或图标绘制在任意经度和纬度上。我在CodeProject和其他编程社区中搜索过,但几乎所有的示例都是为GIS解决方案设计的,并且使用shape文件,还有一些项目是为Google地图实现的。因此,我决定开发我自己的应用程序。
背景
首先,我将讨论图形部分。为了实现这个应用程序,我同时使用Graphics
类和OnPaint
事件。因为在某些情况下,我需要绘制和清除地理网格,所以我将两者结合起来使用。
Using the Code
在地理地图中,我们有360度的经度和90度的纬度,为了将每个像素映射到合适的坐标点,我使用了宽度为720像素、高度为360像素的面板。因此,当鼠标在地图面板上移动时,我读取像素坐标点,然后除以二,计算出相应的纬度和经度。插入鼠标移动以及相反的操作,当您输入坐标点值时,您可以在地图上看到图标或彩色点。以下是与图标和点相关的源代码:
private void Map_Panel_Paint(object sender, PaintEventArgs e)
{
// Checking for drawing grid, point and icon
#region Draw Point
if (drawpoint)
{
double lon = Convert.ToDouble(long_t.Text);
double lat = Convert.ToDouble(lat_t.Text);
if (lon > 180 || lon < -180)
{
drawpoint = false;
MessageBox.Show("Invalid Longitude");
return;
}
if (lat > 90 || lat < -90)
{
drawpoint = false;
MessageBox.Show("Invalid Latitude");
return;
}
e.Graphics.FillEllipse(Brushes.Yellow, (float)((lon * 2) + 360 -
2), (float)(180 - (lat * 2) - 2), 5, 5);
}
#endregion
#region Draw Icon
if (drawicon)
{
double lon = Convert.ToDouble(long_t.Text);
double lat = Convert.ToDouble(lat_t.Text);
if (lon > 180 || lon < -180)
{
drawicon = false;
MessageBox.Show("Invalid Longitude");
return;
}
if (lat > 90 || lat < -90)
{
drawicon = false;
MessageBox.Show("Invalid Latitude");
return;
}
e.Graphics.DrawImage((Image)(Graphic_Map.Properties.Resources.icon),
new Rectangle((int)((lon * 2) + 360 - 11),
(int)(180 - (lat * 2) - 11), 23, 23));
}
#endregion
#region Draw Grid
if (Grid_ch.Checked == true)
{
Pen pen = new Pen(Color.DodgerBlue);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
Point point1 = new Point();
Point point2 = new Point();
for (int i = -150; i < 160; i += 30)
{
point1.X = ((i * 2) + 360);
point1.Y = (180 - (90 * 2));
point2.X = ((i * 2) + 360);
point2.Y = (180 - (-90 * 2));
e.Graphics.DrawLine(pen, point1, point2);
}
for (int i = -60; i < 70; i += 30)
{
point1.X = ((-180 * 2) + 360);
point1.Y = (180 - (i * 2));
point2.X = ((180 * 2) + 360);
point2.Y = (180 - (i * 2));
e.Graphics.DrawLine(pen, point1, point2);
}
}
#endregion
}
//////////////////////////////////////////////////////////////
private void Map_Panel_MouseMove(object sender, MouseEventArgs e)
{
// Show appropriate coordinate point on theremoved of map panel
double temp1 = e.X / 2.0;
if (temp1 >= 180)
{
EW_l.Text = "East";
long_l.Text = (temp1 - 180).ToString();
}
else
{
EW_l.Text = "West";
long_l.Text = (180 - temp1).ToString();
}
double temp2 = e.Y / 2.0;
if (temp2 <= 90)
{
NS_l.Text = "North";
lat_l.Text = (90 - temp2).ToString();
}
else
{
NS_l.Text = "South";
lat_l.Text = (temp2 - 90).ToString();
}
}
关注点
当前项目旨在介绍一个地理地图。当鼠标悬停在地图上时,您可以在地图框架底部看到每个点的纬度和经度,反之,您可以在文本框中输入纬度和经度值,并在地图上以图标或彩色点的方式显示输入的点。该软件的另一个功能是在地图上绘制纬度和经度网格,以及使用新的图片更改地图。
历史
- 2009年8月25日:初始发布