Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created December 2, 2022 19:56
Show Gist options
  • Save icebeam7/ced7a2a73f12133fe91a8d4eb41d66fa to your computer and use it in GitHub Desktop.
Save icebeam7/ced7a2a73f12133fe91a8d4eb41d66fa to your computer and use it in GitHub Desktop.
ViewModel for Map application. An ObservableCollection and Command are exposed to get and show the current device location
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls.Maps;
using CommunityToolkit.Mvvm.Input;
using MapDemo.Models;
namespace MapDemo.ViewModels
{
public partial class MapViewModel : BaseViewModel
{
public ObservableCollection<Place> Places { get; } = new();
private CancellationTokenSource cts;
private IGeolocation geolocation;
private IGeocoding geocoding;
public MapViewModel(IGeolocation geolocation, IGeocoding geocoding)
{
this.geolocation = geolocation;
this.geocoding = geocoding;
}
[RelayCommand]
private async Task GetCurrentLocationAsync()
{
try
{
cts = new CancellationTokenSource();
var request = new GeolocationRequest(
GeolocationAccuracy.Medium,
TimeSpan.FromSeconds(10));
var location = await Geolocation.GetLocationAsync(request, cts.Token);
var placemarks = await Geocoding.GetPlacemarksAsync(location);
var address = placemarks?.FirstOrDefault()?.AdminArea;
Places.Clear();
Places.Add(new Place()
{
Location = location,
Address = address,
Description = "Current Location"
});
}
catch (Exception ex)
{
// Unable to get location
}
}
[RelayCommand]
private void DisposeCancellationToken()
{
if (cts != null && !cts.IsCancellationRequested)
cts.Cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment