Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. using Xamarin.Essentials;
  2. using Xamarin.Forms;
  3. using Xamarin.Forms.GoogleMaps;
  4.  
  5. namespace TrackingSample
  6. {
  7. public partial class MainPage : ContentPage
  8. {
  9. public MainPage()
  10. {
  11. InitializeComponent();
  12. GetActualLocationCommand = new Command(async () => await GetActualLocation());
  13.  
  14. }
  15. //Center map in actual location
  16. protected override void OnAppearing()
  17. {
  18. base.OnAppearing();
  19. GetActualLocationCommand.Execute(null);
  20. }
  21.  
  22. public static readonly BindableProperty GetActualLocationCommandProperty =
  23. BindableProperty.Create(nameof(GetActualLocationCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay);
  24.  
  25. public ICommand GetActualLocationCommand
  26. {
  27. get { return (ICommand)GetValue(GetActualLocationCommandProperty); }
  28. set { SetValue(GetActualLocationCommandProperty, value); }
  29. }
  30.  
  31. async Task GetActualLocation()
  32. {
  33. try
  34. {
  35. var request = new GeolocationRequest(GeolocationAccuracy.High);
  36. var location = await Geolocation.GetLocationAsync(request);
  37.  
  38. if (location != null)
  39. {
  40. map.MoveToRegion(MapSpan.FromCenterAndRadius(
  41. new Position(location.Latitude, location.Longitude), Distance.FromMiles(0.3)));
  42.  
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. await DisplayAlert("Error", "Unable to get actual location", "Ok");
  48. }
  49. }
  50.  
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement