Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.GoogleMaps;
- namespace TrackingSample
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- GetActualLocationCommand = new Command(async () => await GetActualLocation());
- }
- //Center map in actual location
- protected override void OnAppearing()
- {
- base.OnAppearing();
- GetActualLocationCommand.Execute(null);
- }
- public static readonly BindableProperty GetActualLocationCommandProperty =
- BindableProperty.Create(nameof(GetActualLocationCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay);
- public ICommand GetActualLocationCommand
- {
- get { return (ICommand)GetValue(GetActualLocationCommandProperty); }
- set { SetValue(GetActualLocationCommandProperty, value); }
- }
- async Task GetActualLocation()
- {
- try
- {
- var request = new GeolocationRequest(GeolocationAccuracy.High);
- var location = await Geolocation.GetLocationAsync(request);
- if (location != null)
- {
- map.MoveToRegion(MapSpan.FromCenterAndRadius(
- new Position(location.Latitude, location.Longitude), Distance.FromMiles(0.3)));
- }
- }
- catch (Exception ex)
- {
- await DisplayAlert("Error", "Unable to get actual location", "Ok");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement