private void Finish(Location location) { var p = new Position(); if (location.HasAccuracy) { p.Accuracy = location.Accuracy; } if (location.HasAltitude) { p.Altitude = location.Altitude; } if (location.HasBearing) { p.Heading = location.Bearing; } if (location.HasSpeed) { p.Speed = location.Speed; } p.Longitude = location.Longitude; p.Latitude = location.Latitude; p.Timestamp = GeolocatorImplementation.GetTimestamp(location); if (this.finishedCallback != null) { this.finishedCallback(); } this.completionSource.TrySetResult(p); }
public void OnLocationChanged(Location location) { if (location.Provider != this.activeProvider) { if (this.activeProvider != null && this.manager.IsProviderEnabled(this.activeProvider)) { LocationProvider pr = this.manager.GetProvider(location.Provider); TimeSpan lapsed = GetTimeSpan(location.Time) - GetTimeSpan(this.lastLocation.Time); if (pr.Accuracy > this.manager.GetProvider(this.activeProvider).Accuracy && lapsed < timePeriod.Add(timePeriod)) { location.Dispose(); return; } } this.activeProvider = location.Provider; } var previous = Interlocked.Exchange(ref this.lastLocation, location); if (previous != null) { previous.Dispose(); } var p = new Position(); if (location.HasAccuracy) { p.Accuracy = location.Accuracy; } if (location.HasAltitude) { p.Altitude = location.Altitude; } if (location.HasBearing) { p.Heading = location.Bearing; } if (location.HasSpeed) { p.Speed = location.Speed; } p.Longitude = location.Longitude; p.Latitude = location.Latitude; p.Timestamp = GeolocatorImplementation.GetTimestamp(location); var changed = PositionChanged; if (changed != null) { changed(this, new PositionEventArgs(p)); } }
private void HandleTimeout(object state) { if (state != null && (bool)state) { this.tcs.TrySetCanceled(); } if (this.bestPosition != null) { this.tcs.TrySetResult(GeolocatorImplementation.GetPosition(this.bestPosition)); } else { this.tcs.TrySetCanceled(); } }
private void WatcherOnPositionChanged(object sender, GeoPositionChangedEventArgs <GeoCoordinate> e) { if (e.Position.Location.IsUnknown) { return; } bool isRecent = timeout == Timeout.Infinite || (e.Position.Timestamp - this.start).TotalMilliseconds < this.timeout; if (e.Position.Location.HorizontalAccuracy <= this.desiredAccuracy && isRecent) { this.tcs.TrySetResult(GeolocatorImplementation.GetPosition(e.Position)); } if (this.bestPosition == null || e.Position.Location.HorizontalAccuracy < this.bestPosition.Location.HorizontalAccuracy) { this.bestPosition = e.Position; } }
internal SinglePositionListener(double accuracy, int timeout, CancellationToken cancelToken) { cancelToken.Register(HandleTimeout, true); this.desiredAccuracy = accuracy; this.start = DateTime.Now; this.timeout = timeout; System.Threading.Tasks.Task.Factory.StartNew(() => { this.watcher = new GeoCoordinateWatcher(GeolocatorImplementation.GetAccuracy(accuracy)); this.watcher.PositionChanged += WatcherOnPositionChanged; this.watcher.StatusChanged += WatcherOnStatusChanged; this.watcher.Start(); }); if (timeout != Timeout.Infinite) { this.timer = new Timer(HandleTimeout, null, timeout, Timeout.Infinite); } Task.ContinueWith(Cleanup); }