Geeks With Blogs
Saif Khan { I am Saif } "Knowledge is power, information is free, share it!"

 
Recently I was working on a project where I needed to implement mapping functionality - I am not new to mapping technology, as a matter fact, a few years back I created my own Winform mapping control which interfaced with yahoo and ArcGIS maps which works great in some of my apps, however, I was looking for something more and didn’t want to re-invent the wheel – so to speak. I stumbled upon GMap.NET and as one user put it, “Absolutely fantastic piece of work”. The only thing lacking is proper documentation.


Overview
GMap.NET is a FREE .NET control which enables mapping functionality to your application. It can be used in your Winform, WPF and Mobile application and supports several maps including

  • OpenStreetMap
  • Yahoo Maps
  • Bing Maps
  • ArcGIS
  • Google – sort of, support has been discontinued. Long story short, Google complained that the developer was in violation of their licenses blah blah.

Here are a few things you need to understand when using the the control

1. What is the map control (GMapControl)? This is the control which renders the map.

2. What is an Overlay (GMapOverlay)? This is a layer on top of the map control. You can have several layers on top of a map, each layer representing, say, a route with stops, a list of stores etc.

3. What are Markers (GMapMarker)? These are the points on a layer, each representing a specific geo location (Lat,Lon) e.g. each drop point on a route.

4. What is a route (GMapRoute)? This is the path or direction between two or more points.

5. WTF are tiles? – well here is something to read…Bing Maps Tile System.

 

map_Layers

The good stuff

When you download the binary from codeplex (this post is on WinForms) you will find several files in the folder. You only need the following

  • GMap.NET.Core.dll
  • GMap.NET.WindowsForms.dll
  • x86 and x64 folders for the version at the time of writing this post. These folders include the System.Data.SQLite.DLL which is used for local caching of the data to reduce the request for map tiles.

Now, add the GMap.NET.WindowsForms.dll to your Visual Studio IDE toolbox. Create a new project VB.NET or C#, whatever your fancy and drag the GMapControl onto the form.

This is how my toolbox look 

8-4-2011 2-48-46 AM

 

 


For sake of clarity I am going to name the control myMap.

You first need to Initialize the control

VB.NET

With myMap
      .SetCurrentPositionByKeywords("USA")
      .MapType = MapType.BingMap
      .MinZoom = 3
      .MaxZoom = 17
      .Zoom = 4
      .Manager.Mode = AccessMode.ServerAndCache
End With

 

Note: You can set the default position of the map with (to list a few)

  • SetCurrentPositionByKeywords(“country”) - USA
  • SetCurrentPositionByKeywords(“state, country”) – Berlin, Germany
  • SetCurrentPositionByKeywords(“province, country”) – Alberta, Canada
  • Position = New PointLatLng(latitude,longitude) - New PointLatLng(54.6961334816182, 25.2985095977783)

Next you need to add overlays with markers to the map

Dim overlayOne As New GMapOverlay(myMap, "OverlayOne")

Note: The GmapOverlay takes the map control and an id as parameters.


Next you need to add markers to the overlay.

overlayOne.Markers.Add(New GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(New PointLatLng(LatitudeValue, LongitudeValue)))


basically you are creating a marker (GMapMarkerGoogleGreen) which takes an latitude and longitude as parameters. Then I am adding that marker to the overlay (overlayOne).


In most cases you’ll need to add multiple markers to the overlay. You can do that with a loop e.g.

For Each customer In Customers
        overlayOne.Markers.Add(New GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(New PointLatLng(customer.Lat, customer.Long)))
Next


in the above code, I am iterating a list of customers, creating a new marker and adding it to the overlay.


Note: It is recommended that you have your points (address) geocoded first to get better performance. it makes no sense having several hundred addresses geocoded on the fly each time you load your map if you are dealing with a large list. Some service like google, have rate limits as to how many geocode query you can send to their servers in a given day.

Finally, add the overlay to the map.

myMap.Overlays.Add(overlayOne)

That’s it! My next posts will be on

  • Removing/Hiding overlays
  • Changing the Markers
  • Adding labels
  • Caching
  • Routing

Here is a screenshot of how I am using it in one of my apps (work-in-progress)

8-3-2011 1-20-33 PM

8-2-2011 3-44-12 PM

Posted on Wednesday, August 3, 2011 7:50 PM | Back to top


Comments on this post: Using GMap.NET – Great Maps for Windows Forms & Presentation

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Dear Saif,

Your tutorial is very good although I work in C#. I am having trouble understanding of the gmap.net Control. although I can see a map in my form but I cannot position it to india. Also because of lack of proper documentation it is not easy.

Can you point out to some good examples or tutorials

thanx

saurabh
Left by saurabh gupta on Sep 14, 2011 10:02 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Dear,

I got the zoom part finally. Can you tell me where to get the source code for the gmap.net as i tried downloading their examples none of them are working in my vs2010. Also i tried to download through tortise but was unable to do so as it does not download and shows me error. Can you guide me towards a working source code.
regds

saurabh
Left by saurabh on Sep 27, 2011 7:13 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
http://greatmaps.codeplex.com/SourceControl/list/changesets#

p.s. all demos are working perfectly, thx Saif for examples

Left by radioman on Oct 17, 2011 12:32 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Can share your icons?
Left by Suker Yang on Oct 18, 2011 6:02 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
can you share Windows form code with markers kindly share
Left by Hussain Munaf on Dec 10, 2011 8:34 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Hi, thanks for the control, it is really fantastic,

but I cannot find the property myMap.MapType = ....

!!!
Thanks
Left by logicchild on Jan 15, 2012 11:31 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Where is MapType ??
Left by Carlos A. on Feb 18, 2012 6:52 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
dear Saif,
I'm currently working desktop app using google map. Your tutorial's very helpful but i can't drag it. Can you help me and share more examples pls pls...
Left by ENKHEE on Feb 27, 2012 2:42 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
hi.
same logicchild, .maptype not found...

T.Y.
Left by Giulio on Feb 29, 2012 2:11 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Anyone know if this control has any built-in methods for determining if a PointLatLng falls within a GMapPolygon?
Left by Steve on Mar 07, 2012 9:23 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
@Steve - You'll need to check for points within the polygon

/// <summary>
/// Determines if a Point is inside a polygon.
/// </summary>
/// <returns>Return True if the point is inside Polygon.</returns>
public static bool IsInsidePolygon(PointF[] polygon, PointF point)
{
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddPolygon(polygon);
return gp.IsVisible(point.X, point.Y);
}
}

Left by Saif Khan on Mar 07, 2012 9:54 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Wow Man!

This is great! Very good your tutorial.
Left by Pedro Pablo on Apr 25, 2012 4:04 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Hi

How to work with asp.net, i want to do website but i can't do.
Help me please.
Left by Edisson on May 09, 2012 6:12 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Hello, My Name is Santiago! I m From Argentine! Excuise Me...Do you have de Code? I don under stand the inicialization....un WinForm!

Thank!
Left by Santiago on May 15, 2012 8:47 AM

