13,085,936 members (88,282 online)
alternative version

#### Stats

46.6K views
35 bookmarked
Posted 25 Aug 2009

# Earth Map in C#

, 25 Aug 2009
 Rate this:
Detecting coordinate point on the raster image map

## 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)
{
// 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();
}
}```

## 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

## Share

 Software Developer (Senior) Iran (Islamic Republic of)
I started programming computers in 2000 at the university and graduated as a computer engineer in 2004.
I have been working with Microsoft and Borland technologies. From 2004 I became an independent computer consultant. My clients have included wide range of corporations, software houses and consulting firms and my assignments have ranged from designing, to implementing, testing, documenting and then enhancing everything from small utilities to large systems.
Clinical Management Software is one of the best projects that implemented during these years. This is versatile software that handles medical tasks.

I enjoying from programming like as ROCK CLIMBING!!!

## You may also be interested in...

 First Prev Next
 Problem drawicon Member 1186482229-Oct-15 9:01 Member 11864822 29-Oct-15 9:01
 تشکر Member 1166132610-May-15 13:00 Member 11661326 10-May-15 13:00
 Problem: yatharthz23-Nov-13 21:11 yatharthz 23-Nov-13 21:11
 سلام AP11224-Apr-13 5:07 AP112 24-Apr-13 5:07
 What is the map projection you are using? wedelthomas28-Nov-12 5:20 wedelthomas 28-Nov-12 5:20
 تشکر naser200018-Sep-12 8:55 naser2000 18-Sep-12 8:55
 Icon doesnt show jasonkkz17-Sep-09 21:47 jasonkkz 17-Sep-09 21:47
 Re: Icon doesnt show Reza Shojaee20-Sep-09 20:56 Reza Shojaee 20-Sep-09 20:56
 Complete the code ! pingofgs1-Sep-09 10:01 pingofgs 1-Sep-09 10:01
 Re: Complete the code ! Reza Shojaee20-Sep-09 20:44 Reza Shojaee 20-Sep-09 20:44
 got a similar solution in as3 Member 314736531-Aug-09 9:14 Member 3147365 31-Aug-09 9:14
 Re: got a similar solution in as3 Reza Shojaee20-Sep-09 21:07 Reza Shojaee 20-Sep-09 21:07
 well..few comments Md. Marufuzzaman25-Aug-09 3:06 Md. Marufuzzaman 25-Aug-09 3:06
 Re: well..few comments sotona25-Aug-09 3:11 sotona 25-Aug-09 3:11
 Last Visit: 31-Dec-99 18:00     Last Update: 16-Aug-17 10:41 Refresh 1