The IGps Interface |
After reading this tutorial you should be able to create a sample application that will follow the current position (and rotate to the current heading) of a given GPS device.
Note |
---|
The code in this tutorial will be used as the framework for the following tutorials:
The tutorials listed above can be completed in any order, however, each tutorial requires the code from this tutorial as its basis. |
IGps is an interface representing a generic GPS device. GeoBase has built-in support for these primary GPS devices:
A simulated GPS device, using the SimulatedGps class
Generic NMEA compatible devices, using the NMEAGps class
To implement your own GPS device it is only necessary to implement your own IGps member. We will briefly discuss (below) the use of the two classes of device described above.
It is not necessary to have an actual GPS device on-hand, the SimulatedGps class will function as a substitute.
The NMEAGps class connects to a COM port, and is designed for interfacing directly with standard NMEA GPS hardware. This class expects exclusive access to the GPS device.
This tutorial assumes basic knowledge of GeoBase routing and the creation of a simple GeoBase application.
The following tutorials may provide useful background reading:
A SimulatedGps is a simulated GPS unit that follows a predefined set of points at the maximum allowable speed limit. The following code returns a SimulatedGps following a predefined route along a freeway:
private IGps GetGps() { /* define a sample route for us to simulate */ RouteStop start = new RouteStop(new LatLon(33.65856, -117.75528)); RouteStop end = new RouteStop(new LatLon(33.65006, -117.75523)); Route route = new Route(start, end); LatLon[] points = route.GetDirections().Points; /* create and return a SimulatedGps following the route points */ return new SimulatedGps(points[0], points); }
The NMEAGps class interfaces with a standard NMEA GPS device. The following code demonstrates the initialization of a NMEA GPS device connected to the COM1 serial port at a baud rate of 9600. You may need to adjust these properties to suit your GPS device.
private IGps GetGps() { /* create and configure the GPS's serial port */ SerialPort myPort = new SerialPort(); myPort.PortName = "COM1"; myPort.BaudRate = 9600; /* create NMEA GPS from port, power it up & return */ IGps myGps = new NMEAGps(myPort); myGps.PowerUp(); return myGps; }
Create a new Visual Studio Project. Add the following controls to the form:
When run your application should appear similar to the screenshot below:
The map can be centered and rotated to match the current location and heading of the GPS device by adding the following UpdateLocation method:
IGps myGps; RendererList renderList = new RendererList(); private void UpdateLocation(object sender, EventArgs e) { renderList.Add(new PushPin(myGps.Position.location)); mapMain.Center = myGps.Position.location; mapMain.Heading = myGps.Position.heading; mapMain.Invalidate(); /* invalidating the map causes a redraw */ }
To make the code simpler and more readable, add the following using directives to the top of the project form.
using Telogis.GeoBase.Routing; using Telogis.GeoBase.Navigation; using Telogis.GeoBase;
Add a click event to buttonGo. We will use the click event to initialize our chosen GPS device and start a timer which will update the map using the UpdateLocation() method (above). Ensure that the GetGps() function (see examples above) is implemented to return a suitable GPS device.
private void buttonGo_Click(object sender, EventArgs e) { mapMain.Renderer = renderList; /* set map to render everything on our list */ mapMain.Zoom = 0.5; /* zoom the map close */ myGps = GetGps(); /* initialize gps */ /* start a timer that fires every second, calling our eventhandler */ Timer tim = new Timer(); tim.Interval = 1000; tim.Tick += new EventHandler(UpdateLocation); tim.Start(); }
Run the application. After clicking the 'Go' button your location will be periodically shown on the map as a PushPin, and the map will rotate to match your direction of travel.
Tip |
---|
It is recommended that you create an archive of this project, as it will be modified slightly for each of the following tutorials |
The application will appear similar to the screenshot below: