Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Adding a world map to windows form and connect locations with lines
 

Adding a world map to windows form and connect locations with lines

Hi,

I am trying tocreate a windows form with a world map as its background and connect different locations on the map.
I have informations on the latitudes and longtitudes of the locations.
Should I have a map with latitudes and longtitudes ? If not, how would I join the locations?
And if yes, how can I draw lines on the map linking different locations?

Thanks.

Regards,
Jeremy
JeremyHel  Thursday, September 03, 2009 9:20 AM

Hi JeremyHel,

Based on my understanding, we can follow these steps to draw a line on the map:

1. Convert latitude & longitude coordinates to pixel coordinates.

This is an article discussing about map developing:
http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!488.entry.

It includes a method which converts from latitude & longitude coordinates to pixel coordinates, this is the code snippet:

    // Converts a latlitude longitude coordinate to a pixel coordinate of a map based on the map view
    private Point LatLongToPixel(Location latlong, BestView view)
    {    //Formulas based on following article:
        //http://msdn.microsoft.com/en-us/library/bb259689.aspx

        //calcuate pixel coordinates of center point of map
        double sinLatitudeCenter = Math.Sin(view.center.Latitude * Math.PI / 180); double pixelXCenter = ((view.center.Longitude + 180) / 360) * 256 * Math.Pow(2, view.zoom); double pixelYCenter = (0.5 - Math.Log((1 + sinLatitudeCenter) / (1 - sinLatitudeCenter)) / (4 * Math.PI)) * 256 * Math.Pow(2, view.zoom);        //calculate pixel coordinate of location
        double sinLatitude = Math.Sin(latlong.Latitude * Math.PI / 180); double pixelX = ((latlong.Longitude + 180) / 360) * 256 * Math.Pow(2, view.zoom); double pixelY = (0.5 - Math.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI)) * 256 * Math.Pow(2, view.zoom);    //calculate top left corner pixel coordiates of map image
        double topLeftPixelX = pixelXCenter - (mapWidth / 2); double topLeftPixelY = pixelYCenter - (mapHeight / 2);    //calculate relative pixel cooridnates of location
        double x = pixelX - topLeftPixelX; double y = pixelY - topLeftPixelY; return new Point((int)Math.Floor(x), (int)Math.Floor(y));
    }

2. Draw the line.

When we get the two points, we can draw the line in the OnPaint method of the Form. This is the code snippet:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawLine(Pens.Red, startPoint, endPoint);
    }


Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 07, 2009 3:40 AM
Jeremy:

You might want to have this topic moved to another forum. This one is for issues with Windows Forms Designer, which is not the same as "help me design a form" forum. :)

Windows Forms Designer is the design-time environment of Visual Studio, and this forum treats issues related to extending an customizing this environment and experience for custom Windows Forms controls.

Good luck.
MCP
webJose  Friday, September 04, 2009 2:29 AM

Hi JeremyHel,

Based on my understanding, we can follow these steps to draw a line on the map:

1. Convert latitude & longitude coordinates to pixel coordinates.

This is an article discussing about map developing:
http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!488.entry.

It includes a method which converts from latitude & longitude coordinates to pixel coordinates, this is the code snippet:

    // Converts a latlitude longitude coordinate to a pixel coordinate of a map based on the map view
    private Point LatLongToPixel(Location latlong, BestView view)
    {    //Formulas based on following article:
        //http://msdn.microsoft.com/en-us/library/bb259689.aspx

        //calcuate pixel coordinates of center point of map
        double sinLatitudeCenter = Math.Sin(view.center.Latitude * Math.PI / 180); double pixelXCenter = ((view.center.Longitude + 180) / 360) * 256 * Math.Pow(2, view.zoom); double pixelYCenter = (0.5 - Math.Log((1 + sinLatitudeCenter) / (1 - sinLatitudeCenter)) / (4 * Math.PI)) * 256 * Math.Pow(2, view.zoom);        //calculate pixel coordinate of location
        double sinLatitude = Math.Sin(latlong.Latitude * Math.PI / 180); double pixelX = ((latlong.Longitude + 180) / 360) * 256 * Math.Pow(2, view.zoom); double pixelY = (0.5 - Math.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI)) * 256 * Math.Pow(2, view.zoom);    //calculate top left corner pixel coordiates of map image
        double topLeftPixelX = pixelXCenter - (mapWidth / 2); double topLeftPixelY = pixelYCenter - (mapHeight / 2);    //calculate relative pixel cooridnates of location
        double x = pixelX - topLeftPixelX; double y = pixelY - topLeftPixelY; return new Point((int)Math.Floor(x), (int)Math.Floor(y));
    }

2. Draw the line.

When we get the two points, we can draw the line in the OnPaint method of the Form. This is the code snippet:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawLine(Pens.Red, startPoint, endPoint);
    }


Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 07, 2009 3:40 AM
Hi Aland,

Thanks for replying. Your answer definitely helps alot !!

Jeremy.
JeremyHel  Friday, September 11, 2009 8:38 AM

You can use google to search for other answers

Custom Search

More Threads

• Authenticating users in windows application...?
• How to make a Picturebox accept focus
• Open my local help.html file when selecting the component and press F1?
• How to generate "this.components = new System.ComponentModel.Container();" for component ?
• DataGrid and collections
• How to make an application containg WinForms designer ?
• Specifing the Editor a PropertyGrid should use on a Custom Control Property
• how to create a push pin as in VS toolbox
• Custom TableLayoutPanel control...can't resize columns and rows
• Custom component selection problem