A simple example of Gmap.net with c#

on Saturday, January 21, 2012
This tutorial is specially made for a member in my team Explorer who has the task to locate the position of our robot in a  map.
gmap.net is a great .net framework nonetheless it lacks of documentation.
So what I'm going to do is to show you how to refer gmap.net in your application  and how to locate something in the world map.
So let's get started!

First of all we need to download the binary package which contains all the *.dll that we need.
http://greatmaps.codeplex.com/releases/view/20235#DownloadId=67818
extract the folder in a decent place, say for exemple c:\\projects\
we will need only 2 files:
GMap.NET.Core.dll
GMap.NET.WindowsForms.dll
Now we will add the GMap.NET.WindowsForms.dll to our Visual Studio IDE toolbox.
to do so go to tools-->choose toolbox items

before clicking brows, make sure you selected the ".NET Framework Components"
Select the Gmap.NET.WindowsForms.dll
click Open or "ouvrir" the click ok
now go to your toolbox (view-->toolbox) you should find a new control the "GmapControl".


Next step  is even more simple :))
we need to add a reference to our new library in our projects so go to:
(view-->Solution explorer)
Select the GMap.NET.Core.dll click Ok
Note that the reference are being added to your projects reference,
we can now go back to the design and set our gmap control


Right click on the control and choose "propoerties"
In the properties windows set the Name to "mapexplr"

Finally we can start coding

Add these lines to the using lines
using GMap.NET.WindowsForms;
using GMap.NET;
using GMap.NET.MapProviders;



The Code
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        GMapOverlay overlayOne;
        String contry;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void mapexplr_Load(object sender, EventArgs e)
        {
            //initialisation de notre map
            mapexplr.SetCurrentPositionByKeywords("Tunisia");
            mapexplr.MapProvider = GMapProviders.BingMap;
            mapexplr.MinZoom = 3;
            mapexplr.MaxZoom = 17;
            mapexplr.Zoom = 5;
            mapexplr.Manager.Mode = AccessMode.ServerAndCache;
            //ajout des overlay
            overlayOne = new GMapOverlay(mapexplr, "OverlayOne");
            //ajout de Markers
            overlayOne.Markers.Add(new                                         GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(new PointLatLng(36.657403,10.327148)));
            //ajout de overlay à la map
            mapexplr.Overlays.Add(overlayOne);
        }
    }
}
The final result:



Love you tunisia !
I hope this tutorial was helpful, till the next time don't stop sharing

0 comments:

Post a Comment