Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Global Positioning Device Software

0.00/5 (No votes)
9 Dec 2012 1  
A C# based application to trace the location of a GPS device.

Introduction

We needed to develop an application which could plot the position of a device placed in a vehicle or a container etc., for a given interval of time and date, over map in a C# .NET based software.

Background

We are doing Industrial Electronics Engineering form IIEE, Karachi, Pakistan. The idea of this project came when we visited an electronic project exhibition where we saw a project displaying the latitude and longitude on an LCD using a GPS module. This inspired us to build a portable/wireless device which can log its GPS coordinates using a cell phone attached to it. We started working on that but as our exams came near our respected Sir Sajid Hussain gave us a project to develop a software using C#. This is where our project really got its shape. We decided to merge our C# project with the GPS project to make a software which can detect and plot the position of our GPS device. Sir Sajid Hussain gave us the idea of including a GSM module in it for updating the position of our device on a web server from where our software gets the data and plot it on the map. The software is customizable for multiple devices.  

The Code

The code of this project can be divided into three parts.

  1. GMAP.NET.
  2. HTTP Web Request (System.NET).
  3. XCoolForms for Windows (Additional Feature).

GMAP.NET 

The code uses GMap.NET.dll and GMap.NET.WindowsForms.dll to add controls to the XCoolForms, and it is quite easy and simple to use. First of all, GMapControls need to be initialized using:

gMapControl1.SetCurrentPositionByKeywords("Pakistan"); // The country to be focused after map is loaded
gMapControl1.gMapProvider = GMapProviders.GoogleMap;// The map provider
gMapControl1.MapScaleInfoEnabled = true;
gMapControl1.ForceDoubleBuffer = true;
gMapControl1.RoutesEnabled = true;// Map can have Routes
gMapControl1.MinZoom = 2;     // Minimum zoom level
gMapControl1.MaxZoom = 26;    // Maximum zoom level
gMapControl1.Zoom = 10;       // Initial zoom level
gMapControl1.DragButton = MouseButtons.Left;// Mouse button used for dragging the map

Placing a Marker on the Map

GMapOverlay myOverlay1 = new GMapOverlay(gMapControl1,"myOverlay1"); //Constructing object for Overlay
myOverlay1.Markers.Add(new gMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(
   new PointLatLng(latitude,longitude)));//Adding a new Marker in the Overlay 
gMapControl1.Overlays.Add(myOverlay1);// Adding myOverlay1 to the Control

Fetching the Data from the Server

The following request will return data from the server stored by a GPS device and save it as a string.

WebRequest request = WebRequest.Create("http://www.Abc......");// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;// Get the response.
WebResponse response = request.GetResponse();// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);// Read the content. 
string responseFromServer = reader.ReadLine();// Display the content. 

Plotting Route on the Map

GMapOverlay routes = new GMapOverlay(gMapControl1, "routes");// Constructing object for Overlay
gMapControl1.Overlays.Add(routes);
List<PointLatLng> list = new List<PointLatLng>(); // The list of Coordinates to be plotted
list.Add(new PointLatLng(32.710525233333,51.709773683333));
list.Add(new PointLatLng(32.711725983333,51.704725066667)); 
.
.
.
.
list.Add(new PointLatLng(32.713785566667,51.66982365));
GMapRoute r = new GMapRoute(list, "myroute"); // object for routing
r.Stroke.Width = 5;
r.Stroke.Color = Color.Red;
routes.Routes.Add(r);
gMapControl1.ZoomAndCenterRoute(r);
gMapControl1.Zoom = 15;

ToolTip 

To display related information of pointers, we need to add tooltip functionality to it. It can be added using these code segments:

GMapOverlay myOverlay1 = new GMapOverlay(gMapControl1, "myOverlay1");   
GMapMarkerGoogleGreen CurrentMarker;
CurrentMarker = new GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(new PointLatLng(latitude, longitude));
CurrentMarker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
CurrentMarker.ToolTipText = "Your Text Here!";
myOverlay1.Markers.Add(CurrentMarker);
gMapControl1.Overlays.Add(myOverlay1);

Additional features

XCoolForms

Although it’s not necessary, we wanted to add a little color to our dull looking form, so we decided to add XCoolForms for the windows in our project, which we got from http://www.codeproject.com/Articles/33716/Fancy-Windows-Forms. This is not a major part of our project and we just wanted to add some colors, the code and explanation for this feature can be found in the given link.

Custom Markers

As an additional feature we also added custom markers in our map like red dot, blue dot, and arrow. For this purpose we obtained a custom marker class from http://www.codeproject.com/Articles/32643/GMap-NET-Great-Maps-for-Windows-Forms-and-Presenta.

Point of Interest 

The device we made will work anywhere as long as the signal strength from the GSM vendor remains. 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here