# Drawing custom routs
Requesting Gravatar...
hello every body. please exuse me becose i cnow that my englich is so bed :(

hear is my problem: i want to create routs from a list of LatLngPoints.

I do it and i can calculate distance but routes are not visible on my map.

this is my C# code:
#region il y a des routes
IEnumerable<XElement> RoutesRq = from xElement in xDoc.Root.Descendants("Route") select xElement;
bool HasRouts = false;
if (RoutesRq.Count() > 0)
{
HasRouts = true;
int i = 1;
foreach (XElement route in RoutesRq)
{
List<PointLatLng> points = new List<PointLatLng>();

foreach (XElement p in route.Elements())
{
var latitude = Convert.ToDouble(p.Value.Split(',')[0].Replace('.', ','));
var longitude = Convert.ToDouble(p.Value.Split(',')[1].Replace('.', ','));
points.Add(new PointLatLng(latitude, longitude));
}

GMapRoute Grout = new GMapRoute(points, "R_" + i);
routes.Routes.Add(Grout);
i++;
}
gMapControl.Overlays.Add(routes);
//gMapControl is my GMap.NET.WindowsForms.GMapControl object
}
#endregion il y a des routes
help me plees. thanks a lot.
Left by speloman on Jun 04, 2012 8:39 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
.mapType property doesn't exist, you must have to use :
this.gMap.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
for exempla
Left by May be it can help on Jun 05, 2012 1:51 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
There is no .maptype in the latest releases. How do you set the map provider? I have tried everything I could find.
Left by KK on Jun 25, 2012 6:26 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
I can't Add the GMapControl to the toolbox ??
Left by Muhammad on Jul 14, 2012 11:17 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
I just started looking at this two days ago so my info is AFAIK based but after a bit of experimenting I got the following to work.
I am using Visual Basic.
Replace XLAT and XLON with whatever coordinates you want
After adding references to the project try...

Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Imports GMap.NET.WindowsForms.ToolTips
Imports GMap.NET
Imports GMap.NET.MapProviders
Imports System.Web.Extensions
Imports MySql.Data

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With myMap
.SetCurrentPositionByKeywords("USA")
.MapProvider = GMapProviders.GoogleSatelliteMap
.MaxZoom = 21
.Zoom = 10
.Manager.Mode = AccessMode.ServerAndCache
End With

myMap.Position = New GMap.NET.PointLatLng(XLAT, XLON)

Dim overlayOne As New GMap.NET.WindowsForms.GMapOverlay(myMap, "OverlayOne")
overlayOne.Markers.Add(New GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(New GMap.NET.PointLatLng(XLAT, XLON)))

myMap.Overlays.Add(overlayOne)

End Sub
End Class
Left by Jerry on Jul 19, 2012 8:57 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
It's very helpful!
But I'd like to see the next post of "Adding labels" and change marker, I'm not find the next post by search.
Would you please post it? Or send some clue to me , thanks
Left by Teng on Aug 12, 2012 8:53 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Anyone knows how to show a custom info window (with tabs or something like you can do on a web application) when clicking a marker using this control ??
Left by will on Sep 03, 2012 10:08 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
hi
thanks for ur tutorial but i have a problem with the maptype
Left by ahmed on Nov 14, 2012 1:43 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
i am very new in mapping pls explain further.
Thanks
Left by ahmed on Nov 14, 2012 10:24 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
hello,

How to add labels to markers?

Best Regards,
Marco Gaspar
Left by Marco Gaspar on Nov 23, 2012 7:52 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Great !
Can you share your code, thank you very much !
Left by Tuyen on Nov 23, 2012 4:24 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
hi!!!! That is so great!!!!!
hey
how declare "position"?
i try to do something like that but , some line i can't understand Y -Y
I am student from university
my email :lu_ponce88@hotmail.com

Left by lu on Dec 08, 2012 7:59 AM

# How to add labels to markers?
Requesting Gravatar...
'Define Marker like explained above:

Dim overlayOne As New NET.WindowsForms.GMapOverlay(GMapControl1, "OverlayOne")

overlayOne.Markers.Add(New GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(New GMap.NET.PointLatLng(51.2961334816182, 7.0985095977783)))

'You can use overlayOne.Markers.Count to determin loops
overlayOne.Markers(0).Tag = "Point=1;ID=47"
overlayOne.Markers(0).ToolTipText = "Point 1"

GMapControl1.Overlays.Add(overlayOne)

'ToolTipText is shown when mouse is over Marker

'To handle click-event of markers use GMapControl_OnMarkerClick

Private Sub GMapControl1_OnMarkerClick(item As GMap.NET.WindowsForms.GMapMarker, e As System.Windows.Forms.MouseEventArgs) Handles GMapControl1.OnMarkerClick

MsgBox("Marker clicked: " & item.Tag.ToString, MsgBoxStyle.Information, "Marker " & item.ToolTip.ToString)

End Sub

Greets Lotto
Left by Lotto on Dec 09, 2012 11:36 PM

# lu: how declare "position"?
Requesting Gravatar...
Hi lu,

Me.GMapControl1.Position returns / sets the center of the map.

You can declare a Point like that:

Dim pnt as New GMap.NET.PointLatLng(Y, X)

You can set the center of the map to a point:

Me.GMapControl1.Position = pnt

You can store the actual position of the map into a point:

pnt = Me.GMapControl1.Position

You can move around a map by moving the position. The following code moves the position of the map to the right (east).

const cMoveStep as double = 0.05

Me.GMapControl1.Position = New GMap.NET.PointLatLng(Me.GMapControl1.Position.Lat, Me.GMapControl1.Position.Lng + cMoveStep)

Me.GMapControl1.ReloadMap()

i hope it helps
greets Lotto
Left by Lotto on Dec 10, 2012 4:00 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
how can i get the marker centered on the map ?

thanks in advance
Left by Mostafa Atef on Jan 12, 2013 4:36 AM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Hi anyone know how to return the Lat and Long value when user clicking on the map? Preferably using C#.
Left by Denny on Jan 15, 2013 1:47 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Anyone know how to get Long and Lat value when user clicking on the map? Using C# or VB is ok.
Left by Denny on Jan 15, 2013 1:53 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
i can drag map and also can add markers in it .... but here is my problem
..........................................................................................................
i have added more than 10 markers
&
also added their click event .....
but i am failed to judge that which marker (region) is clicked from 10 markers .....
i think it is possible by getting the latitude and langitude or by adding unique label in every label.....please help how to get langitude and latitude of a marker
or
add unique label in a marker and how to read this marker

i have tried it very much but till now no success
my mail amir@cedsg.com
Left by Amir on Mar 20, 2013 8:27 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
Hello Saif, I need to use the control to display a map using a shape file and integarte with my desktop application to custom visualize spatially data from sql database.You know i want with show all mapping elements in my print lik Title, Legend and the different symbologies.Can you tell a good guide to accomplish this.kindly share code fragment.Thank you.
Left by Jonas on Mar 29, 2013 11:37 PM

# re: Using GMap.NET – Great Maps for Windows Forms & Presentation
Requesting Gravatar...
To extract Lat/Long from a map double-click:

// In MainMap setup...
MainMap.MouseDoubleClick += new MouseEventHandler(MainMap_MouseDoubleClick);

...

// When the user double-clicks, get the location, move
// TargetMarker to that location and save GISLocation.
private void MainMap_MouseDoubleClick(object sender, MouseEventArgs e)
{
// Set the long/lat value based on the click location and replace the marker.
TargetMarker.Position = MainMap.FromLocalToLatLng(e.X, e.Y);
GISLocation = TargetMarker.Position;

// ???? Do more here.
}

Paul T.
Left by Paul T. on Jun 07, 2013 5:16 PM

Your comment:
 (will show your gravatar)
 


Copyright © saifkhan | Powered by: GeeksWithBlogs.net | Join free