samedi 21 janvier 2012

A simple example of Gmap.net with c#

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

24 commentaires:

  1. thanks , but how can I change the image of the marker ?!

    RépondreSupprimer
  2. just from here,
    overlayOne.Markers.Add(new GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(new PointLatLng(36.657403,10.327148)));

    change GMapMarkerGoogleGreen with what you desire

    RépondreSupprimer
  3. why there is one warning when i run this program

    is says that contry never been used... please help me

    RépondreSupprimer
  4. Is there a way how to add a custom marker please?

    RépondreSupprimer
  5. Abu Muslim Al-Amiri: what's your contry?

    RépondreSupprimer
  6. May be by creating a similar class to GmapMarkerGoogleGreen or modifying its code. Sorry I didn't try that before :(

    RépondreSupprimer
  7. Hi, How can I change the type of map?

    RépondreSupprimer
    Réponses
    1. Change this line ,
      mapexplr.MapProvider = GMapProviders. ;
      choose the one you like

      Supprimer
  8. How can i display a map with specified boundaries. What i mean is i would like to display a map in between two points(corners)

    RépondreSupprimer
  9. Salam Mazen ,

    As a young student I need your help in somthing ! Please help me ..

    I want to implement Gmap in an application that will run on tablett win8

    is it possible to programme a fonction that will return the current user position and when he moves it always keep updated? like a True gps software? any idea

    Thank you

    RépondreSupprimer
  10. Hello,
    I'm really sorry, I don't have any experience with mobile developing.

    RépondreSupprimer
  11. Ce commentaire a été supprimé par l'auteur.

    RépondreSupprimer
  12. Mazen, this document is very useful for me. Also your presentation is simple, thank you for this.

    RépondreSupprimer
  13. how can add multiple markers from datagridview in c# i have 1000 lat longs is it possibel to add it in gmap.net

    RépondreSupprimer
  14. if (comboBox5.SelectedIndex == 0)//(REGION 1)
    {
    gMapControl1.MaxZoom = 11;
    gMapControl1.MinZoom = 1;
    gMapControl1.Zoom = 2;

    String Query = " SELECT top 10 Latitude,Longitude FROM [ICPS].[dbo].[Sheet3_kir] ";

    SqlCommand cmdDatabase = new SqlCommand(Query, conDatabase);
    //SqlDataReader myReader;
    gMapControl1.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
    GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;

    GMapOverlay markersOverlay = new GMapOverlay("VCS MAP");
    gMapControl1.Overlays.Add(markersOverlay);


    for (int i = 0; i <= dataGridView1.Rows.Count; i++)
    {
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    var Latitude = float.Parse(dataGridView1.Columns[1]);
    var Longitude = float.Parse(dataGridView1.Columns[2]);
    gMapControl1.Position = new PointLatLng(Latitude, Longitude);

    GMarkerGoogle m = new GMarkerGoogle(gMapControl1.Position, GMarkerGoogleType.green_pushpin);
    //markersOverlay.Markers.Add(m);
    }

    RépondreSupprimer
  15. Jazak allah keer brother. I love Tunisia.
    Salam from Oman

    RépondreSupprimer
  16. I run the program but all maps cannot be loaded. What can I do?

    RépondreSupprimer
    Réponses
    1. Are you connected to internet while you are running the map?

      Supprimer
  17. Hello I seem to be stuck with every tutorial i follow for GMap .NET.

    I am able to run the program but i dont see the map. I can drag the map but i see nothing.

    Screenshot: http://i.imgur.com/tDrMGcn.png

    This also happend when i tried this tutorial: http://www.websofia.com/2013/02/gmap-net-tutorial-maps-markers-and-polygons/

    Any help would be great. I tried different mapproviders.

    RépondreSupprimer