private async Task <Xamarin.Essentials.Location> InternalGetLocation() { MasterDetail.statusPermissions = new Utils.StatusPermissions(); if (MasterDetail.statusPermissions.CheckConnection()) { Xamarin.Essentials.Location results = null; try { Xamarin.Essentials.GeolocationRequest geoRequest = new Xamarin.Essentials.GeolocationRequest(Xamarin.Essentials.GeolocationAccuracy.Medium); results = await Xamarin.Essentials.Geolocation.GetLocationAsync(geoRequest); } catch (Exception ex) { await this.DisplayAlert("Need location", "Active location sensor", "OK"); } return(results); } else { return(null); }; }
static GeoManager() { GeoRequest = new Xamarin.Essentials.GeolocationRequest(Xamarin.Essentials.GeolocationAccuracy.Best); DefaultZone = new Zone() { Location = new Xamarin.Essentials.Location() { Latitude = 0.0, Longitude = -0.0 }, Radius = 0.0, Color = Color.White, Name = "White" }; InitializeZones(); }
static async Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { if (!CLLocationManager.LocationServicesEnabled) { throw new FeatureNotEnabledException("Location services are not enabled on device."); } await Permissions.RequestAsync <Permissions.LocationWhenInUse>(); // the location manager requires an active run loop // so just use the main loop CLLocationManager manager = null; NSRunLoop.Main.InvokeOnMainThread(() => manager = new CLLocationManager()); var tcs = new TaskCompletionSource <CLLocation>(manager); var listener = new SingleLocationListener(); listener.LocationHandler += HandleLocation; cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout); cancellationToken.Register(Cancel); manager.DesiredAccuracy = request.PlatformDesiredAccuracy; manager.Delegate = listener; #if __IOS__ // we're only listening for a single update manager.PausesLocationUpdatesAutomatically = false; #endif manager.StartUpdatingLocation(); var clLocation = await tcs.Task; return(clLocation?.ToLocation()); void HandleLocation(CLLocation location) { manager.StopUpdatingLocation(); tcs.TrySetResult(location); } void Cancel() { manager.StopUpdatingLocation(); tcs.TrySetResult(null); } }
static async Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { await Permissions.RequireAsync(PermissionType.LocationWhenInUse); var geolocator = new Geolocator { DesiredAccuracyInMeters = request.PlatformDesiredAccuracy }; cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout); var location = await geolocator.GetGeopositionAsync().AsTask(cancellationToken); return(location?.Coordinate?.ToLocation()); }
static async Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { await Permissions.RequireAsync(PermissionType.LocationWhenInUse); // the location manager requires an active run loop // so just use the main loop CLLocationManager manager = null; NSRunLoop.Main.InvokeOnMainThread(() => manager = new CLLocationManager()); var tcs = new TaskCompletionSource <CLLocation>(manager); var listener = new SingleLocationListener(); listener.LocationHandler += HandleLocation; cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout); cancellationToken.Register(Cancel); manager.DesiredAccuracy = request.PlatformDesiredAccuracy; manager.Delegate = listener; // we're only listening for a single update manager.PausesLocationUpdatesAutomatically = false; manager.StartUpdatingLocation(); var clLocation = await tcs.Task; if (clLocation == null) { return(null); } return(clLocation.ToLocation()); void HandleLocation(CLLocation location) { manager.StopUpdatingLocation(); tcs.TrySetResult(location); } void Cancel() { manager.StopUpdatingLocation(); tcs.TrySetResult(null); } }
public async Task SetCurrentLocation() { try { Essentials.GeolocationRequest request = new Essentials.GeolocationRequest(Essentials.GeolocationAccuracy.High); Essentials.Location location = await Essentials.Geolocation.GetLocationAsync(request); if (location != null) { Position = new Position(location.Latitude, location.Longitude); } } catch { await DisplayAlertAsync("Unable to get current location. Ensure geolocation is active on the device."); } }
static async Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { await Permissions.RequestAsync <Permissions.LocationWhenInUse>(); var locationManager = Platform.LocationManager; var enabledProviders = locationManager.GetProviders(true); var hasProviders = enabledProviders.Any(p => !ignoredProviders.Contains(p)); if (!hasProviders) { throw new FeatureNotEnabledException("Location services are not enabled on device."); } // get the best possible provider for the requested accuracy var providerInfo = GetBestProvider(locationManager, request.DesiredAccuracy); // if no providers exist, we can't get a location // let's punt and try to get the last known location if (string.IsNullOrEmpty(providerInfo.Provider)) { return(await GetLastKnownLocationAsync()); } var tcs = new TaskCompletionSource <AndroidLocation>(); var allProviders = locationManager.GetProviders(false); var providers = new List <string>(); if (allProviders.Contains(LocationManager.GpsProvider)) { providers.Add(LocationManager.GpsProvider); } if (allProviders.Contains(LocationManager.NetworkProvider)) { providers.Add(LocationManager.NetworkProvider); } if (providers.Count == 0) { providers.Add(providerInfo.Provider); } var listener = new SingleLocationListener(locationManager, providerInfo.Accuracy, providers); listener.LocationHandler = HandleLocation; cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout); cancellationToken.Register(Cancel); // start getting location updates // make sure to use a thread with a looper var looper = Looper.MyLooper() ?? Looper.MainLooper; foreach (var provider in providers) { locationManager.RequestLocationUpdates(provider, 0, 0, listener, looper); } var androidLocation = await tcs.Task; if (androidLocation == null) { return(null); } return(androidLocation.ToLocation()); void HandleLocation(AndroidLocation location) { RemoveUpdates(); tcs.TrySetResult(location); } void Cancel() { RemoveUpdates(); tcs.TrySetResult(listener.BestLocation); } void RemoveUpdates() { for (var i = 0; i < providers.Count; i++) { locationManager.RemoveUpdates(listener); } } }
public static Task <Location> GetLocationAsync(GeolocationRequest request, CancellationToken cancelToken) => PlatformLocationAsync(request ?? new GeolocationRequest(), cancelToken);
public static Task <Location> GetLocationAsync(GeolocationRequest request) => PlatformLocationAsync(request ?? new GeolocationRequest(), default);
static async Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { await Permissions.RequireAsync(PermissionType.LocationWhenInUse); Locator service = null; var gps = Platform.GetFeatureInfo <bool>("location.gps"); var wps = Platform.GetFeatureInfo <bool>("location.wps"); if (gps) { if (wps) { service = new Locator(LocationType.Hybrid); } else { service = new Locator(LocationType.Gps); } } else { if (wps) { service = new Locator(LocationType.Wps); } else { service = new Locator(LocationType.Passive); } } var tcs = new TaskCompletionSource <bool>(); cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout); cancellationToken.Register(() => { service?.Stop(); tcs.TrySetResult(false); }); double KmToMetersPerSecond(double km) => km * 0.277778; service.LocationChanged += (s, e) => { if (e.Location != null) { lastKnownLocation.Accuracy = e.Location.Accuracy; lastKnownLocation.Altitude = e.Location.Altitude; lastKnownLocation.Course = e.Location.Direction; lastKnownLocation.Latitude = e.Location.Latitude; lastKnownLocation.Longitude = e.Location.Longitude; lastKnownLocation.Speed = KmToMetersPerSecond(e.Location.Speed); lastKnownLocation.Timestamp = e.Location.Timestamp; } service?.Stop(); tcs.TrySetResult(true); }; service.Start(); await tcs.Task; return(lastKnownLocation); }
static Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) => throw new NotImplementedInReferenceAssemblyException();
static Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) => throw ExceptionUtils.NotSupportedOrImplementedException;