Saif Khan

{ I am Saif } "Knowledge is power, information is free, share it!"
posts - 26 , comments - 139 , trackbacks - 0

Using GMap.NET – Great Maps for Windows Forms & Presentation

 
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

Print | posted on Wednesday, August 3, 2011 7:50 PM |

Feedback

Gravatar

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

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
9/14/2011 10:02 PM | saurabh gupta
Gravatar

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

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
9/27/2011 7:13 PM | saurabh
Gravatar

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

http://greatmaps.codeplex.com/SourceControl/list/changesets#

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

10/17/2011 12:32 AM | radioman
Gravatar

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

Can share your icons?
10/18/2011 6:02 PM | Suker Yang
Gravatar

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

can you share Windows form code with markers kindly share
12/10/2011 8:34 AM | Hussain Munaf
Gravatar

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

Hi, thanks for the control, it is really fantastic,

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

!!!
Thanks
1/15/2012 11:31 PM | logicchild
Gravatar

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

Where is MapType ??
2/18/2012 6:52 AM | Carlos A.
Gravatar

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

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...
2/27/2012 2:42 AM | ENKHEE
Gravatar

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

hi.
same logicchild, .maptype not found...

T.Y.
2/29/2012 2:11 AM | Giulio
Gravatar

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

Anyone know if this control has any built-in methods for determining if a PointLatLng falls within a GMapPolygon?
3/7/2012 9:23 AM | Steve
Gravatar

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

@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);
}
}

3/7/2012 9:54 AM | Saif Khan
Gravatar

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

Wow Man!

This is great! Very good your tutorial.
4/25/2012 4:04 AM | Pedro Pablo
Gravatar

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

Hi

How to work with asp.net, i want to do website but i can't do.
Help me please.
5/9/2012 6:12 AM | Edisson
Gravatar

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

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!
5/15/2012 8:47 AM | Santiago
Gravatar

# Drawing custom routs

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.
6/4/2012 8:39 PM | speloman
Gravatar

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

.mapType property doesn't exist, you must have to use :
this.gMap.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
for exempla
6/5/2012 1:51 AM | May be it can help
Gravatar

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

There is no .maptype in the latest releases. How do you set the map provider? I have tried everything I could find.
6/25/2012 6:26 PM | KK
Gravatar

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

I can't Add the GMapControl to the toolbox ??
7/14/2012 11:17 PM | Muhammad
Gravatar

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

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
7/19/2012 8:57 PM | Jerry
Gravatar

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

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
8/12/2012 8:53 PM | Teng
Gravatar

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

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 ??
9/3/2012 10:08 AM | will
Gravatar

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

hi
thanks for ur tutorial but i have a problem with the maptype
11/14/2012 1:43 AM | ahmed
Gravatar

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

i am very new in mapping pls explain further.
Thanks
11/14/2012 10:24 PM | ahmed
Gravatar

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

hello,

How to add labels to markers?

Best Regards,
Marco Gaspar
11/23/2012 7:52 AM | Marco Gaspar
Gravatar

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

Great !
Can you share your code, thank you very much !
11/23/2012 4:24 PM | Tuyen
Gravatar

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

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

12/8/2012 7:59 AM | lu
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: