Displaying GPS location on a map

There are two ways to display GPS location on a map using the API:

  1. using a GPSLayer, or
  2. using a Graphics Layer and a custom GPSEventListener.

The first option is best for the simple display of GPS location data, including the current location, previous trackpoints, and a trail connecting the trackpoints. This option includes the optional functionality to automatically pan the map to keep the latest GPS point visible. The second option is best if further customization of the display of GPS information is desired, for example to display satellite information on screen, or to change the GPS symbol based on the status of the GPS device. With both options it is possible to display the location data either from a connected device or from a text file containing raw NMEA sentences.

Building an application using a GPSLayer

If you have not already done so, create a Java application with a map as described in the topic Creating a Java map application.

In the Java map application, create the GPSLayer and add it to the map, placing the code after the tiled layer code which will serve as a basemap:

// create the tiled layer and add to map (could also be an ArcGISLocalTiledLayer)
ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
    "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.getLayers().add(tiledLayer);

// create the GPS layer and add to map
GPSLayer gpsLayer = new GPSLayer();
map.getLayers().add(gpsLayer);

The GPSLayer can be created with no parameters, as in the code above: this results in a default serial port GPS watcher (SerialPortGPSWatcher) being used internally to obtain data for the layer. This is the option to use to display data from a connected GPS device via a serial port (it could be a virtual serial port for testing purposes).

The GPSLayer can also be created using a custom IGPSWatcher as a parameter. For example, to display data from a file containing raw NMEA data, a FileGPSWatcher can be created then passed as a parameter to the layer:

IGPSWatcher watcher = new FileGPSWatcher("path of NMEA text file", 500, true);
GPSLayer gpsLayer = new GPSLayer(watcher);

To display data from a GPS device connected to a specific serial port, a SerialPortGPSWatcher can be created with the desired serial port information (SerialPortInfo), then passed as a parameter to the GPSLayer:

// create SerialPortInfo for a port named "COM2"
SerialPortInfo myPortInfo = new SerialPortInfo(
    "COM2", BaudRate.BAUD_4800, Parity.NONE, StopBits.ONE, 7);
SerialPortGPSWatcher myWatcher = new SerialPortGPSWatcher(myPortInfo);
GPSLayer gpsLayer = new GPSLayer(myWatcher);
NoteNote:

Creating a SerialPortInfo object with the parameters matching your serial port's settings is necessary if your serial port or virtual serial port is named something other than COM* on Windows, or /dev/ttyS* on Linux.

Now that your GPSLayer has been created and added to the map, you can run the application and it will display your location if you have a GPS receiver connected to your machine; if using a FileGPSWatcher, the application will display the location of the object tracked in the file containing the raw NMEA sentences.

The GPSLayer can be set to different modes, depending on how you want the map to pan and the GPS symbol aligned. Please see GPS layer modes for information on these modes. You can also customize the GPSLayer, for example by setting your own location symbol or choosing not to display a trail or trackpoints, by using the following methods on the GPSLayer:

Building an application using a custom GPSEventListener

For further customization of the display of GPS information - for example to display satellite information on screen, or to change the GPS symbol based on the status of the GPS device - a GraphicsLayer can be used in conjunction with a GPSWatcher and a custom GPSEventListener. The first step is to create a new GraphicsLayer, which will display the GPS data, and add it to the map:

GraphicsLayer myGpsLayer = new GraphicsLayer();
map.getLayers().add(myGpsLayer);

Secondly, a custom GPSEventListener must be created. This is a listener which implements the interface GPSEventListener. Four methods can be implemented:

These methods will contain the logic behind displaying GPS information or reacting to a change in the status of the GPS device, as the next step is to pass this custom GPSEventListener to a GPS Watcher. If displaying GPS data coming from a connected GPS device, a SerialPortGPSWatcher should be created, with the custom listener as a parameter in the constructor. If displaying GPS data saved to a text file (containing the raw NMEA sentences), a FileGPSWatcher should be created, with the custom listener as one of the parameters in the constructor. The listener thus receives events from the GPS watcher when an NMEA sentence is received, when the position of the tracked object has changed, when the satellites in view have changed, and when the status of the GPS watched has changed (representing the status of the underlying GPS device).

As an example, to display satellite information on screen using a custom GPSEventListener, the onSatellitesInViewChanged method of the listener can take the data passed in as a parameter and have it displayed in a UI element. To display the current location of the tracked object (one's own computer, if a GPS device is connected and receiving data), the onPositionChanged method must take the position received and have it displayed as a graphic in the GraphicsLayer of the map, with the appropriate projection from the spatial reference of the GPS data points (WGS84) to the spatial reference of the map, if they differ.

Sample code

Several interactive sample applications which use the methods discussed above are available in the Java Sample Application under the table of content heading 'GPS'.

2/7/2013