public void OnLocationChanged(Location location) { if (location.Accuracy <= desiredAccuracy) { Finish(location); return; } lock (locationSync) { if (GeolocationUtils.IsBetterLocation(location, bestLocation)) { bestLocation = location; } } }
/// <summary> /// Gets the last known and most accurate location. /// This is usually cached and best to display first before querying for full position. /// </summary> /// <returns>Best and most recent location or null if none found</returns> public async Task <Position> GetLastKnownLocationAsync() { var hasPermission = await CheckPermissions(); if (!hasPermission) { throw new GeolocationException(GeolocationError.Unauthorized); } Location bestLocation = null; foreach (var provider in Providers) { var location = Manager.GetLastKnownLocation(provider); if (location != null && GeolocationUtils.IsBetterLocation(location, bestLocation)) { bestLocation = location; } } return(bestLocation?.ToPosition()); }
public GeolocationSingleListener(LocationManager manager, float desiredAccuracy, int timeout, IEnumerable <string> activeProviders, Action finishedCallback) { this.desiredAccuracy = desiredAccuracy; this.finishedCallback = finishedCallback; this.activeProviders = new HashSet <string>(activeProviders); foreach (var provider in activeProviders) { var location = manager.GetLastKnownLocation(provider); if (location != null && GeolocationUtils.IsBetterLocation(location, bestLocation)) { bestLocation = location; } } if (timeout != Timeout.Infinite) { timer = new Timer(TimesUp, null, timeout, 0); } }