0% found this document useful (0 votes)
41 views27 pages

How To Use Geographic Maps in Visual Studio Applications

The document explains how to insert geographic maps into Visual Studio .NET applications for free using the GMap.NET component. It details the steps to download and install GMap.NET, add references in a Visual Studio project, and use the component to load different types of maps and GPX routes. It shows example code to change the map type, load a GPX route, and develop a functional application that uses GMap.NET.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views27 pages

How To Use Geographic Maps in Visual Studio Applications

The document explains how to insert geographic maps into Visual Studio .NET applications for free using the GMap.NET component. It details the steps to download and install GMap.NET, add references in a Visual Studio project, and use the component to load different types of maps and GPX routes. It shows example code to change the map type, load a GPX route, and develop a functional application that uses GMap.NET.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

How to use geographic maps in Visual Studio .

NET applications for free

Published byalonsojpdNovember 4, 2015CSharp, Programming|

We show in this tutorial how to insert a geographical map into our Visual Studio .NET application.

We indicate which free component to use (GMap.NET), how to install it in Visual Studio .NET Community

2015 and how to add this visual component to the form of our application. We also explain a

Example of using GMap.NET to change the type of map (Google Satellite, Google Hybrid, Open Street)

Map, Open Cycle Map, etc.) and how to upload a polyline (route or GPS track).

Video tutorial Insert geographic map in Visual Studio .NET C# for free.

Requirements to use geographical maps in Visual Studio .NET applications.

Download and installation of GMap.NET in Microsoft Visual Studio .NET Community 2015.

How to use GMap.NET in Visual Studio .NET C# CSharp application.

Example code to load GPX file in GMap.NET.


Change the map type in GMap.NET.

Example application using GMap.NET with Visual Studio .NET C# in operation.


Video tutorial Insert geographic map in Visual Studio .NET C# for free

Video tutorial on how to incorporate a geographic map into our applications developed in Visual Studio

.NET C# C Sharp. We show in this video tutorial how to add maps to our applications in a way

free using the GMap.NET component. We explain how to load a map and how to install and reference it.

component in Visual Studio .NET Community:

Insert geographic map Visual Studio NET C# free:


Requirements to use geographical maps in Visual Studio .NET applications

In order to use geographic maps (Google Maps, satellite view, hybrid, Open Street Map, Open Cycle Map,

etc.) in our .NET applications (both C# and Visual Basic .NET) we will logically need to have,

from Visual Studio .NET, in the following link we show how to download and install this development IDE:

Install Microsoft Visual Studio .NET Community 2015 first C# application.

We will also need a device with an Internet connection to download the free component GMap.NET.
Download and install GMap.NET in Microsoft Visual Studio .NET Community
2015
We will access the official website of the component, currently at the link:

The provided text is a URL and does not contain any translatable text.

And we will download the file GMap.NET.WindowsForms.zip:


Once downloaded, we will unzip it:
The compressed file contains the files:

Demo.Windows.Forms.exe: example application using GMap.NET.

GMap.NET.Core.dll: the component itself.

GMap.NET.Core.xml: configuration file of the previous library.

GMap.NET.WindowsForms.dll: the visual component to add to the form.

GMap.NET.WindowsForms.xml: configuration file of the visual component library.

We will now open Visual Studio .NET Community 2015 and create a new project, for this we will access

to the menu 'File' - 'New' - 'Project':


We will choose the type of project, in our case on the left we will select 'Templates' - 'Visual C#' -

"Windows" and on the right we will select "Windows Forms Application". We will enter a name for

the project, for example "AjpdSoftMapaCSharp" and we will press "Accept":


The assistant to create a new Visual Studio .NET project will have created our C# application with a

initial form
After the creation of the project, what we will do now is add the GMap.NET libraries to our project.

the necessary references. To do this, we will access the folder where we have used Windows Explorer.

Unzip the GMap.NET file and we will copy all the dll libraries from GMap.NET and the files.

Configuration XML:
We will take the copied files in the folder of our project, specifically in the folder bin/debug.

our case in:

C:/My documents/Visual Studio 2015/Projects/AjpdSoftMapCSharp/bin/Debug


Basically, what our application will require is to have the libraries in the same folder as the executable.

DLL deGMap.

After placing the GMap.NET libraries in the same folder as the application executable, the following

The next step will be to add the reference to GMap.NET in our Visual Studio .NET project. To do this, from Visual

Studio, with our open project, we will click on the 'Project' menu - 'Add reference':
In the References Manager window, we will click on 'Browse':
We will navigate to the folder of our project, to the folder…/bin/Debug, we will select the

We will click 'Add' for library GMap.NET.Core.dll


We will repeat the process to select the library GMap.NET.WindowsForms.dll:
After selecting the two deGMap.NET libraries we will click on "OK":
Finally, to have the visual component of GMap.NET, we must add it to the window of Box

of tools. To do this, we will right-click on the Toolbox window (if not

it appears we can show it from the menu "View"–"Toolbox"), in the pop-up menu

we will select 'Choose items':


We will wait for the loading process to finish.
We will click on the '.NET Framework Components' tab and click on 'Browse':
We will access the folder ../bin/Debug of our project and select the

library GMap.NET.WindowsForms.dll which contains the visual control GMapControl and we will press 'Open':
It will show us a security warning, we will press 'Yes':

With the text:

—————————

Microsoft Visual Studio

—————————
Security warning: probably 'C:/Users/AjpdSoft/Documents/Visual Studio

2015/Projects/AjpdSoftMapaCSharp/AjpdSoftMapaCSharp/bin/Debug/GMap.NET.WindowsForms.dll' is

downloaded from a network location and could harm the equipment. Only load assemblies from publishers in the

that he trusts.

Do you want to load it anyway?

—————————

Yes

—————————

After adding 'GMapControl' to the .NET Framework components, we will click 'OK':

Once added, in the Toolbox, in the 'Common Controls' group we will have

availableGMapControl to add it to our form:


How to use GMap.NET in Visual Studio .NET C# CSharp application

Once installed and the reference to GMap.NET added, we can use the visual component, for this, from

"Toolbox", in "Common Controls" we will press on GMapControl and we will click on the

application form for you to add the component. Once added, we will stretch it until it is left at

size we want, the map will be displayed in this component:


Before continuing, in the Using of our form, we will reference the GMap.NET namespace for

to be able to use it, therefore, from the source code of our form, we will add the following lines:

using GMap.NET.MapProviders;

using GMap.NET;
We will add a button so that when pressed, the map loads at a specific latitude and longitude.

Hello, in the eventClick of the button we will add the following code (taking into account that to

componentGMapControl we have called "map" in its "Name" property:


C#

1 private void button1_Click(object sender, EventArgs e)


2 {
3 MouseButtons.Left
4 map.CanDragMap = true;
5 map.MapProvider = GMapProviders.GoogleMap;
6 new PointLatLng(37.983, -1.133);
7 mapa.MinZoom = 0;
8 mapa.MaxZoom = 24;
9 mapa.Zoom = 9;
10 map.AutoScroll = true;
11
The above code will load the Google Maps map, we can load other maps such as:

Google Maps Satellite.

Google Maps Street Map.

Google Maps Hybrid.

OpenStreetMap.

OpenClycleMap.

Code example to load GPX file in GMap.NET

GPX is a GPS data exchange format, it is an XML scheme intended for transferring GPS data between

applications. It can be used to describe points (waypoints), routes (tracks), and paths (routes). A

Below we show a source code in C# that will allow us to equip our applications in Visual

Studio .NET C Sharp option to load a GPX file and display the route taken on the map. For the
Next, we will need a component of type System.Windows.Forms.OpenFileDialog.

we will add to our form from the Toolbox, we will call this component 'dlg':

We will logically add a button so that when the user presses it, the selection window is displayed.

GPX document and once selected, load it on the map. Therefore, the code for the 'Click' event of

Button will be:


C#

1 private void btLoadGPX_Click(object sender, EventArgs e)


2 {
3 dlg.CheckPathExists = true;
4 dlg.CheckFileExists = false;
5 dlg.AddExtension = true;
6 gpx
7 dlg.ValidateNames = true;
8 Load GPX File
9 GPX Files (*.gpx)|*.gpx
10 dlg.FilterIndex = 1;
11 dlg.RestoreDirectory = true;
12
13 if(dlg.ShowDialog() == DialogResult.OK)
14 {
15 try
16 {
17 string gpx = File.ReadAllText(dlg.FileName);
18
19 gpxType r = mapa.Manager.DeserializeGPX(gpx);
20 if(r != null)
21 {
22 if(r.trk.Length > 0)
23 {
24 foreach(var trk in r.trk)
25 {
26 List points = new List();
27
28 foreach(var seg in trk.trkseg)
29 {
30 foreach(var p in seg.trkpt)
31 {
32 points.Add(new PointLatLng((double) p.lat,
33 (double) p.lon));
34 }
35 }
36
37 GMapRoute rt = new GMapRoute(points, string.Empty);
38 {
39 new Pen(Color.FromArgb(144, Color.Red));
40 rt.Stroke.Width = 5;
41 rt.Stroke.DashStyle =
42 System.Drawing.Drawing2D.DashStyle.Solid;
43 }
44 routes.Routes.Add(rt);
45 routes.IsVisibile = true;
46
47 }
48 map.Overlays.Add(routes);
49 map.ZoomAndCenterRoutes(null);
50 }
51 }
52 }
53 catch(Exception ex)
54 {
55 Error importing GPX:
56 Error importing GPX
57 MessageBoxButtons.OK, MessageBoxIcon.Warning);
58 }
59 }
60 }
Change map type in GMap.NET
Another option we can implement in our .NET application with a map is to change the type of map.

(satellite, hybrid, street, etc.). To do this, we will add a dropdown to the form.

(System.Windows.Forms.ComboBox). We will call the dropdown 'lsMapas' and in its 'Items' property

we will add:

Google Maps Satellite

Google Maps Street Map

Google Maps Hybrid

OpenStreetMap

OpenClycleMap

So that the user can choose the type of map to display. We will add a button to show the

map and in the 'Click' event we will introduce the following code:
C#

1 private void btReloadMap_Click(object sender, EventArgs e)


2 {
3 if (lsMapas.Text == "Google Maps Satellite")
4 map.MapProvider = GMapProviders.GoogleSatelliteMap;
5 if (lsMaps.Text == "Google Street Maps")
6 map.MapProvider = GMapProviders.GoogleMap;
7 if (lsMapas.Text == "Google Hybrid Maps")
8 map.MapProvider = GMapProviders.GoogleHybridMap;
9 if (lsMapas.Text == "OpenStreetMap")
10 map.MapProvider = GMapProviders.OpenStreetMap;
11 if (lsMapas.Text == "OpenClycleMap")
12 mapa.MapProvider = GMapProviders.OpenCycleMap;
13
14 map.Refresh();
15 }
Example application using GMap.NET with Visual Studio .NET C# in
operation

Next, we show an application developed in C Sharp .NET in operation that uses the

GMap.NET component to display geographic maps and routes on the map:

Satellite view of the map:

You might also like