Introduction
For developing a special application, I needed an earth map or map component in C# which was able to draw a point or icon in each arbitrary latitude and longitude. I searched on CodeProject and other programming communities, but almost all given samples were designed for GIS solution and worked with shape file and also other projects implemented for Google map. Thus I decided to develop my own application.
Background
First of all, I shall talk about the graphic part. For implementing this application, I use the Graphics
class and OnPaint
event simultaneously. Because in some cases, I need to draw and clear geographic grid, I combine both of them.
Using the Code
In the geographic map, we have a 360 degree longitude and 90 degree latitude, for mapping each pixel to the appropriate coordinate point; I used the panel with 720 pixel width and 360 pixel height. So when mouse moves over the map panel, I read the pixel coordinate point and after dividing by two, compute the corresponding latitude and longitude. Insert mouse move and the opposite, when you enter coordinate point value, you can see icon or colored point in the map. Below is the source code pertaining to icon and point:
private void Map_Panel_Paint(object sender, PaintEventArgs e)
{
#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)
{
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();
}
}
Points of Interest
In the current project, we attempt to introduce a geographic map. When the mouse is over on the map, you see the latitude and longitude of each point on the bottom of map frame, vice versa you can enter the latitude and longitude value in the text box and show the entered point in the map as an icon or a colored point. Another capability of this software is drawing the latitude and longitude grid on the map and also changing the map picture with the new ones.
History
- 25th August, 2009: Initial post