How to use a GPS for WPF
If you have not already done so, create a WPF application with a Map as described in the topic Creating a map. Your application's XAML should appear as follows:
<Window x:Class="ArcGIS_WPF_Map_Application.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!-- Map Control -->
<esri:Map x:Name="Map" Background="White" Extent="-20014711, 15, 1656956, 12175318">
<esri:ArcGISTiledMapServiceLayer ID="BaseLayer"
Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
</esri:Map>
</Grid>
</Window>
The code in the main page's code-behind (e.g. MainWindow.xaml.cs) should be unchanged from the Creating a map topic.
In the XAML
- In your application, add a reference to ESRI.ArcGIS.Client.Toolkit.DataSources and System.Device. In addition, ensure your application has a reference to ESRI.ArcGIS.Client.dll and ESRI.ArcGIS.Client.Local.dll, if not previously added.
Inside the Map's XAML element, declare a GPS layer. Be sure to put it below the tiled map service layer:
<esri:Map x:Name="Map" Background="White" Extent="-20014711, 15, 1656956, 12175318"> <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" /> <esri:GpsLayer x:Name="MyGpsLayer" /> </esri:Map>
When you declare a GPS layer, you must give it a unique Name. You will use this Name in your application's .NET code (code-behind) to get a reference to the layer. The code above assigns the GpsLayer a name of "MyGpsLayer."
In the Code-Behind
- Add using statements for ESRI.ArcGIS.Client.Local and ESRI.ArcGIS.Client.Local.Gps
- Create a class-level variable for the GpsCoordinateWatcher.
If you are using a GPS receiver:
SerialPortGpsCoordinateWatcher _locationWatcher;
For testing and demonstration purposes, if you do not have a GPS/GNSS receiver connected to your system you can replay NMEA sentences from a file.
FileGpsCoordinateWatcher _locationWatcher;
- In the class constructor, initialize the local server.
LocalServer.Initialize();
Then initialize the GpsCoordinateWatcher.
If using a GPS receiver, initialize the GpsCoordinateWatcher with the connection information for your receiver
_locationWatcher = new SerialPortGpsCoordinateWatcher() { PortName = "COM#" };
Where the # is replaced with the port number for your receiver.
If you are replaying NMEA sentences from a file, initialize the GpsCoordinateWatcher with the file's path
_locationWatcher = new FileGpsCoordinateWatcher() { Path = @"Path to File" };
Where "Path to File" gets replaced with the path to the file containing NMEA sentences. A sample file has been included with the library, you can find it in the default install directory in SDK\Samples\Data\GPS\campus.txt.
- Set the GPS display layer's GeoPositionWatcher to the newly created
location watcher
MyGpsLayer.GeoPositionWatcher = _locationWatcher
If using a GPS receiver the code-behind should look similar to:
using System.Windows;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client.Local.Gps;
using ESRI.ArcGIS.Client.Toolkit.DataSources;
namespace ArcGISWPFMapApplication1
{
public partial class MainWindow : Window
{
SerialPortGpsCoordinateWatcher _locationWatcher;
public MainWindow()
{
InitializeComponent();
LocalServer.Initialize();
_locationWatcher = new SerialPortGpsCoordinateWatcher() { PortName = "COM2" };
MyGpsLayer.GeoPositionWatcher = _locationWatcher;
}
}
}
If you are replaying NMEA sentences from a file the code-behind should look similar to:
using System.Windows;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client.Local.Gps;
using ESRI.ArcGIS.Client.Toolkit.DataSources;
namespace ArcGISWPFMapApplication1
{
public partial class MainWindow : Window
{
FileGpsCoordinateWatcher _locationWatcher;
public MainWindow()
{
InitializeComponent();
LocalServer.Initialize();
_locationWatcher = new FileGpsCoordinateWatcher()
{
Path = @"C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\gps\campus.txt"
};
MyGpsLayer.GeoPositionWatcher = _locationWatcher;
}
}
}