Getting started with GPS development
This sample is a basic implementation of how the Global Positioning System (GPS) classes are used to provide location information. When a GPS connection is established, a GPS track displays on the map via the GPS Display object. This sample does not require a GPS device, since a pre-recorded GPS track is included.
Working with GpsDisplay
To start using GPS information in your application, add the SerialPortGpsConnection component from the ArcGIS Mobile Controls tab in Visual Studio Toolbox to your project. The properties on this component configure the communication properties between your device and the receiver, and include items such as the com port and baud rate. The common practice is to leave the default values and set the COM port.
To receive GPS information from the receiver, you need to call SerialPortGpsConnection.Open within your code. Usually this is called from an appropriate control on your user interface (UI), such as a button or menu item. To stop receiving information, call the corresponding Close method.
The GPS location can be displayed on your map using the GpsDisplay component by setting the GpsConnection property to the SerialPortGpsConnection that you previously added. When you run your application and open the GpsConnection, the map will center on the GPS location and display the position with a marker.
// can be hardcoded or using "AUTO" keyword it will try to find the correct port
serialPortGpsConnection1.PortName = "COM5";
serialPortGpsConnection1.Open();
//this links the GpsConnection to the Display
gpsDisplay1.GpsConnection = serialPortGpsConnection1;
gpsDisplay1.Map = map1;
//to follow proper programming guidelines you must close the connection when finished
serialPortGpsConnection1.Close();
The GpsDisplay.Autopan property does not keep the GpsDisplay icon in the center of map. This property is used to ensure that the map will automatically pan, so the GPS display icon remains in the visible area.
Working with GPS information
In addition to displaying the GPS position on the map, you can also obtain the GPS information received from the connection. Each time a GPS sentence is received by the GpsConnection, a GpsChanged event is fired. You can listen to this event and get a GpsEventArgs class. Due to the multi-threaded architecture of the components, you should include the appropriate code to handle thread pools as shown in the following example:
private void
serialPortGpsConnection1_GpsChanged(object sender, GpsEventArgs e)
{
if (InvokeRequired) { Invoke(new GpsChangedEventHandler(serialPortGpsConnection1_GpsChanged),sender, e);
return;
}
MessageBox.Show(e.Altitude);
}
Where to get the sample
The sample is available for download from ArcGIS Online.