Click here to Skip to main content
15,884,628 members
Articles / Desktop Programming / Windows Forms

Earth Map in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (14 votes)
25 Aug 2009CPOL2 min read 75.2K   7.1K   35   17
Detecting coordinate point on the raster image map
Image 1

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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Iran (Islamic Republic of) 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!!!

Comments and Discussions

 
QuestionThank you-easy to load, compile and understand - well done Pin
Member 1494757813-Jan-22 15:29
Member 1494757813-Jan-22 15:29 
QuestionProblem drawicon Pin
Member 1186482229-Oct-15 9:01
Member 1186482229-Oct-15 9:01 
Questionتشکر Pin
Member 1166132610-May-15 13:00
Member 1166132610-May-15 13:00 
QuestionProblem: Pin
yatharthz23-Nov-13 21:11
yatharthz23-Nov-13 21:11 
Questionسلام Pin
AP11224-Apr-13 5:07
AP11224-Apr-13 5:07 
Questionmer30 Pin
ahmadRAD31-Dec-12 22:38
ahmadRAD31-Dec-12 22:38 
QuestionWhat is the map projection you are using? Pin
wedelthomas28-Nov-12 5:20
wedelthomas28-Nov-12 5:20 
Questionتشکر Pin
naser200018-Sep-12 8:55
naser200018-Sep-12 8:55 
GeneralIcon doesnt show Pin
jasonkkz17-Sep-09 21:47
jasonkkz17-Sep-09 21:47 
GeneralRe: Icon doesnt show Pin
Reza Shojaee20-Sep-09 20:56
Reza Shojaee20-Sep-09 20:56 
Generalgood program... Pin
mohammad-mirshahi9-Sep-09 10:58
mohammad-mirshahi9-Sep-09 10:58 
GeneralComplete the code ! Pin
pingofgs1-Sep-09 10:01
pingofgs1-Sep-09 10:01 
GeneralRe: Complete the code ! Pin
Reza Shojaee20-Sep-09 20:44
Reza Shojaee20-Sep-09 20:44 
Generalgot a similar solution in as3 Pin
BitBarbeQue31-Aug-09 9:14
BitBarbeQue31-Aug-09 9:14 
GeneralRe: got a similar solution in as3 Pin
Reza Shojaee20-Sep-09 21:07
Reza Shojaee20-Sep-09 21:07 
Generalwell..few comments Pin
Md. Marufuzzaman25-Aug-09 3:06
professionalMd. Marufuzzaman25-Aug-09 3:06 
JokeRe: well..few comments Pin
dethtroll25-Aug-09 3:11
dethtroll25-Aug-09 3:11 
I think this code draws icon or rectangle in some point of PictureBox

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.