Parse NMEA sentences and use the results to show device location on the map.
Use case
NMEA sentences can be retrieved from GPS receivers and parsed into a series of coordinates with additional information. Devices without a built-in GPS receiver can retrieve NMEA sentences by using a separate GPS dongle, commonly connected bluetooth or through a serial port.
The NMEA location data source allows for detailed interrogation of the information coming from the GPS receiver. For example, allowing you to report the number of satellites in view.
How to use the sample
Tap "Start" to parse the NMEA sentences into a simulated location data source, and initiate the location display. Tap "Recenter" to recenter the location display. Tap "Reset" to reset the location display.
How it works
- Load NMEA sentences from a local file.
- Parse the NMEA sentence strings, and push data into
NmeaLocationDataSource
. - Set the
NmeaLocationDataSource
to the location display's data source. - Start the location display to begin receiving location and satellite updates.
Relevant API
- Location
- LocationDisplay
- NmeaLocationDataSource
- NmeaSatelliteInfo
About the data
A list of NMEA sentences is used to initialize a to simulate a stream of NMEA data. This simulated data source provides NMEA data periodically, and allows the sample to be used on devices without a GPS dongle that produces NMEA data.
The route taken in this sample features a 2-minute driving trip around Redlands, CA.
Additional information
Below is a list of protocol strings for commonly used GNSS external accessories. Please refer to the ArcGIS Field Maps documentation for model and firmware requirements.
Supported by this sample
- com.bad-elf.gps
- com.eos-gnss.positioningsource
- com.geneq.sxbluegpssource
Others
- com.amanenterprises.nmeasource
- com.dualav.xgps150
- com.garmin.pvt
- com.junipersys.geode
- com.leica-geosystems.zeno.gnss
- com.searanllc.serial
- com.trimble.correction, com.trimble.command (1)
(1) Some Trimble models require a proprietary SDK for NMEA output.
Tags
dongle, GNSS, GPS, history, navigation, NMEA, real-time, RTK, trace
Sample Code
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Location;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
namespace ArcGIS.WinUI.Samples.LocationWithNMEA
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Display device location with NMEA data sources",
category: "Location",
description: "Parse NMEA sentences and use the results to show device location on the map.",
instructions: "Tap \"Start\" to parse the NMEA sentences into a simulated location data source, and initiate the location display. Tap \"Recenter\" to recenter the location display. Tap \"Reset\" to reset the location display.",
tags: new[] { "GNSS", "GPS", "NMEA", "RTK", "dongle", "history", "navigation", "real-time", "trace" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData("d5bad9f4fee9483791e405880fb466da")]
public partial class LocationWithNMEA : IDisposable
{
private NmeaLocationDataSource _nmeaSource;
private NMEAStreamSimulator _simulatedNMEADataSource;
public LocationWithNMEA()
{
InitializeComponent();
_ = Initialize();
}
private async Task Initialize()
{
// Create the map.
MyMapView.Map = new Map(BasemapStyle.ArcGISNavigation);
MyMapView.SetViewpoint(new Viewpoint(new MapPoint(-117.191, 34.0306, SpatialReferences.Wgs84), 100000));
// Create the data simulation using stored mock data.
_simulatedNMEADataSource = new NMEAStreamSimulator(6.0);
_simulatedNMEADataSource.NmeaMessageChanged += UpdateNmeaMessageLabel;
// Create the NMEA data source.
_nmeaSource = new NmeaLocationDataSource(SpatialReferences.Wgs84);
// To create a NmeaLocationDataSource for a bluetooth device, use the `FromBluetooth` constructor. https://developers.arcgis.com/net/api-reference/api/uwp/Esri.ArcGISRuntime/Esri.ArcGISRuntime.Location.NmeaLocationDataSource.FromBluetooth.html
// To create a NmeaLocationDataSource from a serial port, use the `FromSerialPort` constructor. https://developers.arcgis.com/net/api-reference/api/uwp/Esri.ArcGISRuntime/Esri.ArcGISRuntime.Location.NmeaLocationDataSource.FromSerialPort.html
// Set the location data source to use the stream from the simulator.
Stream messageStream = _simulatedNMEADataSource.MessageStream;
_nmeaSource.NmeaDataStream = messageStream;
// Create an event handler to update the UI when the location changes.
_nmeaSource.SatellitesChanged += SatellitesChanged;
_nmeaSource.LocationChanged += LocationChanged;
// Start the location data source.
try
{
MyMapView.LocationDisplay.DataSource = _nmeaSource;
await _nmeaSource.StartAsync();
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, ex.Message.GetType().Name).ShowAsync();
}
}
private void UpdateNmeaMessageLabel(object sender, NmeaMessageEventArgs e)
{
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
NmeaMessageLabel.Text = e.NmeaMessage;
});
}
private void SatellitesChanged(object sender, IReadOnlyList<NmeaSatelliteInfo> infos)
{
// Check for any satellite info.
if (infos.Count == 0) return;
// Get all of the satellite Id's from the satellite infos.
SortedSet<int> uniqueSatelliteIds = new SortedSet<int>();
foreach (var info in infos)
{
uniqueSatelliteIds.Add(info.Id);
}
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
// Show the status information in the UI.
CountLabel.Text = $"Satellite count: {infos.Count}";
SatellitesLabel.Text = $"Satellites: {string.Join(", ", uniqueSatelliteIds)}";
SystemLabel.Text = $"System: {infos.First().System}";
});
}
private void LocationChanged(object sender, Location loc)
{
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
// Show the status information in the UI.
AccuracyLabel.Text = $"Accuracy: H {string.Format("{0:0.00}", loc.HorizontalAccuracy)} meters, V {string.Format("{0:0.00}", loc.VerticalAccuracy)} meters";
// Recenter on the new location.
MyMapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.Recenter;
});
}
private void StartClick(object sender, RoutedEventArgs e)
{
_simulatedNMEADataSource.Start();
AccuracyLabel.Text = string.Empty;
}
private void ResetClick(object sender, RoutedEventArgs e)
{
_simulatedNMEADataSource.Stop();
_simulatedNMEADataSource.Reset();
// Reset the labels.
AccuracyLabel.Text = "Simulation reset.";
CountLabel.Text = string.Empty;
SatellitesLabel.Text = string.Empty;
SystemLabel.Text = string.Empty;
NmeaMessageLabel.Text = string.Empty;
}
public void Dispose()
{
// Stop the location data source.
MyMapView.LocationDisplay?.DataSource?.StopAsync();
_simulatedNMEADataSource?.Dispose();
}
}
/*
* This class uses mock data (an edited recording of a real NMEA data stream) to simulate live NMEA data and create a stream.
* For NMEA location data sources created from a Bluetooth device or serial input, you may not need to create your own stream.
* For any other case, you can write the data to a memory stream like below.
*/
public class NMEAStreamSimulator : IDisposable
{
private Timer _timer;
public Stream MessageStream;
private int _lineCounter = 0;
private const int DefaultInterval = 1000;
private string[] _nmeaStrings;
public event EventHandler<NmeaMessageEventArgs> NmeaMessageChanged;
public NMEAStreamSimulator(double speed = 1.0)
{
_timer = new Timer(DefaultInterval / speed);
// Populate an array with all of the mock NMEA data.
string nmeaMockDataPath = DataManager.GetDataFolder("d5bad9f4fee9483791e405880fb466da", "Redlands.nmea");
_nmeaStrings = File.ReadAllText(nmeaMockDataPath).Split('\n');
// Create a data stream for the `NmeaLocationDataSource` to use.
MessageStream = new MemoryStream();
_timer.Elapsed += TimerElapsed;
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
public void Reset()
{
_timer.Stop();
_lineCounter = 0;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
// Get the next NMEA string and append return and newline characters to it.
string nmeaString = $"{_nmeaStrings[_lineCounter]}\r\n";
NmeaMessageChanged?.Invoke(this, new NmeaMessageEventArgs { NmeaMessage = nmeaString });
// Check if the start of a new location message.
if (nmeaString.StartsWith("$GPGGA"))
{
// Flush any existing NMEA data from the stream.
MessageStream.Flush();
}
// Write the string to the stream.
byte[] data = System.Text.Encoding.UTF8.GetBytes(nmeaString);
MessageStream.Write(data, 0, data.Length);
// Increment the line counter.
_lineCounter = (_lineCounter + 1) % _nmeaStrings.Length;
}
public void Dispose()
{
// Dispose of the timer and stream.
_timer.Stop();
_timer.Dispose();
MessageStream.Dispose();
}
}
public class NmeaMessageEventArgs : EventArgs
{
public string NmeaMessage { get; set; }
}
}