Wednesday, August 3, 2011 7:50 PM
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.
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
- Yahoo Maps
- Bing Maps
- 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.
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
- 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
For sake of clarity I am going to name the control myMap.
You first need to Initialize the control
.MapType = MapType.BingMap
.MinZoom = 3
.MaxZoom = 17
.Zoom = 4
.Manager.Mode = AccessMode.ServerAndCache
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)))
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.
That’s it! My next posts will be on
- Removing/Hiding overlays
- Changing the Markers
- Adding labels
Here is a screenshot of how I am using it in one of my apps (work-in-progress)