Skip to content

Instantly share code, notes, and snippets.

@devjaime
Created April 20, 2019 23:06
Show Gist options
  • Select an option

  • Save devjaime/bbda1487a4a5ac4aaced138fa2a7afa3 to your computer and use it in GitHub Desktop.

Select an option

Save devjaime/bbda1487a4a5ac4aaced138fa2a7afa3 to your computer and use it in GitHub Desktop.
viewmodel location
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