using System; using System.Linq; using System.Collections.Generic; using System.Text; using SpeedTracker.Common; using SpeedTracker.Converters; using SpeedTracker.Gps; namespace SpeedTracker { class GpsLocationProvider : ILocationProvider { private const string ProviderName = "GPSID Provider"; private GpsId _gpsId; private bool _hasFix = false; private bool _isActive = false; private int _fixQuality = 0; private IValueConverter _speedConverter=null; private Location _lastLocation = new Location(); public object SyncRoot = new object(); public GpsLocationProvider() { _speedConverter = SpeedTracker.Converters.ConverterCatalog.GetConverter(new ConversionUnitNames("nautical mile", "kilometer")); } #region ILocationProvider Members public string Name { get { return ProviderName; } } public event EventHandler<LocationUpdatedEventArgs> LocationUpdated; protected void OnLocationUpdated(Location newLocation) { if (LocationUpdated != null) lock (SyncRoot) { if (LocationUpdated != null) { LocationUpdated(this, new LocationUpdatedEventArgs() {NewLocation = newLocation}); } } } public bool IsActive { get { return _isActive; } } public bool HasFix { get { return _hasFix; } } public Location LastLocation { get { return _lastLocation; } } public void Start() { if(_gpsId==null) { _gpsId = new GpsId(); _gpsId.LocationChanged += new LocationChangedEventHandler(gpsId_LocationChanged); _gpsId.DeviceStateChanged += new DeviceStateChangedEventHandler(_gpsId_DeviceStateChanged); } if(_gpsId!=null) { _gpsId.Open(); _isActive = true; } } void _gpsId_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args) { } void gpsId_LocationChanged(object sender, LocationChangedEventArgs args) { if(!_isActive) return; Location newLoc = new Location() {TimeStamp = DateTime.MinValue}; newLoc.IsLocationValid = _hasFix = (args.Position != null) && (args.Position.LongitudeValid && args.Position.LongitudeValid); if (_hasFix) { newLoc.Latitude = args.Position.Latitude; newLoc.Longitude = args.Position.Longitude; newLoc.Speed = (float) (double) _speedConverter.Convert((double)args.Position.Speed, typeof (double), null, null); newLoc.Heading = args.Position.Heading; newLoc.TimeStamp = args.Position.Time; } this._lastLocation = newLoc; OnLocationUpdated(newLoc); } public void Stop() { if (_gpsId != null) { _gpsId.Close(); _gpsId.LocationChanged -= (gpsId_LocationChanged); _gpsId.DeviceStateChanged -= (_gpsId_DeviceStateChanged); } _isActive = false; _hasFix = false; } #endregion #region IDisposable Members public void Dispose() { } #endregion } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)