Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Windows.Input;
  9. using TrackingSample.Helpers;
  10. using TrackingSample.Models;
  11. using TrackingSample.Services;
  12. using Xamarin.Essentials;
  13. using Xamarin.Forms;
  14. using Xamarin.Forms.GoogleMaps;
  15.  
  16. namespace TrackingSample.ViewModels
  17. {
  18. public class MainViewModel : INotifyPropertyChanged
  19. {
  20. public ICommand CalculateRouteCommand { get; set; }
  21. public ICommand UpdatePositionCommand { get; set; }
  22.  
  23. public ICommand LoadRouteCommand { get; set; }
  24. public ICommand StopRouteCommand { get; set; }
  25. IGoogleMapsApiService googleMapsApi = new GoogleMapsApiService();
  26.  
  27. public bool HasRouteRunning { get; set; }
  28. string _originLatitud;
  29. string _originLongitud;
  30. string _destinationLatitud;
  31. string _destinationLongitud;
  32.  
  33. GooglePlaceAutoCompletePrediction _placeSelected;
  34. public GooglePlaceAutoCompletePrediction PlaceSelected { get
  35. {
  36. return _placeSelected;
  37. }
  38. set
  39. {
  40. _placeSelected = value;
  41. if (_placeSelected != null)
  42. GetPlaceDetailCommand.Execute(_placeSelected);
  43. }
  44. }
  45. public ICommand FocusOriginCommand { get; set; }
  46. public ICommand GetPlacesCommand { get; set; }
  47. public ICommand GetPlaceDetailCommand { get; set; }
  48.  
  49. public ObservableCollection<GooglePlaceAutoCompletePrediction> Places { get; set; }
  50. public ObservableCollection<GooglePlaceAutoCompletePrediction> RecentPlaces { get; set; } = new ObservableCollection<GooglePlaceAutoCompletePrediction>();
  51.  
  52. public bool ShowRecentPlaces { get; set; }
  53. bool _isPickupFocused = true;
  54.  
  55. string _pickupText;
  56. public string PickupText
  57. {
  58. get
  59. {
  60. return _pickupText;
  61. }
  62. set
  63. {
  64. _pickupText = value;
  65. if (!string.IsNullOrEmpty(_pickupText))
  66. {
  67. _isPickupFocused = true;
  68. GetPlacesCommand.Execute(_pickupText);
  69. }
  70. }
  71. }
  72.  
  73. string _originText;
  74. public string OriginText
  75. {
  76. get
  77. {
  78. return _originText;
  79. }
  80. set
  81. {
  82. _originText = value;
  83. if (!string.IsNullOrEmpty(_originText))
  84. {
  85. _isPickupFocused = false;
  86. GetPlacesCommand.Execute(_originText);
  87. }
  88. }
  89. }
  90.  
  91. public ICommand GetLocationNameCommand { get; set; }
  92. public bool IsRouteNotRunning { get {
  93. return !HasRouteRunning;
  94. }
  95. }
  96.  
  97. public MainViewModel()
  98. {
  99. LoadRouteCommand = new Command(async () => await LoadRoute());
  100. StopRouteCommand = new Command(StopRoute);
  101. GetPlacesCommand = new Command<string>(async (param) => await GetPlacesByName(param));
  102. GetPlaceDetailCommand = new Command<GooglePlaceAutoCompletePrediction>(async (param) => await GetPlacesDetail(param));
  103. GetLocationNameCommand = new Command<Position>(async (param) => await GetLocationName(param));
  104. }
  105.  
  106. public async Task LoadRoute()
  107. {
  108. var positionIndex = 1;
  109. var googleDirection = await googleMapsApi.GetDirections(_originLatitud, _originLongitud, _destinationLatitud, _destinationLongitud);
  110. if(googleDirection.Routes!=null && googleDirection.Routes.Count>0)
  111. {
  112. var positions = (Enumerable.ToList(PolylineHelper.Decode(googleDirection.Routes.First().OverviewPolyline.Points)));
  113. CalculateRouteCommand.Execute(positions);
  114.  
  115. HasRouteRunning = true;
  116.  
  117. //Location tracking simulation
  118. Device.StartTimer(TimeSpan.FromSeconds(1),() =>
  119. {
  120. if(positions.Count>positionIndex && HasRouteRunning)
  121. {
  122. UpdatePositionCommand.Execute(positions[positionIndex]);
  123. positionIndex++;
  124. return true;
  125. }
  126. else
  127. {
  128. return false;
  129. }
  130. });
  131. }
  132. else
  133. {
  134. await App.Current.MainPage.DisplayAlert(":(", "No route found", "Ok");
  135. }
  136.  
  137. }
  138. public void StopRoute()
  139. {
  140. HasRouteRunning = false;
  141. }
  142.  
  143. public async Task GetPlacesByName(string placeText)
  144. {
  145. var places = await googleMapsApi.GetPlaces(placeText);
  146. var placeResult= places.AutoCompletePlaces;
  147. if (placeResult != null && placeResult.Count > 0)
  148. {
  149. Places = new ObservableCollection<GooglePlaceAutoCompletePrediction>(placeResult);
  150. }
  151.  
  152. ShowRecentPlaces = (placeResult == null || placeResult.Count ==0);
  153. }
  154.  
  155. public async Task GetPlacesDetail(GooglePlaceAutoCompletePrediction placeA)
  156. {
  157. var place = await googleMapsApi.GetPlaceDetails(placeA.PlaceId);
  158. if (place != null)
  159. {
  160. if (_isPickupFocused)
  161. {
  162. PickupText = place.Name;
  163. _originLatitud = $"{place.Latitude}";
  164. _originLongitud = $"{place.Longitude}";
  165. _isPickupFocused = false;
  166. FocusOriginCommand.Execute(null);
  167. }
  168. else
  169. {
  170. _destinationLatitud = $"{place.Latitude}";
  171. _destinationLongitud = $"{place.Longitude}";
  172.  
  173. RecentPlaces.Add(placeA);
  174.  
  175. if (_originLatitud == _destinationLatitud && _originLongitud == _destinationLongitud)
  176. {
  177. await App.Current.MainPage.DisplayAlert("Error", "Origin route should be different than destination route", "Ok");
  178. }
  179. else
  180. {
  181. LoadRouteCommand.Execute(null);
  182. await App.Current.MainPage.Navigation.PopAsync(false);
  183. CleanFields();
  184. }
  185.  
  186. }
  187. }
  188. }
  189.  
  190. void CleanFields()
  191. {
  192. PickupText = OriginText = string.Empty;
  193. ShowRecentPlaces = true;
  194. PlaceSelected = null;
  195. }
  196.  
  197.  
  198. //Get place
  199. public async Task GetLocationName(Position position)
  200. {
  201. try
  202. {
  203. var placemarks = await Geocoding.GetPlacemarksAsync(position.Latitude, position.Longitude);
  204. var placemark = placemarks?.FirstOrDefault();
  205. if (placemark != null)
  206. {
  207. PickupText = placemark.FeatureName;
  208. }
  209. else
  210. {
  211. PickupText = string.Empty;
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. Debug.WriteLine(ex.ToString());
  217. }
  218. }
  219.  
  220. public event PropertyChangedEventHandler PropertyChanged;
  221.  
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement