Advertisement
eduardogr

Untitled

Jan 3rd, 2021
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1.  
  2.         private Address _SelectedItemFrom;
  3.         public Address SelectedItemFrom {
  4.             get { return _SelectedItemFrom; }
  5.             set
  6.             {
  7.                 if (_SelectedItemFrom != value) {
  8.                     _SelectedItemFrom = value;
  9.                     RaisePropertyChanged();
  10.                     getCoordinatesAsync("from");
  11.                 }
  12.             }
  13.         }
  14.  
  15.  
  16.         private Address _SelectedItemTo;
  17.         public Address SelectedItemTo {
  18.             get { return _SelectedItemTo; }
  19.             set
  20.             {
  21.                 if (_SelectedItemTo != value) {
  22.                     _SelectedItemTo = value;
  23.                     RaisePropertyChanged();
  24.                     getCoordinatesAsync("to");
  25.                 }
  26.             }
  27.         }
  28.  
  29.         public ICommand StartNavigation { get; set; }
  30.  
  31.         public MapNavigarionVM() {
  32.  
  33.             StartNavigation = new Command(async () => {
  34.  
  35.                 await Navigate();
  36.             });
  37.         }
  38.         private async void getCoordinatesAsync(string ToOrFrom) {
  39.  
  40.             try {
  41.                 if (ToOrFrom.Equals("to")) {
  42.                     var address = $"{SelectedItemTo.street} , {SelectedItemTo.city} , {SelectedItemTo.state}";
  43.                     var locations = await Geocoding.GetLocationsAsync(address);
  44.                     var location = locations?.FirstOrDefault();
  45.                     if (location != null) {
  46.                         SelectedItemTo.Lat = location.Latitude;
  47.                         SelectedItemTo.lon = location.Longitude;
  48.                     }
  49.                 } else if (ToOrFrom.Equals("from")) {
  50.                     var address = $"{SelectedItemFrom.street} , {SelectedItemFrom.city} , {SelectedItemFrom.state}";
  51.                     var locations = await Geocoding.GetLocationsAsync(address);
  52.                     var location = locations?.FirstOrDefault();
  53.                     if (location != null) {
  54.                         SelectedItemFrom.Lat = location.Latitude;
  55.                         SelectedItemFrom.lon = location.Longitude;
  56.                     }
  57.                 }
  58.             } catch (Exception ex) {
  59.                 await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
  60.             }
  61.         }
  62.  
  63.         public async Task Navigate() {
  64.             var location = new Location(SelectedItemTo.lon, SelectedItemTo.Lat) ;
  65.             var options = new MapLaunchOptions { NavigationMode = NavigationMode.Driving };
  66.  
  67.             await Map.OpenAsync(location, options);
  68.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement