Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Input;
  6. using TrackingSample.Views;
  7. using Xamarin.Essentials;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.GoogleMaps;
  10.  
  11. namespace TrackingSample
  12. {
  13. public partial class MainPage : ContentPage
  14. {
  15. public static readonly BindableProperty CalculateCommandProperty =
  16. BindableProperty.Create(nameof(CalculateCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay);
  17.  
  18. public ICommand CalculateCommand
  19. {
  20. get { return (ICommand)GetValue(CalculateCommandProperty); }
  21. set { SetValue(CalculateCommandProperty, value); }
  22. }
  23.  
  24. public static readonly BindableProperty UpdateCommandProperty =
  25. BindableProperty.Create(nameof(UpdateCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay);
  26.  
  27. public ICommand UpdateCommand
  28. {
  29. get { return (ICommand)GetValue(UpdateCommandProperty); }
  30. set { SetValue(UpdateCommandProperty, value); }
  31. }
  32.  
  33. public MainPage()
  34. {
  35. InitializeComponent();
  36. CalculateCommand = new Command<List<Xamarin.Forms.GoogleMaps.Position>>(Calculate);
  37. UpdateCommand = new Command<Xamarin.Forms.GoogleMaps.Position>(Update);
  38. GetActualLocationCommand = new Command(async () => await GetActualLocation());
  39.  
  40. }
  41.  
  42. async void Update(Xamarin.Forms.GoogleMaps.Position position)
  43. {
  44. if (map.Pins.Count == 1 && map.Polylines!=null&& map.Polylines?.Count>1)
  45. return;
  46.  
  47. var cPin = map.Pins.FirstOrDefault();
  48.  
  49. if (cPin != null)
  50. {
  51. cPin.Position = new Position(position.Latitude, position.Longitude);
  52. cPin.Icon = BitmapDescriptorFactory.FromView(new Image() { Source = "ic_taxi.png", WidthRequest = 25, HeightRequest = 25 });
  53.  
  54. await map.MoveCamera(CameraUpdateFactory.NewPosition(new Position(position.Latitude, position.Longitude)));
  55. var previousPosition = map?.Polylines?.FirstOrDefault()?.Positions?.FirstOrDefault();
  56. map.Polylines?.FirstOrDefault()?.Positions?.Remove(previousPosition.Value);
  57. }
  58. else
  59. {
  60. //END TRIP
  61. map.Polylines?.FirstOrDefault()?.Positions?.Clear();
  62. }
  63.  
  64.  
  65. }
  66.  
  67. void Calculate(List<Xamarin.Forms.GoogleMaps.Position> list)
  68. {
  69. searchLayout.IsVisible = false;
  70. stopRouteButton.IsVisible = true;
  71. map.Polylines.Clear();
  72. var polyline = new Xamarin.Forms.GoogleMaps.Polyline();
  73. foreach (var p in list)
  74. {
  75. polyline.Positions.Add(p);
  76.  
  77. }
  78. map.Polylines.Add(polyline);
  79. map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(polyline.Positions[0].Latitude, polyline.Positions[0].Longitude), Xamarin.Forms.GoogleMaps.Distance.FromMiles(0.50f)));
  80.  
  81. var pin = new Xamarin.Forms.GoogleMaps.Pin
  82. {
  83. Type = PinType.Place,
  84. Position = new Position(polyline.Positions.First().Latitude, polyline.Positions.First().Longitude),
  85. Label = "First",
  86. Address = "First",
  87. Tag = string.Empty,
  88. Icon = BitmapDescriptorFactory.FromView(new Image() { Source = "ic_taxi.png", WidthRequest = 25, HeightRequest = 25 })
  89.  
  90. };
  91. map.Pins.Add(pin);
  92. var pin1 = new Xamarin.Forms.GoogleMaps.Pin
  93. {
  94. Type = PinType.Place,
  95. Position = new Position(polyline.Positions.Last().Latitude, polyline.Positions.Last().Longitude),
  96. Label = "Last",
  97. Address = "Last",
  98. Tag = string.Empty
  99. };
  100. map.Pins.Add(pin1);
  101. }
  102.  
  103. public async void OnEnterAddressTapped(object sender, EventArgs e)
  104. {
  105. await Navigation.PushAsync(new SearchPlacePage() { BindingContext = this.BindingContext }, false);
  106.  
  107. }
  108.  
  109. public void Handle_Stop_Clicked(object sender, EventArgs e)
  110. {
  111. searchLayout.IsVisible = true;
  112. stopRouteButton.IsVisible = false;
  113. map.Polylines.Clear();
  114. map.Pins.Clear();
  115. }
  116.  
  117. //Center map in actual location
  118. protected override void OnAppearing()
  119. {
  120. base.OnAppearing();
  121. GetActualLocationCommand.Execute(null);
  122. }
  123.  
  124. public static readonly BindableProperty GetActualLocationCommandProperty =
  125. BindableProperty.Create(nameof(GetActualLocationCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay);
  126.  
  127. public ICommand GetActualLocationCommand
  128. {
  129. get { return (ICommand)GetValue(GetActualLocationCommandProperty); }
  130. set { SetValue(GetActualLocationCommandProperty, value); }
  131. }
  132.  
  133. async Task GetActualLocation()
  134. {
  135. try
  136. {
  137. var request = new GeolocationRequest(GeolocationAccuracy.High);
  138. var location = await Geolocation.GetLocationAsync(request);
  139.  
  140. if (location != null)
  141. {
  142. map.MoveToRegion(MapSpan.FromCenterAndRadius(
  143. new Position(location.Latitude, location.Longitude), Distance.FromMiles(0.3)));
  144.  
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. await DisplayAlert("Error", "Unable to get actual location", "Ok");
  150. }
  151. }
  152.  
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement