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.
- GMAP.NET.
- HTTP Web Request (System.NET).
- 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"); gMapControl1.gMapProvider = GMapProviders.GoogleMap;gMapControl1.MapScaleInfoEnabled = true;
gMapControl1.ForceDoubleBuffer = true;
gMapControl1.RoutesEnabled = true;gMapControl1.MinZoom = 2; gMapControl1.MaxZoom = 26; gMapControl1.Zoom = 10; gMapControl1.DragButton = MouseButtons.Left;
Placing a Marker on the Map
GMapOverlay myOverlay1 = new GMapOverlay(gMapControl1,"myOverlay1"); myOverlay1.Markers.Add(new gMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(
new PointLatLng(latitude,longitude)));gMapControl1.Overlays.Add(myOverlay1);
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......");request.Credentials = CredentialCache.DefaultCredentials;WebResponse response = request.GetResponse();Console.WriteLine(((HttpWebResponse)response).StatusDescription);Stream dataStream = response.GetResponseStream();StreamReader reader = new StreamReader(dataStream);string responseFromServer = reader.ReadLine();
Plotting Route on the Map
GMapOverlay routes = new GMapOverlay(gMapControl1, "routes");gMapControl1.Overlays.Add(routes);
List<PointLatLng> list = new List<PointLatLng>(); 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"); 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.