This sample demonstrates how to use the
SerialPortGpsCoordinateWatcher to search for a serial port with a
connected GPS receiver and to show the reported location on a map.
<UserControlx:Class="ArcGISWPFSDK.SerialPortGpsLayer"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"Unloaded="UserControl_Unloaded"><Grid><Grid.Resources><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:MapExtent="-13046586.9363318,4036013.64823107,-13045676.4100906,4036832.85611"><esri:ArcGISTiledMapServiceLayerID="arcGISTiledMapServiceLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/><esri:GpsLayerx:Name="gpsLayer"/></esri:Map><BorderBackground="#995C90B2"BorderThickness="1"CornerRadius="5"HorizontalAlignment="Center"VerticalAlignment="Center"Visibility="{Binding ElementName=txtInfo, Path=Visibility}"Margin="20"Padding="5"BorderBrush="Black"><TextBlockx:Name="txtInfo"Visibility="Collapsed"Text="Looking for GPS device..."/></Border><GridHorizontalAlignment="Left"VerticalAlignment="Top"Margin="10,10,10,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#DDFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="5,5,5,10"/><StackPanel><TextBlockx:Name="Instructions"Text="Click Start to search for a GPS device attached to a serial port and click Stop to cancel the search."Width="200"TextAlignment="Left"Margin="15,15,15,5"TextWrapping="Wrap"Foreground="Black"/><ButtonWidth="100"Margin="15,5,15,20"Content="Start"x:Name="btnGps"Click="btnGps_Click"/></StackPanel></Grid></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Device.Location;
using System.Threading;
namespace ArcGISWPFSDK
{
///<summary>/// Interaction logic for SerialPortGpsLayer.xaml///</summary>publicpartialclass SerialPortGpsLayer : UserControl
{
IGeoPositionWatcher<GeoCoordinate> _locationWatcher;
bool _isStarted;
public SerialPortGpsLayer()
{
InitializeComponent();
Application.Current.Exit += new ExitEventHandler(Current_Exit);
}
privatevoid CreateGpsWatcher(List<string> serialPorts)
{
if (!this.Dispatcher.CheckAccess())
{
// Invoke in calling threadthis.Dispatcher.Invoke((ThreadStart)delegate() { CreateGpsWatcher(serialPorts); });
return;
}
btnGps.IsEnabled = true;
if (serialPorts == null)
{
txtInfo.Text = "GPS device not found";
return;
}
// Hide searching text
txtInfo.Visibility = System.Windows.Visibility.Collapsed;
_locationWatcher = new ESRI.ArcGIS.Client.Local.Gps.SerialPortGpsCoordinateWatcher() { PortName = serialPorts[0] };
gpsLayer.GeoPositionWatcher = _locationWatcher;
}
void Current_Exit(object sender, ExitEventArgs e)
{
CleanUp();
}
privatevoid UserControl_Unloaded(object sender, RoutedEventArgs e)
{
CleanUp();
}
privatevoid CleanUp()
{
if (_locationWatcher == null)
return;
//Cleanup IGeoPositionWatcher resource
IDisposable gpsWatcher = _locationWatcher as IDisposable;
if (gpsWatcher != null)
gpsWatcher.Dispose();
}
privatevoid btnGps_Click(object sender, RoutedEventArgs e)
{
txtInfo.Visibility = System.Windows.Visibility.Visible;
if (_isStarted)
{
CleanUp();
btnGps.IsEnabled = true;
btnGps.Content = "Start";
txtInfo.Visibility = System.Windows.Visibility.Collapsed;
_isStarted = false;
return;
}
_isStarted = true;
btnGps.Content = "Stop";
txtInfo.Text = "Looking for GPS device...";
ThreadPool.QueueUserWorkItem((WaitCallback)delegate
{
// Search for GPS port
CreateGpsWatcher(ESRI.ArcGIS.Client.Local.Gps.SerialPortGpsCoordinateWatcher.FindGPSEnabledPort(1));
});
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Device.Location
Imports System.Threading
''' <summary>''' Interaction logic for SerialPortGpsLayer.xaml''' </summary>Namespace ArcGISWPFSDK
PartialPublicClass SerialPortGpsLayer
Inherits UserControl
Private _locationWatcher As IGeoPositionWatcher(Of GeoCoordinate)
Private _isStarted AsBooleanPublicSubNew()
InitializeComponent()
AddHandler Application.Current.[Exit], New ExitEventHandler(AddressOf Current_Exit)
EndSubPrivateSub CreateGpsWatcher(serialPorts As List(Of String))
IfNotMe.Dispatcher.CheckAccess() Then'Invoke in calling threadMe.Dispatcher.Invoke(DirectCast(Sub() CreateGpsWatcher(serialPorts), ThreadStart))
ReturnEndIf
btnGps.IsEnabled = TrueIf serialPorts IsNothingThen
txtInfo.Text = "GPS device not found"ReturnEndIf'Hide searching text
txtInfo.Visibility = System.Windows.Visibility.Collapsed
_locationWatcher = New ESRI.ArcGIS.Client.Local.Gps.SerialPortGpsCoordinateWatcher() With { _
.PortName = serialPorts(0) _
}
gpsLayer.GeoPositionWatcher = _locationWatcher
EndSubPrivateSub Current_Exit(sender AsObject, e As ExitEventArgs)
CleanUp()
EndSubPrivateSub UserControl_Unloaded(sender AsObject, e As RoutedEventArgs)
CleanUp()
EndSubPrivateSub CleanUp()
If _locationWatcher IsNothingThenReturnEndIf'Cleanup GpsCoordinateWatcher resourceDim gpsWatcher As IDisposable = TryCast(_locationWatcher, IDisposable)
If gpsWatcher IsNotNothingThen
gpsWatcher.Dispose()
EndIfEndSubPrivateSub btnGps_Click(sender AsObject, e As RoutedEventArgs)
txtInfo.Visibility = System.Windows.Visibility.Visible
If _isStarted Then
CleanUp()
btnGps.IsEnabled = True
btnGps.Content = "Start"
txtInfo.Visibility = System.Windows.Visibility.Collapsed
_isStarted = FalseReturnEndIf
_isStarted = True
btnGps.Content = "Stop"
txtInfo.Text = "Looking for GPS device..."'Search for GPS port
ThreadPool.QueueUserWorkItem(DirectCast(Sub() CreateGpsWatcher(ESRI.ArcGIS.Client.Local.Gps.SerialPortGpsCoordinateWatcher.FindGPSEnabledPort(1)), WaitCallback))
EndSubEndClassEndNamespace