-
-
Save devjaime/bbda1487a4a5ac4aaced138fa2a7afa3 to your computer and use it in GitHub Desktop.
viewmodel location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading; | |
| using System.Windows.Input; | |
| using Xamarin.Essentials; | |
| using Xamarin.Forms; | |
| namespace Geolocalizacion.ViewModel | |
| { | |
| public class GeolocationViewModel : BaseViewModel | |
| { | |
| string notAvailable = "no disponible"; | |
| string lastLocation; | |
| string currentLocation; | |
| int accuracy = (int)GeolocationAccuracy.Default; | |
| CancellationTokenSource cts; | |
| public GeolocationViewModel() | |
| { | |
| GetLastLocationCommand = new Command(OnGetLastLocation); | |
| GetCurrentLocationCommand = new Command(OnGetCurrentLocation); | |
| } | |
| public ICommand GetLastLocationCommand { get; } | |
| public ICommand GetCurrentLocationCommand { get; } | |
| public string LastLocation | |
| { | |
| get => lastLocation; | |
| set => SetProperty(ref lastLocation, value); | |
| } | |
| public string CurrentLocation | |
| { | |
| get => currentLocation; | |
| set => SetProperty(ref currentLocation, value); | |
| } | |
| public string[] Accuracies | |
| => Enum.GetNames(typeof(GeolocationAccuracy)); | |
| public int Accuracy | |
| { | |
| get => accuracy; | |
| set => SetProperty(ref accuracy, value); | |
| } | |
| async void OnGetLastLocation() | |
| { | |
| if (IsBusy) | |
| return; | |
| IsBusy = true; | |
| try | |
| { | |
| var location = await Geolocation.GetLastKnownLocationAsync(); | |
| LastLocation = FormatLocation(location); | |
| } | |
| catch (Exception ex) | |
| { | |
| LastLocation = FormatLocation(null, ex); | |
| } | |
| IsBusy = false; | |
| } | |
| async void OnGetCurrentLocation() | |
| { | |
| if (IsBusy) | |
| return; | |
| IsBusy = true; | |
| try | |
| { | |
| var request = new GeolocationRequest((GeolocationAccuracy)Accuracy); | |
| cts = new CancellationTokenSource(); | |
| var location = await Geolocation.GetLocationAsync(request, cts.Token); | |
| CurrentLocation = FormatLocation(location); | |
| } | |
| catch (Exception ex) | |
| { | |
| CurrentLocation = FormatLocation(null, ex); | |
| } | |
| finally | |
| { | |
| cts.Dispose(); | |
| cts = null; | |
| } | |
| IsBusy = false; | |
| } | |
| string FormatLocation(Location location, Exception ex = null) | |
| { | |
| if (location == null) | |
| { | |
| return $"No se puede detectar la ubicación. Excepción: {ex?.Message ?? string.Empty}"; | |
| } | |
| return | |
| $"Latitud: {location.Latitude}\n" + | |
| $"Longitud: {location.Longitude}\n" + | |
| $"Exactitud: {location.Accuracy}\n" + | |
| $"Altitud: {(location.Altitude.HasValue ? location.Altitude.Value.ToString() : notAvailable)}\n" + | |
| $"Titulo: {(location.Course.HasValue ? location.Course.Value.ToString() : notAvailable)}\n" + | |
| $"Velocidad: {(location.Speed.HasValue ? location.Speed.Value.ToString() : notAvailable)}\n" + | |
| $"Fecha (UTC): {location.Timestamp:d}\n" + | |
| $"Hora (UTC): {location.Timestamp:T}" + | |
| $"Moking Provider: {location.IsFromMockProvider}"; | |
| } | |
| public override void OnDisappearing() | |
| { | |
| if (IsBusy) | |
| { | |
| if (cts != null && !cts.IsCancellationRequested) | |
| cts.Cancel(); | |
| } | |
| base.OnDisappearing(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment