Click here to Skip to main content
Click here to Skip to main content

Earth Map in C#

, 25 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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 

License

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

Share

About the Author

Reza Shojaee
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 got a professional job about aerospace in 2006 at a large company which continuing up to now.
I enjoying from programming like as ROCK CLIMBING!!!

Comments and Discussions

 
QuestionProblem: Pinmemberyatharthz23-Nov-13 22:11 
Questionسلام PinmemberAP11224-Apr-13 6:07 
Questionmer30 PinmemberahmadRAD31-Dec-12 23:38 
QuestionWhat is the map projection you are using? Pinmemberwedelthomas28-Nov-12 6:20 
Questionتشکر Pinmembernaser200018-Sep-12 9:55 
GeneralIcon doesnt show Pinmemberjasonkkz17-Sep-09 22:47 
GeneralRe: Icon doesnt show PinmemberReza Shojaee20-Sep-09 21:56 
Generalgood program... Pinmembermohammad-mirshahi9-Sep-09 11:58 
GeneralComplete the code ! Pinmemberpingofgs1-Sep-09 11:01 
GeneralRe: Complete the code ! PinmemberReza Shojaee20-Sep-09 21:44 
Generalgot a similar solution in as3 PinmemberMember 314736531-Aug-09 10:14 
GeneralRe: got a similar solution in as3 PinmemberReza Shojaee20-Sep-09 22:07 
Generalwell..few comments PingroupMd. Marufuzzaman25-Aug-09 4:06 
JokeRe: well..few comments Pinmembersotona25-Aug-09 4:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 25 Aug 2009
Article Copyright 2009 by Reza Shojaee
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid